【发布时间】:2019-01-02 18:51:57
【问题描述】:
我正在使用 jQuery DataTables 并寻找一种方法来使用按钮从整个数据集中选择包含特定值(在本例中为“foo”)的行。
这是我用来填充表格的脚本:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-html5-1.2.2/b-print-1.2.2/r-2.1.0/se-1.2.0/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-html5-1.2.2/b-print-1.2.2/r-2.1.0/se-1.2.0/datatables.min.js"></script>
var oTable = $('#table').DataTable({
'ajax': {
url: 'script-to-return-json-row-data.php',
type: "POST",
dataSrc: function ( data ) {
return data;
},
'columns': [
{
"data": "name",
"render": function ( data, type, row ) {
return data;
}
]
}
});
从script-to-return-json-row-data.php 接收的样本数据集如下所示:
[
["name":"a-name-i-want-to-select","specific-value":"foo"],
["name":"a-name-i-dont-want-to-select","specific-value":"bar"]
]
过去我可以使用下面的脚本来选择包含特定类的行
$('#select-specific-values-button').click(function(e){
oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
if($(this.node()).hasClass('class-name')){
$(this.node()).addClass('selected');
}
});
});
但是我想知道是否有办法修改上面的代码以仅选择行数据specific-value 等于foo 的行。关于如何做到这一点的任何想法?
我知道下面的代码行不通,但这应该可以很好地说明我要完成的工作:
$('#select-specific-values-button').click(function(e){
oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
// if($(this).rowIdx.data.specific-value == 'foo'){
// $(this.node()).addClass('selected');
// }
});
});
【问题讨论】:
标签: jquery datatables datatables-1.10