Tuesday, October 18, 2011

Add ID to TR using Datatables

I find datatables for sorting of table very useful. (See datatables.net) It has live search and very flexible with customization. In my project, I tried to insert a new row by using the following sytax:
var myTable = $('#result_table').dataTable( );
var checkbox = 
var edit = Edit

$('#result_table').dataTable().fnAddData( [
     $('#add_division_name').val(),
     checkbox,
     edit
] );                          
But the problem is since the insertion is automated, I could not insert an ID for TR on the fly. So here's the solution I got:
// Get the settings of the table
var oSettings = $('#result_table').dataTable().fnSettings();       
// Count the rows, server side data
var rows = data.count;
// Insert the server side id
oSettings.aoData[rows].nTr.id = 'tr-' + data.id; 
I tried counting the hidden and shown rows so the counting of rows will be made front end by using the :hidden selector, but my codes doesn't seem to work. Looks like I have to stick to server side counting of rows.

No comments:

Post a Comment