尽管排序顺序,但仍在jQuery DataTables的顶部保持启用的复选框



我希望我的jQuery DataTable始终在顶部显示启用的复选框行,而在下面的残疾复选框行。到目前为止,我只能通过单击第一个标头标签"点击"来实现这一目标。从本质上讲,我将"灰色灰色"的课程添加到相关的行中,然后对其进行排序。问题是,当我对其他标题进行排序时,我无法维持这两个组。当我使用列过滤器时,我还需要保留订单。例如,单击"产品组"下拉过滤器中的" long"应显示顶部的2个启用" long"行,并在底部显示1个禁用的" longs'行。恐怕我感到困惑,坦率地说,下一步。任何建议都非常感谢。小提琴:https://jsfiddle.net/mxb6vp3b/

<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.2.0/js/dataTables.select.min.js"></script>
<script src="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css"></script>
<script src="https://cdn.datatables.net/select/1.2.0/css/select.dataTables.min.css"></script>

<form id="frm-example" action="/nosuchpage" method="POST">
  <table id="example" class="display select" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>Clicks</th>
        <th>Trading Code</th>
        <th>Product Group</th>
        <th>Product description</th>
      </tr>
    </thead>
    <tbody>
      <tr id="row250200">
        <td><input id="HS250200" type="checkbox" value="HS 250200" /></td>
        <td> 250200 </td>
        <td> Longs </td>
        <td> Iron Ore - unroasted </td>
      </tr>
      <tr id="row260111">
        <td><input id="HS260111" type="checkbox" value="HS 260111" /></td>
        <td> 260111 </td>
        <td> Raw Materials - Iron ore </td>
        <td> Iron Ore - fines, concentrate, lump </td>
      </tr>
      <tr id="row730490" class="TypeCarbon_Alloy">
        <td><input id="HS730490" type="checkbox" value="HS 730490" /></td>
        <td> 730490 </td>
        <td> Pipe &amp; tube - Seamless </td>
        <td> Seamless tube - other </td>
      </tr>
      <tr id="row730512" class="TypeCarbon_Alloy">
        <td><input id="HS730512" type="checkbox" value="HS 730512" /></td>
        <td> 730512 </td>
        <td> Longs </td>
        <td> Welded tube - line pipe, LW, >406.4mm </td>
      </tr>
      <tr id="row730230" class="TypeCarbon_Alloy_Stainless">
        <td><input id="HS730230" type="checkbox" value="HS 730230" /></td>
        <td> 730230 </td>
        <td> Longs </td>
        <td> Railway Materials </td>
      </tr>
      <tr id="row721921" class="TypeStainless">
        <td><input id="HS721921" type="checkbox" value="HS 721921" /></td>
        <td> 721921 </td>
        <td> Flats - HR plate </td>
        <td> HR plate - discrete or CTL, >10mm </td>
      </tr>
    </tbody>
  </table>
</form>
.grey_out {
  color: lightgrey;
}
$(function() {
    // Disable all rows
    $('#example td input').prop("disabled", true).closest('tr').addClass('grey_out');
    // Eanable relevant rows
    $("#HS260111, #HS730512, #HS730230").prop("disabled", false).closest('tr').removeClass('grey_out');

    $('#example').DataTable({
        orderCellsTop: true,
        scrollY: '50vh',
        scrollCollapse: true,
        orderCellsTop: true,
        'columnDefs': [{
            'targets': 0,
            'orderDataType': 'dom-checkbox',
            'orderable': true,
        }],
        "order": [
            [0, "asc"]
        ],
        responsive: true,
        initComplete: function() {
            this.api().columns([1, 2, 3]).every(function() {
                var column = this;
                var select = $('<select><option value="">Show all</option></select>')
                    .appendTo($(column.header()))
                    .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>')
                });
            });
        }
    });

    // Sort for Enabled'
    $.fn.dataTable.ext.order['dom-checkbox'] = function(settings, col) {
        return this.api().column(col, {
            order: 'index'
        }).nodes().map(function(td, i) {
            return $(td).parent().is(':not(.grey_out)') ? '0' : '1';
        });
    }
});

orderFixed

[...]此选项指定的订购将始终应用于 该表,无论用户交互如何。

现在,将隐藏的列添加到您的表格,并将其用作orderFixed的基础:

var table = $('#example').DataTable({
   columnDefs: [
     { targets: 0, visible: false }
   ],
   orderFixed: [0, 'desc']
}) 

然后,在检查或未选中其同胞复选框时,将隐藏的列更新为10

$('#example').on('click', 'input[type="checkbox"]', function() {
   var row = table.row($(this).closest('tr'));
   table.cell({ row: row.index(), column: 0 } ).data( this.checked ? 1 : 0 )
   row.invalidate().draw()
})

"检查"行现在始终保持最高,无论用户如何对表进行排序。

我在上面进行了标记,并为简单起来做了一个新的演示 -> http://jsfiddle.net/6e56cu8u/

最新更新