【问题标题】:jQuery DataTables two rows in head: first row column names and sorting, second row filteringjQuery DataTables 头部两行:第一行列名和排序,第二行过滤
【发布时间】:2012-04-02 21:31:46
【问题描述】:

在旧版本的 DataTables(最高 1.7.?)中,我曾经能够拥有一个具有两行列标题的表,其中排序在顶行完成,并包含列名,并且在第二行中完成了输入和选择的过滤。

<table>
   <thead>
     <tr>
         <th>Col 1</th>
         <th>Col 2</th>
         <th>Col 3</th>
     </tr>
     <tr>
        <td><input type="text" /></td>
        <td><select><option ....></select></td>
        <td><input type="text" /></td>         
     </tr>
  </thead>
  <tbody>...

对于更高的版本,包括最新的 (1.9.0),这不再有效,因为 sortable 标题被应用到第二行而不是第一行。

有没有一种方法可以在不借助其他插件的情况下使其正常工作,例如 http://code.google.com/p/jquery-datatables-column-filter/?

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    jQuery DataTables 作者 Allan Jardine pointed out 完成此任务的简单方法:

    使用 bSortCellsTop 选项,自 1.8 版起可用。

    【讨论】:

      【解决方案2】:

      我对此进行了很多搜索以寻求更好的解决方案并创建了以下代码。在这里,您可以通过在表格标题单元格中使用相应的类名称来决定组合框和搜索框。排序按钮和列名在第一行,过滤选项在第二行。

      <table id="example" width="100%">
          <thead>
              <tr>
                  <th>Sıra</th>
                  <th>SKU</th>
                  <th>Stok Adı</th>
                  <th>Ana Kategori</th>
                  <th>Alt Kategori</th>
                  <th>Stok Adedi</th>
                  <th>Alt Limit</th>
                  <th>Son Giriş Tarihi</th>
                  <th>Son Giriş Adedi</th>
                  <th>Stok Yaşı</th>
              </tr>
              <tr class="filter">
                  <th class="combo"></th>
                  <th class="combo"></th>
                  <th class="combo"></th>
                  <th class="search"></th>
                  <th class="search"></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
                  <th></th>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <td>1</td>
                  <td>S49996</td>
                  <td>A4-Tech Q Klavye Siyah Ps2 (KM720)</td>
                  <td>Ofis Elektroniği</td>
                  <td>Klavye - Mouse</td>
                  <td>25</td>
                  <td>10</td>
                  <td>10-10-2015</td>
                  <td>78</td>
                  <td>89</td>
              </tr>
          </tbody>
      </table>
      
      <script type="text/javascript">
      $(document).ready(function() {
      
          var table = $('#example').DataTable({
          "bPaginate": false,
          "bLengthChange": false,
          "bFilter": true,
          "bInfo": false,
          "bAutoWidth": false,
          "bSortCellsTop": true,
              "scrollY": "300px",
              "scrollCollapse": true,
              initComplete: function () {
                  this.api().columns().every( function () {
                      var column = this;
                      var columnIndex = this.index();
                      switch ($(".filter th:eq("+columnIndex+")").attr('class')) { 
                                          case 'combo': 
                                              var select = $('<select style="width:100%;"><option value=""></option></select>')
                                                  .appendTo( $(".filter th:eq("+columnIndex+")").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>' )
                                              });
                                              break;
                                          case 'search': 
                                              $(".filter th:eq("+columnIndex+")").html( '<input type="text" />' );
      
                                              $( 'input', $(".filter th:eq("+columnIndex+")") ).on( 'keyup change', function () {
                                                  if ( column.search() !== this.value ) {
                                                      column
                                                          .search( this.value )
                                                          .draw();
                                                  }
                                              });
                                              break;
                      }
                  } );
              }       
        });
      });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        • 2020-10-09
        相关资源
        最近更新 更多