Datatables – Add Id to Table Row of a Newly Created Row

I’m a big fan of the jquery datatables grid plugin,  I think it’s one of the best grids around (  However, I struggled to overcome quite a basic task – so I hope the example below might help somebody.

var oTable    = $(‘#datatablesTable’).dataTable();
var oSettings = oTable.fnSettings();

var aiNew = oTable.fnAddData( [
'<input type="text" id="qty'+ widgetId +'" readonly="readonly" value="0" style="width: 30px;" />',
'' ]);

var nNewTr = oSettings.aoData[ aiNew[0] ].nTr;
$(nNewTr).attr(‘id’, ’1);

The example above call the internal fnAddData function to inject a single row.  It
then grabs the table row element in then NewTr variable and finally we use the .attr jquery function to create a new row id with the value 1 to the nNewTr element.

This entry was posted in Development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>