13

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?

7
  • staffId in my case is a number. Nope, 'staffId' is a string in your code. Commented Apr 16, 2016 at 10:42
  • @hindmost, nonetheless, the source code looks like: <tr id="2" role="row" class="odd"> ... </tr> which is no good I think. Commented Apr 16, 2016 at 10:46
  • Yes! ID attribute should not start with Numbers as per the docs.. You need to define Not working! Commented Apr 16, 2016 at 10:47
  • @RayonDabre, so that was the question. How do I add some prefix when assign IDs to get something like <tr id="staff_1"> Commented Apr 16, 2016 at 10:48
  • @Deka87, What is the current id after the code you have applied ? It looks good to me... Commented Apr 16, 2016 at 10:49

2 Answers 2

42

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

Sign up to request clarification or add additional context in comments.

9 Comments

Though so too, however it gives an error "staffId is not defined".
Have you defined it anywhere ? How do you expect it to be staff_1 without defining staffId ?
stuffId comes from a JSON file. rowId: "staffId" (with quotes) works alright. Please see the official docs: datatables.net/reference/option/rowId. The problem is that I cannot add a prefix to the ID assigned.
From the docs, the only source to set the id is data-source..There is no option to set it custom
Rayon, don't waste your time on this, I got the answer! =) I will post it here in a second.
|
5

This is a simple solution, just use

rowId: [1] 

This will set the value of first column as row id

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.