Skip to content
Advertisement

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. staffIdin 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?

Advertisement

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement