retrieving id from row in datatable

retrieving id from row in datatable

samwsamw Posts: 15Questions: 7Answers: 0

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

  • kthorngrenkthorngren Posts: 21,152Questions: 26Answers: 4,919
    Answer ✓

    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:

    Important This method does not read the DOM id for the tr element, but rather gets the row id from the row's data source (location specified by rowId).

    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 not row().EmpID() API. Also there is no attr() API for Datatables for tbl.attr("id");.

    Kevin

  • allanallan Posts: 63,175Questions: 1Answers: 10,409 Site admin

    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's id you could just use this.id.

    Allan

  • samwsamw Posts: 15Questions: 7Answers: 0

    the sample you provided helped, thank you

This discussion has been closed.