set an id on jQuery datatables to be used by TableDnD plugin
I have a table which I am loading data into from a server side PHP script which returns the table as JSON for jQuery dataTables to render. I am then using the jQuery tableDnD plugin to allow drag and drop re-ordering of tables, this requires that the rows of the table contain the id matching the table row in the databas开发者_JS百科e ie <tr id="1">
I am wondering how I can achieve this using dataTables as it doesn't allow me to specify this.
My current thinking is to use a callback on the data maybe, that or rewrite tableDnD to read the id value from an index column, does anyone have any ideas regarding this?
The call back proved to be the answer,
$('#dataTable').dataTable( {
...
"aoColumns" : [
{ sWidth:'40px' },
{ sWidth:'40px',sClass:'position' },
...
{ sWidth:'3px',sClass:'handle' }
],
...
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
$(nRow).attr("id",aData[0]);
return nRow;
},
using the fnRowCallback
I was able to add the attribute id to the table row and then load it with the id which was the first element of data returned aData[0]
I found I can use the "aoColumns"
to load both width and the classes 'position', and 'handle' needed for my jQuery tableDnD.
I guess I should read the manual a bit more next time!
精彩评论