英文:
How to find a value in a jQuery Datatable
问题
我正在尝试在数据表的第一列中查找一个数值,并且在找到该数值时将行着色。
我尝试了这个:
$.each(table.column(0), function(key, value){
if(value==="value"){
table.row(0).css('background-color', 'red');
}
})
然后尝试了这个:
for(var i = 0; i < table.row(0).length; i++){
if(table.column(0).item(i).data() === "value"){
table.row(i).css('background-color', 'red');
}
}
但都没有成功。
英文:
I am trying to find a value in the first column on a datatable, and colored the row when this value is found.
I tried this :
$.each(table.column(0), function(key, value){
if(value==="value"){
table.row(0).css('background-color', 'red');
}
})
then this:
for(var i =0; i<table.row(0).length; i++){
if(table.column(0).item(i).data() === "value"){
table.row(i).css('background-color', 'red');
}
}
But nothing worked.
答案1
得分: 0
You can loop inside your datatable after bind using YourTable.rows().every(function(rowIdx, tableLoop, rowLoop){
and add desire class whatever needed.
Example:
var YourTable = $('#table').DataTable({}); // 先绑定你的表格。
YourTable.rows().every(function(rowIdx, tableLoop, rowLoop){
var rowData = this.data();
if(rowData.value == "value"){
$(this.node()).addClass('YourClass');
}
});
On CSS:
.YourClass{
background-color: red;
}
Note: 不要混淆 rowData.value
,它是字段名称,就像 first name
、Email
等一样。
英文:
You can loop inside your datatable after bind using YourTable.rows().every(function(rowIdx, tableLoop, rowLoop){
and add desire class whatever needed.
Example:
var YourTable = $('#table').DataTable({}); // Bind your table first.
YourTable .rows().every(function(rowIdx, tableLoop, rowLoop){
var rowData = this.data();
if(rowData.value == "value"){
$(this.node()).addClass('YourClass');
}
});
On CSS:
.YourClass{
background-color: red
}
Note: Dont confuse with rowData.value
, its field name just like first name
, Email
.........
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论