Return Row ID
Return Row ID
dtw11
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);
});
This discussion has been closed.
Answers
Just using
table.row()
is always going to select the first row since you aren't passing a selector into therow()
method.Use:
Allan
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);
}
That looks like it should be okay to me. In fairness, I would suggest a delegated event:
I'd need a link to a page showing the issue to be able to offer any help before that though.
Allan
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]);
That suggests that your
data
variable is not an array, but rather an object.Allan
ok so what are the properties of the data object
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)?
Thanks by the way for your responses
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 dodata.name
(ordata['name']
if you prefer that syntax).Does the browser you are using support
Object.values()
? Are any messages shown on the console when you use it?Allan
I resolved with a loop through the Object.keys(obj) and used the key as a obj[key].