Return Row ID

Return Row ID

dtw11dtw11 Posts: 18Questions: 4Answers: 0

I have an icon in a column for my table. I want to return the id for the row. I have assigned a row id for each using DT_RowId. I can only get the first row id for every row. row(this) does not work.

$('#grid tbody tr td .fa-times').on('click', function () {
var rowid = table.row().id();
alert(rowid);
});

Answers

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    Just using table.row() is always going to select the first row since you aren't passing a selector into the row() method.

    Use:

    var rowid = table.row( $(this).closest('tr') ).id();
    

    Allan

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0

    Still undefined so I assume the selector for the click event is wrong.

    $('#grid tbody tr td .fa-times').on('click', function () {
    var rowid = table.row( $(this).closest('tr'String) ).id();
    alert(rowed);
    }

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    That looks like it should be okay to me. In fairness, I would suggest a delegated event:

    $('#grid tbody').on('click', 'tr td .fa-times', function () {
      var rowid = table.row( $(this).closest('tr') ).id();
      alert(rowed);
    }
    

    I'd need a link to a page showing the issue to be able to offer any help before that though.

    Allan

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0

    ok so I found something I had done to cause your suggestion not to work so it is now getting the id but the following does not work I get undefined

    var data = table.row($(this).closest('tr')).data();
    alert(data[0]);

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    That suggests that your data variable is not an array, but rather an object.

    Allan

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0

    ok so what are the properties of the data object

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0

    I can do this Object.keys(obj) and get the data names which I can use as data['name'] but why can't I get Object.values(obj)?

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0
    edited March 2017

    Thanks by the way for your responses

  • allanallan Posts: 63,873Questions: 1Answers: 10,528 Site admin

    ok so what are the properties of the data object

    No idea! The object is whatever you are feeding into DataTables - it is exactly the same object, so it entirely depends upon your configuration.

    If you have a name property in the object, then you can do data.name (or data['name'] if you prefer that syntax).

    why can't I get Object.values(obj)?

    Does the browser you are using support Object.values()? Are any messages shown on the console when you use it?

    Allan

  • dtw11dtw11 Posts: 18Questions: 4Answers: 0

    I resolved with a loop through the Object.keys(obj) and used the key as a obj[key].

This discussion has been closed.