Reputation: 97
I try to use datatables in my view (see Datatables)
My problem is that I want to select a specific row that contains a value. For example if there is a table including one column and two rows and the forst cell includes "first" and the second "second", I want to iterate and if I find the value "second" I want to select this row.
What I tried is:
table.column(0).data().each(function (value, index) {
if (value == 'second') {
table.row(index).addClass('selected');
}
});
and
table.rows().indexes().each(function (i) {
var r = table.row(i);
if (r.data().category == 'second') {
r.node().to$().addClass("active");
}
});
and
table.rows().each(function (i) {
var r = table.row(i);
if ($(this).text() == 'second') {
alert('test');
r.node().to$().addClass("active");
}
});
all of them with no results, nothing happens.
Do you have any idea how I can do this? I would be very thankful for any suggestion.
Upvotes: 1
Views: 8394
Reputation: 21
Maybe you are trying to add a class only on the specific row. You can try my code below:
rowCallback: function (row, data) {
if (something == true) {
table.row(row).select()
}
},
rowCallback function allows you to 'post process' each row after it have been generated for each table draw, but before it is rendered into the document. This means that the contents of the row might not have dimensions ($().width() for example) if it is not already in the document.(1)
Upvotes: 2
Reputation: 1352
Add following jquery script :
Here '#example' is id of datatable.
$('#example td').each(function() {
var cellText = $(this).html();
if(cellText == 'second'){
$(this).closest('tr').addClass("selected");
}
});
Upvotes: 3