我尝试在表中查找字符串并将类添加到该行。但这段代码不起作用。只是什么也没发生。这是我的代码,后面我调用 myTable()
函数:
function myTable() {
var selectDateVar = $('#selectDate').val();
var table = $('#example').DataTable( { // Таблица
"processing": true,
"serverSide": true,
"deferRender": true,
"bDestroy": true,
"sAjaxSource": "server_processing.php?data=30/09/2015",
"order": [[ 2, "desc" ]],
initComplete: function(){
var api = this.api();
new $.fn.dataTable.Buttons(api, {
buttons: [
{
extend: 'print',
text: 'Принтиране',
'className': 'btn-lg btn btn-warning printBTN',
},
]
});
api.buttons().container().appendTo( '.printButton' );
}
});
var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
return table.cell( rowIdx, 3 ).data() === '180' ? true : false;
} );
table.rows( indexes ).nodes().to$().addClass( 'highlight' );
}
我的 table :
最佳答案
function myTable() {
var selectDateVar = $('#selectDate').val();
var table = $('#example').DataTable( { // Таблица
"processing": true,
"serverSide": true,
"deferRender": true,
"bDestroy": true,
"sAjaxSource": "server_processing.php?data=30/09/2015",
"order": [[ 2, "desc" ]],
initComplete: function(){
var api = this.api();
new $.fn.dataTable.Buttons(api, {
buttons: [
{
extend: 'print',
text: 'Принтиране',
'className': 'btn-lg btn btn-warning printBTN',
},
]
});
api.buttons().container().appendTo( '.printButton' );
//filtering code should be inside of initComplete function
//but in your case an empty table is filtered
var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
return table.cell( rowIdx, 3 ).data() === '180' ? true : false;
} );
table.rows( indexes ).nodes().to$().addClass( 'highlight' );
}
});
}
您需要在数据加载时调用代码。目前,您在表填充服务器数据之前调用它。只需将您的代码示例添加到 initComplete 函数即可。 initComplete 将在 Ajax 数据加载后调用。
第二个问题的答案:如果您需要跨多个列进行搜索,只需添加以下代码:
var indexes = table.rows().eq( 0 ).filter( function (rowIdx) {
return table.cell( rowIdx, 3 ).data() === '180' &&
table.cell( rowIdx, 0 ).data() === '521' ? true : false;
} );
关于数据表查找字符串并添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251449/