Deka87 Deka87 - 7 months ago 54
Javascript Question

Datatables rowId starting with a number issue

According to the Datatables specs, I can add a unique ID to every row in my table:

$('#myTable').DataTable( {
ajax: '/api/staff',
rowId: 'staffId'
} );


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:

$('#myTable').DataTable( {
ajax: '/api/staff',
rowId: 'id_' + 'staffId'
} );


However, this didn't work. Any ideas?

Answer

Use function-expression as a value for rowId and return manipulated string to be used as ID

var appListTable = $(".app-list__table").DataTable({
  dom: "",
  // Load json with list of applicants
  ajax: "https://api.myjson.com/bins/391gc",
  columns: [{
    "data": "uid"
  }, {
    "data": "location"
  }, {
    "data": "date"
  }],
  // Set rows IDs
  rowId: function(a) {
    return 'id_' + a.uid;
  },
});

Fiddle here

Comments