select first row onload

select first row onload

jterjter Posts: 1Questions: 0Answers: 0
edited November 2013 in General
I'm trying to auto select first row of table when data is loaded for the first time and failing to do that.

How do I auto select first row of table when table loads for the first time? This html doesn't work:
[code]
<!DOCTYPE html>





@import "DataTables/css/demo_page.css";
@import "DataTables/css/demo_table.css";








var oTable;
var firstTime = true;

$(document).ready(function () {

$("#example tbody").click(function (event) {
$(oTable.fnSettings().aoData).each(function () {
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});

oTable = $('#example').dataTable({
"sScrollX": "500px",
"sPaginationType": "full_numbers",

"bServerSide": true,
"sAjaxSource": "services/data3.ashx",
"sServerMethod": "POST",
"fnDrawCallback": function (oSettings) {
if (firstTime) {
alert('DataTables has redrawn the table');
oTable.$('tr:first').click();
firstTime = false;
}

}
});


});








id
name
surname








name
surname
id










[/code]

Replies

  • allanallan Posts: 61,920Questions: 1Answers: 10,151 Site admin
    First you are using static events - I'd very strongly suggest using delegated events: http://datatables.net/faqs#events

    Then all you need to do in fnInitComplete is add: `$('#example tbody tr:eq(0)').click();` .

    Allan
This discussion has been closed.