【问题标题】:jQuery datatables select rows with specific data from datasetjQuery 数据表从数据集中选择具有特定数据的行
【发布时间】: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


【解决方案1】:

这最终对我有用:

$('#select-specific-values-button').click(function(e){
   oTable.rows( {search:'applied'} ).every(function(rowIdx, tableLoop, rowLoop){
        if(oTable.row( rowIdx ).data().specific-value == 'foo'){
             $(this.node()).addClass('selected'); 
        }
   });  
});

【讨论】:

  • 太好了,你想通了!请记住将您的答案标记为已接受,也许它可以帮助未来的访问者。
猜你喜欢
  • 2014-05-15
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多