DT_RowId in a 2-dim static array?

DT_RowId in a 2-dim static array?

fburleighfburleigh Posts: 3Questions: 0Answers: 0
edited June 2011 in DataTables 1.8
Suppose DataTables is given a 2 dimensional array as its data source:

aDataset=[
["item 1", 1],
["item 2", 2]
...
];

Then let's say we create the table like this:

$('#datadisplay').dataTable( {
bFilter:false,
bInfo:false,
iDisplayLength:25,
bLengthChange:false,
aaData:aDataset,
aoColumns:aColumns
} );

What would I do to give each row a DT_RowId? I tried:
{DT_RowID:"item 1"} and a programmed aDataset[i]["DT_RowId"]=
and got what I thought I needed (in the second case) but, alas, that's not what DT 1.8 wants.

Thanks.

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    In order to use DT_RowId you would need to be passing DataTables an object and use mDataProp to read the contents for each column. Unfortunately in JSON it is not possible to specify an array with properties attached to it, although it is of course possible in Javascript itself. So there are a number of options

    1. Use objects
    2. Assign DT_RowId to the array as a property before giving it to DataTables (i.e. pre-process it)
    3. Add the ID using standard DOM methods in the draw callback (fnDrawCallback)

    Allan
  • fburleighfburleigh Posts: 3Questions: 0Answers: 0
    Thanks, Allen. Could I ask for minimal code examples? I believe I tried both 1. and 2., like this

    1. Object in each row: aDataset = [[1, 2, ..., {DT_RowId:value}], ... ];
    2. Preprocess javascript 2-dim array before making DT -- for each row i: aDataset[i].DT_RowId="value";

    console.log() on 2. seemed like it ought to have "worked" to me.

    I'm clearly overlooking something.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    1. To use objects, you need to do something like that shown in this example: http://datatables.net/release-datatables/examples/ajax/objects.html .

    2. I would have expected to work as well. Can you link us to a page with that code which isn't working?

    Allan
  • garryclgarrycl Posts: 1Questions: 0Answers: 0
    This has worked for me with PHP:
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $row = array();
    for ( $i=0 ; $i
  • hemmeterhemmeter Posts: 6Questions: 0Answers: 0
    I have tried #2 in fburleigh's response and it doesn't seem to take. I am setting the DT_RowId property on each array inside of aaData, but it has no effect. Ideas?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    What does your JSON return look like?

    Allan
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    Actually, I'm seeing the same issue. Returning a DT_RowId, but when I view the generated HTML using IE Developer Tools, the TR.id field actually holds the value from the first column in my aaData.

    I'll try to post some JSON as well.
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    My JSON:

    {"iTotalDisplayRecords":31,"iTotalRecords":31,"aaData":[{"3":"","2":"1 Years","1":"United States","0":"CDU2B","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"111","DT_RowId":"0"},{"3":"","2":"2 Years","1":"United States","0":"CDU2B","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"100","DT_RowId":"9"},{"3":"","2":"5 Years","1":"United States","0":"CDU2B","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"94.35","DT_RowId":"13"},{"3":"","2":"3 Years","1":"United States","0":"CDU2B","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"94.35","DT_RowId":"17"},{"3":"","2":"4 Years","1":"United States","0":"CDU2B","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"94.35","DT_RowId":"20"},{"3":"","2":"2 Years","1":"United States","0":"CSU00","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"47.7","DT_RowId":"1"},{"3":"","2":"1 Years","1":"United States","0":"CSU00","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"53","DT_RowId":"5"},{"3":"","2":"3 Years","1":"United States","0":"CSU00","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"45.05","DT_RowId":"6"},{"3":"","2":"4 Years","1":"United States","0":"CSU00","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"45.05","DT_RowId":"16"},{"3":"","2":"5 Years","1":"United States","0":"CSU00","7":"","6":"01/07/2008 01:00:00","5":"USD","4":"45.05","DT_RowId":"26"}],"sEcho":"1"}

    And when I look at the displayed HTML it says:



    I am using JEditable and the DataTable/Editable plugin. Looking at the source, that doesn't seem to affect it. Stepping through the DataTables source, seems to show it correctly setting the ID:

    oData.nTr.setAttribute( 'id', oData._aData.DT_RowId );

    Looking at the example you suggest (http://www.datatables.net/beta/1.8/examples/server_side/ids.html) I see yours working, but I don't think I can see a difference.
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Can you link to your example please? I don't see anything obviously wrong with what you have there.

    Allan
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    I'm really sorry but I can't - internal corporate website.

    The odd bit is that even though the DT_RowID I'm returning is "1", the tr.ID field says "CDU2B".
    Unless I've misunderstood, the tr.ID field should equal the DT_RowID field?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    From your code I would have expected it to be an integer such as 26 or whatever DT_RowID is set to for that column. I don't see why that wouldn't work from the above code I'm afraid.

    Allan
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    Well - 1 or 26 - I expected a number not CDU2B ;)
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    I'm still seeing this problem, and it's two days before Production deployment :(

    The problem I'm seeing is that I'm using the two jeditable plugins - and when I perform edits on Page 2, the "sUpdateURL" callback doesn't pass me a page number, and the rowID is the row number on THAT page, not the accumalative row number - so I think I'm applying edits to the first page as the rowID is 5 or so.

    I still can't get DT_RowID to be used on the web page. All the examples on the website aren't implemented using a server call, so I think you avoid seeing the sUpdateURL page number problem.

    I'll keep searching for clues, but if anyone comes up with any suggestions, I would be very eager to try them out.
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    OK, it seems that 'jquery.dataTables.editable.js' is to blame. This portion of code doesn't even try to preserve DT_RowId, when an update occurs. It just takes the first field returned from the server call, which is the first column of data.

    [code]
    $(oTable.fnGetNodes()).each(function () {
    var position = oTable.fnGetPosition(this);
    var id=oTable.fnGetData(position)[0];
    properties.fnSetRowID($(this), id);
    }
    );
    [/code]

    Currently testing to see if this code would be better:

    [code]
    $(oTable.fnGetNodes()).each(function () {
    var position = oTable.fnGetPosition(this);
    var data = oTable.fnGetData(position);
    var id;
    if ( typeof data.DT_RowId != 'undefined' ) {
    id = data.DT_RowId;
    } else {
    id = data[0];
    }
    properties.fnSetRowID($(this), id);
    }
    );
    [/code]
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Sounds like a good change. Might be worth raising a bug against the editable plug-in ( http://code.google.com/p/jquery-datatables-editable/ ) for this, as that sounds rather important to me!

    Hope the release goes smoothly :-)

    Allan
  • cs94njwcs94njw Posts: 7Questions: 0Answers: 0
    edited September 2011
    Raised, and many thanks ;)

    http://code.google.com/p/jquery-datatables-editable/issues/detail?id=63
This discussion has been closed.