[英]DataTables: Using AJAX, count how many rows in total have a specific value in a cell
我的代碼是:
var table1 = $('#view-table').DataTable( {
fixedHeader: true,
"search": {
"smart": false
},
"columnDefs": [
{
"targets": [ 0,16 ],
"visible": false,
"bSearchable": true
},
{
"orderable": false,
"targets": [ 1,15 ]
}
],
"createdRow": function( row, data, dataIndex ) {
if ( data[16] == "yes" ) {
$(row).addClass('warning');
}
if ( data[0] == "yes" ) {
$(row).removeClass('warning');
$(row).addClass('success');
}
},
"ajax": '/getViewData',
"pageLength": 25
});
我看過jQuery如何通過不同的列值計算表中的行數,但它只帶回當時的屏幕內容。
我需要查看的是返回的所有數據的第16列中有多少行的值為'yes'
。 屏幕上不僅顯示數據。 一切。
如上所述,我嘗試過的所有示例僅在不基於AJAX的情況下有效
幾周前我也遇到了同樣的問題,我不確定這是否是最好的解決方案,但后來做到了:
var count = table1.rows(function(idx, data, node) {
return data[16] == 'yes'
}).count();
編輯:如果要在ajax結束后進行計數,則可以使用initComplete
initComplete: function(row, data, index) {
var count = table1.rows(function(idx, data, node) {
return data[16] == 'yes'
}).count();
console.log('count: ', count);
}, // initComplete()
編輯2 :添加jsfiddle
您可能應該使用ajax回調。 代替此"ajax": '/getViewData'
這樣做:
"ajax": {
"type": "GET",
"url": "/getViewData",
"dataSrc": function(json){
//Count your rows here
return json.data;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.