How to set up the row ID properly
How to set up the row ID properly
in DataTables
Hi there, I would need more help how to use and setup the rowID as I am new on the web. I am not sure what staffID means in the description (whether it is a custom or built-in property?). I am also not sure if I need to set up a column structure and how to be in line with the IDs. Could you help me?
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
select: true,
stateSave: true,
ajax: '/api/staff',
rowId: 'staffId',
columnDefs: [ {
orderable: false,
className: 'select-checkbox',
targets: 0
} ],
select: {
style: 'os',
selector: 'td:first-child'
},
order: [[ 1, 'asc' ]],
"ajax": "data.txt"
} );
$("#delBtn").click(function(){
var id = table.rows( { selected: true } ).ids().toArray();
alert( 'Clicked row id '+id);
});
$('#example').on( 'click', 'tr', function () {
var id = table.row( this ).id();
alert( 'Clicked row id '+id );
} );
setInterval( function () {
var rows = table.rows({selected: true});
table.ajax.reload(
function () {rows.select()}, false );},
1000 );
});
</script>
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
This discussion has been closed.
Answers
Since you don't have
columns.datayour row data is arrays instead of objects. See the data docs for details. I'm not sure ifrowIdwill work with array based data which uses indexes (numbers) to access the row data instead of a string which is used for objects. TherowIDdocs state it takes a string parameter.The
rowIddocs haverowId: 'staffId'in the example. ThestaffIdis one of the object keys. You would use a key that has unique data for the column. It is recommended to use object instead of arrays for the row data.Kevin