retrieving id from row in datatable
retrieving id from row in datatable

If I have a list of employees in my datatable; How do I get the employee id from the row I select?
The field employee id from my viewmodel is EmpID.
var tbl = $('#EmployeeTable').DataTable();
$('#EmployeeTable tbody').on('click', 'tr', function ()
{
//see below statements (1 through 6)
}
I've tried the following:
1. var empID = tbl.row({ selected: true }).data()[0][0];
2. var empID = tbl.row({ selected: true }).data()[0];
3. var empID= tbl.row(this).id();
4. var empID= tbl.row(this).EmpID();
5. var empID= tbl.attr("id");
6. var empID= tbl.attr("EmpID");
This question has an accepted answers - jump to answer
Answers
See if this example works:
http://live.datatables.net/bamuyero/1/edit
It shows getting the row data and the row
data-id
attribute.{ selected: true }
works with the Select Extension.According to the
row().id()
docs:If your table is sourced form the DOM then that is likely why
tbl.row(this).id();
didn't work.I know you didn't intend this but
tbl.row(this).EmpID();
didn't work because there is notrow().EmpID()
API. Also there is noattr()
API for Datatables fortbl.attr("id");
.Kevin
Its also worth keeping in mind that the jQuery event handler callback scope is the element that was clicked on. So if you want the
tr
element'sid
you could just usethis.id
.Allan
the sample you provided helped, thank you