According to the Datatables specs, I can add a unique ID to every row in my table:
JavaScript
x
5
1
$('#myTable').DataTable( {
2
ajax: '/api/staff',
3
rowId: 'staffId'
4
} );
5
However, the same spec says that IDs cannot start with a number. staffId
in my case is a number, I tried to add some prefix to it:
JavaScript
1
5
1
$('#myTable').DataTable( {
2
ajax: '/api/staff',
3
rowId: 'id_' + 'staffId'
4
} );
5
However, this didn’t work. Any ideas?
Advertisement
Answer
Use function-expression
as a value for rowId
and return manipulated string to be used as ID
JavaScript
1
16
16
1
var appListTable = $(".app-list__table").DataTable({
2
dom: "",
3
// Load json with list of applicants
4
ajax: "https://api.myjson.com/bins/391gc",
5
columns: [{
6
"data": "uid"
7
}, {
8
"data": "location"
9
}, {
10
"data": "date"
11
}],
12
// Set rows IDs
13
rowId: function(a) {
14
return 'id_' + a.uid;
15
},
16
});