fnGetNodes not working properly.

fnGetNodes not working properly.

javacjavac Posts: 2Questions: 0Answers: 0
edited July 2010 in General
I modified the example dataTables-1.7/examples/api/select_row.html with the following script. It seams that the oTable.fnGetNodes(0) is returning wrong row instead of returning first fow. Is is I am doing some thing obviously wrong.

var activerow = -1;
var act;
$(document).ready(function() {

/* Init the table */
var oTable = $('#example').dataTable( {
"fnRowCallback": function(nRow, aData, iDisplayIndex) {
$(nRow).click( function () {
if(activerow >= 0) {
alert($(act).html());
alert(activerow);
alert($(oTable.fnGetNodes(0)).html());
$(act).removeClass('row_selected');
}
$(this).addClass('row_selected');
act = this;
activerow = iDisplayIndex;
} );
return nRow;
}
});
} );

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The index passed to fnGetNodes is not the visible index, but rather the internal cache index that DataTables maintains. This is before sorting and filtering (i.e the order that the data was originally read in). I've updated the documentation to make this clearer.

    The plug-in fnGetDisplayNodes ( http://datatables.net/plug-ins/api#fnGetDisplayNodes ) will get the node based on the visible index for you.

    Regards,
    Allan
  • javacjavac Posts: 2Questions: 0Answers: 0
    Thanks Allan for the update and quick answer. That brings couple of questions in light.
    1) When we call fnUpdate and pass activerow in second parameter is that visible index or cached index.

    2) Is there anyway to get cached index in the fnRowCallback. As I understand iDisplayIndex is the display index.

    Thanks.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    1. fnUpdate takes the cached index (the index in the aoData array).

    2. If you are using fnUpdate inside fnRowCallback, you can take advantage of the fact that you can pass the TR node as the second parameter to fnUpdate (rather than an integer). Something like:

    [code]
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
    this.fnUpdate( [...], nRow );
    }
    [/code]
    Note that this is using syntax for DataTables 1.7 which is currently in beta. The only difference is the use of 'this'. 1.7 makes it much easier to use API functions inside the callbacks, by changing the execution scope. However the same principle of using nRow applies in 1.6.

    Allan
This discussion has been closed.