Pre-selecting a row based on column content with server-side loading
Pre-selecting a row based on column content with server-side loading
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
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
This discussion has been closed.
Replies
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