Proper syntax for calling fields in mRender function or mData
Proper syntax for calling fields in mRender function or mData
troubledcoder
Posts: 2Questions: 0Answers: 0
Hello and thank you for this excellent jQuery tool. I am using it to display SQL database information information in an ASP classic application. I use "sAjaxSource": "/json_data.asp" to get the data, which is in the "aaData" array of objects format. { "aaData" : [ {"Field1"."Value1","Field2"."Value2"},{"Field2"."Value1","Field2"."Value2"} ] }.
Using this data source and using {"mData":"FIRST_NAME"} (etc.) in aoColumns to bind the specific data to the columns, everything is working fine. Now the hard part for me, since I'm a beginner at JS and JQuery.
What is the correct syntax to use when calling other fields from the data into that column? Whether it's for an "if" statement or "return" within a function, or calling them directly without a function. For example, within aoColumns or aoColumnDefs:
a) [code] { "mRender" : function (data, type, full) { return {"mData": "FIRST_NAME"} + ' ' + {"mData": "LAST_NAME"}; } }, [/code]
where FIRST_NAME and LAST_NAME are the field names in the data source
or
b) [code] { "mRender" : function (data) { return data.aaData[3] + ' ' + data.aaData[4]; } }, [/code]
where aaData[3] and aaData[4] are the positions of the data in the aaData array, not necessarily the table being displayed.
or
c) [code] { "mData" : "FIRST_NAME" + ' ' + "LAST_NAME"} [/code]
would have been the simplest.
I'm unclear on the differences between aoColumns and aoColumnDefs (I looked up the reference info on aoColumnDefs but couldn't find a list of methods etc like I could for other things), and also when to use mData vs mRender (or both) and examples of each.
Most importantly I need to know how to reference one or more fields from the data source with the proper syntax.
In example a) above I am given the typical "Requested unknown parameter 0 from the data source for row 0" and in example b) I'm told that "data is null" (I've seen examples of using "data" as an object name or variable referring to the current column or record's data). For c) I get the unknown parameter FIRST_NAME LAST_NAME or if I wrap those in {} I get unknown token errors.
For those suggesting I should combine / reference the other data fields when querying my data source or constructing the array, note that neither of those options are available to me. I need to combine two fields in the display of the column, or in another column I need to use a value from one field in an while displaying the value from another field.
Thanks very much for this help!
Using this data source and using {"mData":"FIRST_NAME"} (etc.) in aoColumns to bind the specific data to the columns, everything is working fine. Now the hard part for me, since I'm a beginner at JS and JQuery.
What is the correct syntax to use when calling other fields from the data into that column? Whether it's for an "if" statement or "return" within a function, or calling them directly without a function. For example, within aoColumns or aoColumnDefs:
a) [code] { "mRender" : function (data, type, full) { return {"mData": "FIRST_NAME"} + ' ' + {"mData": "LAST_NAME"}; } }, [/code]
where FIRST_NAME and LAST_NAME are the field names in the data source
or
b) [code] { "mRender" : function (data) { return data.aaData[3] + ' ' + data.aaData[4]; } }, [/code]
where aaData[3] and aaData[4] are the positions of the data in the aaData array, not necessarily the table being displayed.
or
c) [code] { "mData" : "FIRST_NAME" + ' ' + "LAST_NAME"} [/code]
would have been the simplest.
I'm unclear on the differences between aoColumns and aoColumnDefs (I looked up the reference info on aoColumnDefs but couldn't find a list of methods etc like I could for other things), and also when to use mData vs mRender (or both) and examples of each.
Most importantly I need to know how to reference one or more fields from the data source with the proper syntax.
In example a) above I am given the typical "Requested unknown parameter 0 from the data source for row 0" and in example b) I'm told that "data is null" (I've seen examples of using "data" as an object name or variable referring to the current column or record's data). For c) I get the unknown parameter FIRST_NAME LAST_NAME or if I wrap those in {} I get unknown token errors.
For those suggesting I should combine / reference the other data fields when querying my data source or constructing the array, note that neither of those options are available to me. I need to combine two fields in the display of the column, or in another column I need to use a value from one field in an while displaying the value from another field.
Thanks very much for this help!
This discussion has been closed.
Replies
[code]
mRender: function ( data, type, row ) {
return row.FIRST_NAME +' '+ row.LAST_NAME;
}
[/code]
Why the examples you posted don't work:
a. you are returning an object from the function, which is meaningless to DataTables. It expects something that can be displayed in the table.
b. aaData[3] doesn't mean anything here since you don't have a property aaData which is an array.
c. Your mData there is just the string: "FIRST_NAME LAST_NAME", which also doesn't mean anything to DataTables. It could split on the space, but what if you didn't want a space, it would need to know what to split on. the function method I described above is the simplest and most flexible.
Allan