【问题标题】:Individual column searching using DataTable , data source from java script使用数据表的单个列搜索,来自 javascript 的数据源
【发布时间】:2017-03-27 07:57:07
【问题描述】:

//我正在尝试使用 DataTable 插件创建表,我需要为表中的每一列单独过滤列, 我正在使用 javascripted 资源获取数据 https://jsfiddle.net/ImmanuelRocha/zu0h3yca/ 例如:https://datatables.net/examples/data_sources/js_array.html

var dataSet = [
  ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
  ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"]];

$(document).ready(function() {
      // Setup - add a text input to each footer cell
      $('#example tfoot th').each(function() {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
      });



      // DataTable
      var table = $('#example').DataTable({
        data: dataSet,
        columns: [{
          title: "Name"
        }, {
          title: "Position"
        }, {
          title: "Office"
        }, {
          title: "Extn."
        }, {
          title: "Start date"
        }, {
          title: "Salary"
        }]
      });

      // Apply the search
      table.columns().every(function() {
        var that = this;

        $('input', this.footer()).on('keyup change', function() {
          if (that.search() !== this.value) {
            that
              .search(this.value)
              .draw();
          }
        });
      });

【问题讨论】:

    标签: javascript jquery datatable


    【解决方案1】:

    在您的表格中添加一个&lt;tfoot&gt;&lt;/tfoot&gt; 标签,例如:

    <tfoot>
        <tr>
            <th></th>
        </tr>
    </tfoot>
    

    和 Jquery 代码:

    $(document).ready(function() {
        var data = [];
        data.push(
            [1,"Sasha","Brenna","0800 1111"],
            [2,"Sage","Mia","(01012) 405872"],
            [3,"Chelsea","Allistair","076 0578 0265"],
            [4,"Uta","Savannah","070 1247 5884"],
            [5,"Remedios","Berk","0995 181 0007"],
            [6,"Ruby","Wayne","0800 1111"],
            [7,"Faith","Fredericka","(01709) 099879"],
            [8,"Myra","Harrison","070 4241 9576"],
            [9,"Drake","Miriam","0800 733635"],
            [10,"Reed","Shannon","076 8597 5067"]
        );
    
        var count = 0;  // To manage the column count for which filter is required
        $('#data_table').DataTable( {
            data:           data,
    
            initComplete: function (){
                this.api().columns().every( function () {
                    if(count == 2)
                    {
                        var column = this;
                        var select = $('<select><option value=""></option></select>')
                            .appendTo( $(column.footer()).empty() )
                            .on( 'change', function () {
                                var val = $.fn.dataTable.util.escapeRegex(
                                    $(this).val()
                                );
    
                                column
                                    .search( val ? '^'+val+'$' : '', true, false )
                                    .draw();
                            });
    
                        column.data().unique().sort().each( function ( d, j ) {
                            select.append( '<option value="'+d+'">'+d+'</option>' )
                        });
                    }
    
                    count++;
                });
            }
        });
    });
    

    Working Fiddle

    Working Fiddle for all column filter

    【讨论】:

    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2014-10-01
    相关资源
    最近更新 更多