Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 285
sdvnksv Avatar asked Apr 16 '16 10:04

sdvnksv


2 Answers

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

like image 60
Rayon Avatar answered Oct 20 '22 01:10

Rayon


This is a simple solution, just use

rowId: [1]

This will set the value of first column as row id

like image 32
sherin Avatar answered Oct 20 '22 01:10

sherin