Pre-selecting a row based on column content with server-side loading

Pre-selecting a row based on column content with server-side loading

manuelmanuel Posts: 5Questions: 0Answers: 0
edited March 2011 in General
Hello,

I'm using the server-side loading and would like to have a row pre-selected when the table has been initialized, given a flag (that is rendered in HTML).

My current approach is as follows:

[code]
$(document).ready(function() {

// DataTable initialisation code

...

// pre-select a row
if ('${selectedUser}'.length > 0) {
$(tabDataTable.fnSettings().aoData).each(function () {
var nTr = $(this.nTr)[0];
if ($($(nTr).children('td')[0]).text() == '${selectedUser}') {
$(this.nTr).addClass('row_selected');
}
});

}
[/code]

${selectedUser} will generate "1" in my example, and the first column (of which I am extracting the text) contains an ID (I'm not sure if this is the proper way of handling such a case, but I found it to be working quite well).

I have checked that the condition above works, and when debugging the code (in Firefox), the row is indeed selected for a short while -- however, this does not work when the page is loaded normally.

Any ideas what could be going wrong here?

Thanks!

Manuel

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Where is this code being executed? It sounds like it would be inside fnDrawCallback? That way it will run every time the table is drawn, and the rows will always be up-to-date with the selection marker.

    Allan
  • manuelmanuel Posts: 5Questions: 0Answers: 0
    Hi Allan,

    thanks, that was it. I had the code sitting after the table initialization, so when the table was re-drawn the selection was lost. It now works fine!

    Manuel
This discussion has been closed.