Advanced row ID question
Advanced row ID question
luketheobscure
Posts: 20Questions: 0Answers: 0
I'm writing a page that will be hit pretty hard- lots of data, lots of bandwidth, and lots of processing. I need to add an ID to each row in the data table for some of my functions to work. The ID is coming across as a column in the JSON data. Is there any way to tell datatables to load a particular column as the ID? Note: I hide this column when initializing the table.
Changing the format of the JSON seems really sub optimal- currently each row of data (and there will be thousands+) just look like "['name','county',ID]". Changing it to something along the lines of "{'DT_RowClass':'ID','col1':name,'col2':'County'}" would possible triple the bandwidth, and I would lose all the advantages of using JSON instead of XML.
Changing the format of the JSON seems really sub optimal- currently each row of data (and there will be thousands+) just look like "['name','county',ID]". Changing it to something along the lines of "{'DT_RowClass':'ID','col1':name,'col2':'County'}" would possible triple the bandwidth, and I would lose all the advantages of using JSON instead of XML.
This discussion has been closed.
Replies
other than that, since you know the column number of your ID, you can use fnGetPosition and fnGetData to get the data in your ID column. this solution doesn't require changing your database columns or order.
[code]
$('#example tbody td').click( function () {
/* Get the position of the current data from the node */
var aPos = oTable.fnGetPosition( this );
/* Get the data array for this row */
var aData = oTable.fnGetData( aPos[0] );
/* Get the ID from column 2 of this row */
id = aData[ 2 ];
} );
[/code]
I have some code that's working (I removed the row identifier on the aPos to get it to work), but I think I might be missing something. Here's what I've got:
[code]
jQuery('#results_table tbody tr').live('click', function () {
var oTable = jQuery("#results_table").dataTable();
var aPos = oTable.fnGetPosition( this );
/* Get the data array for this row */
var aData = oTable.fnGetData( aPos );
/* Get the ID from column 2 of this row */
var id = aData[ 0 ];
updateTable(id);
});
[/code]
But based on your initial comments, it seems like this is extraneous. I rearranged the query to return the id column first. In your opinion, is there a better way to accomplish this?
PS: Thanks again for all the help, you've really been a lifesaver.
the fnGetPosition/fnGetData is just to get the value of the ID if it's in another column.
here's a shot from my debugger
http://i.imgur.com/gtzCD.png
so you should be able to use
[code]
jQuery('#results_table tbody tr').live('click', function () {
updateTable(this.id);
});
[/code]
Here's the complete, non-truncated code that's generating the table:
[code]
function getGrowers(next) {
jQuery("#status").html("loading grower information...")
jQuery.getJSON("model/queries.cfc", {
method: 'getGrowers',
}, function(results){
var columns = [];
jQuery.each(results.COLUMNS, function(i, value){
var obj = { sTitle: value.toLowerCase() };
columns.push(obj);
});
jQuery('#results_area').html( '' );
jQuery('#results_table').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaData": results.DATA ,
"aoColumns": columns,
"aoColumnDefs": [
{ "bVisible": false, "aTargets": [ 0 ] }
]
}), next();
}
);
}
[/code]
Do you see anything out of place?
(maybe with mDataProp and using Objects instead of arrays, you can use DT_RowId with Ajax-sourced)
this is the server-side page that talks about the row id
http://www.datatables.net/usage/server-side