Advanced row ID question

Advanced row ID question

luketheobscureluketheobscure Posts: 20Questions: 0Answers: 0
edited August 2011 in General
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.

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    since the ID column is hidden, just set it as the first column. by default (unless you used DT_ to set the id) the value of column 0 will be your row's ID. you can get the value of the id by getting the TR parent of any TD.

    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]
  • luketheobscureluketheobscure Posts: 20Questions: 0Answers: 0
    Wow, thank you so much for your quick response! It took me a while to understand what you were saying- I was thinking the id would be in , instead of in the JS object.

    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.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    if the ID column is column 0, it should put the value of column 0 into the id of TR, so yes, you should be seeing

    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]
  • luketheobscureluketheobscure Posts: 20Questions: 0Answers: 0
    Strange, I'm not getting anything in the ID. Checked it in the Chrome debugger- nothing. I've got my bloated click code working, so it's not a big issue, but I'm curious why my table doesn't have a row ID (and I guess it would be nice to use that much cleaner code snippet).

    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?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    oh, maybe you're not using bServerSide? that's the case where the column 0 becomes the id. if you're just using AJAX-sourced data, I don't think it affects the ID

    (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
This discussion has been closed.