How can I add a class to the row I'm adding in the datatable?
If not possible, how can I use fnRowCallback
or fnDrawCallback
to change the class?
oTable = $('#example').dataTable( {
"bJQueryUI": true,
"bSortClasses": false,
"sDom":'T<"clear">',
"sPaginationType": "full_numbers",
"sDom": 'T<"clear"><"fg-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix"lfr>t<"fg-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix"ip>',
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
var oSettings = oTable.fnSettings();
oSettings.aoData[iDisplayIndex].nTr.className = "gradeX odd";
}
});
The above code is giving me an error.
this is how I add the row:
oTable.fnAddData(arr);
You can add the classname in your data itself as described in the documentation.
http://www.datatables.net/examples/server_side/ids.html
use DT_RowId for adding id for any row
use DT_RowClass for adding class for any row
use DT_RowData for adding html5 data object to any row
eg:
"data": [ {
"DT_RowId": "row_5",
"first_name": "Airi",
"last_name": "Satou",
"position": "Accountant",
"office": "Tokyo",
"start_date": "28th Nov 08",
"salary": "$162,700"
}]
Official documentation says:
Please read: rows.add()
Try changing your
fnRowCallback
to the following:You can refer to the offical documentation to further understanding this callback function.
After reading the documentation this work for me:
Alright, perhaps I'm not understanding exactly what your question is, but if you were just adding the row, why not set the class before you add it? Like this, somewhat sloppy, example:
This should do the trick: