【问题标题】:Unable to get the Javascript sourced data by column filtering using data tables无法通过使用数据表的列过滤来获取 Javascript 源数据
【发布时间】:2019-11-04 09:09:52
【问题描述】:

我正在尝试通过 data table.js 将 js 源数据加载到表中

加载工作正常,但是当我引入列过滤时,该功能失败。我需要使用数据表进行列过滤的 Javascript 源数据

更多关于列过滤:https://datatables.net/extensions/fixedheader/examples/options/columnFiltering.html

更多关于js源数据: https://datatables.net/examples/data_sources/js_array

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
thead input {
        width: 100%;
}    
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">  
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.5/js/dataTables.fixedHeader.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">  
<table id="example" class="display" style="width:100%">
</table>
    
    
<script>  
$(document).ready(function() {
       
var dataSet = [
    [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
    [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],
    [ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ],
    [ "Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060" ],
    [ "Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700" ],
    [ "Brielle Williamson", "Integration Specialist", "New York", "4804", "2012/12/02", "$372,000" ],
    [ "Quinn Flynn", "Support Lead", "Edinburgh", "9497", "2013/03/03", "$342,000" ],
    [ "Charde Marshall", "Regional Director", "San Francisco", "6741", "2008/10/16", "$470,600" ],
];


        $(document).ready(function() {
            $('#example thead tr').clone(true).appendTo( '#example thead' );
            $('#example thead tr:eq(1) th').each( function (i) {
                var title = $(this).text();
                $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
         
                $( 'input', this ).on( 'keyup change', function () {
                    if ( table.column(i).search() !== this.value ) {
                        table
                            .column(i)
                            .search( this.value )
                            .draw();
                    }
                } );
            } );
         
            var table = $('#example').DataTable( {
                orderCellsTop: true,
                data: dataSet,
                fixedHeader: true,
                columns: [ { title: "Name" },
                          { title: "Position" },
                          { title: "Office" },
                          { title: "Extn." },
                          { title: "Start date" },
                          { title: "Salary" }
                        ],
            } );
        } );
});
</script>


  </body>
</html>

【问题讨论】:

    标签: javascript jquery datatable datatables


    【解决方案1】:

    由于您在初始化数据表时声明了列,因此在您尝试修改它的那一刻表头不存在。您需要克隆表头元素并在数据表对象完全初始化后进行修改。

    你可以使用datatables-settings的initComplete-property:

    $(document).ready(function() {
      
      let dataSet = [
        ["Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800"],
        ["Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750"],
        ["Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000"],
        ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060"],
        ["Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700"],
        ["Brielle Williamson", "Integration Specialist", "New York", "4804", "2012/12/02", "$372,000"],
        ["Quinn Flynn", "Support Lead", "Edinburgh", "9497", "2013/03/03", "$342,000"],
        ["Charde Marshall", "Regional Director", "San Francisco", "6741", "2008/10/16", "$470,600"],
      ];
    
      let table = $('#example').DataTable({
        orderCellsTop: true,
        data: dataSet,
        columns: [{ title: "Name" }, { title: "Position" }, { title: "Office" }, { title: "Extn." }, { title: "Start date" }, { title: "Salary" }],
        initComplete: function(settings, json) {
          $('#example thead tr').clone(false).appendTo('#example thead');
          $('#example thead tr:eq(1) th').each(function(i) {
            let title = $(this).text();
            $(this).removeClass('sorting sorting_asc sorting_desc');
            $(this).html('<input type="text" placeholder="Search ' + title + '" />');
            $('input', this).on('keyup change', function() {
              if (table.column(i).search() !== this.value) {
                table.column(i).search(this.value).draw();
              }
            });
          });
        }
      });
      
    });
    thead input {
      width: 100%;
    }
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    
    <table id="example" class="display" style="width:100%"></table>

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 2015-05-26
      • 2019-07-26
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      相关资源
      最近更新 更多