即使禁用排序,第一列也会显示排序箭头

Sorting arrow is shown for first column even when sorting is disabled

我需要使用 DataTable 插件在 table 中添加 "Select all" 复选框。我没有为此找到标准方法,为此我手动使用加法。好的,但是如果我尝试使用本地化 ('language' 属性),我的 "All select" 复选框就会消失。我尝试通过在 DataTable 库中添加我的代码来修复,但这是一种糟糕的方法。

  <table id="devices" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th style="padding:8px; text-align:center;">        
                <input type='checkbox' class='minimal check-all' id='check-all-device' name='check-all-device'></input>

        </th>
        <th>{% trans "STATUS" %}</th>
        <th>{% trans "DEVICE NAME" %}</th>
        <th>{% trans "ACTIONS" %}</th>
        <th></th>
      </tr>
    </thead>

    <tfoot>
        <tr>
            <th></th>
            <th>{% trans "STATUS" %}</th>
            <th>{% trans "DEVICE NAME" %}</th>
            <th>{% trans "ACTIONS" %}</th>
            <th></th>
        </tr>
    </tfoot>

    <tbody id="devices-table-rows">
      {% for device in object_list %}
        {% include "device_add_row.html" %}
      {% endfor %}
    </tbody>
  </table>

在 javascript 上添加选择处理程序:

devicesTable = $('#devices').DataTable({
    // disable sorting first column
    'aoColumnDefs': [{
        'bSortable': false,
        'aTargets': [0] /* 1st one, start by the right */
    }],
    stateSave: false
});

// Action's select insert in to search row
$('#devices_filter').append($('#devices-actions'));    

// Settings Check ALL
var firstTh = $($($('#devices > thead').find('tr')[0]).find('th')[0]);
firstTh.removeClass("sorting_asc"); 

//iCheck for checkbox and radio inputs
$('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
    checkboxClass: 'icheckbox_minimal-blue',
    radioClass: 'iradio_minimal-blue'
});

// Check handlers All
var checkAll = $('input.check-all');
var checkboxes = $('input.check-single');

checkAll.on('ifChecked ifUnchecked', function(event) {
    if (event.type == 'ifChecked') {
        checkboxes.iCheck('check');
    } else {
        checkboxes.iCheck('uncheck');
    }
});

checkboxes.on('ifChanged', function(event){
    if(checkboxes.filter(':checked').length == checkboxes.length) {
        checkAll.prop('checked', 'checked');
    } else {
        checkAll.removeProp('checked');
        checkAll.prop('checked', false);
    }
    checkAll.iCheck('update');
});

结果 - 好的!:

为 table 本地化添加使用语言:

var languageUrl = "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json";
}

devicesTable = $('#devices').DataTable({
    // disable sorting first column
    'aoColumnDefs': [{
        'bSortable': false,
        'aTargets': [0] /* 1st one, start by the right */
    }],
    stateSave: false,
    language: {
        "url": languageUrl
    }
});

我的设置已重置:

正在排序

选项orderable 仅控制最终用户对列进行排序的能力。这不会阻止以编程方式对列进行排序。

控制 table 排序方式的 order 选项的默认值为 [[0, 'asc']]。使用此选项设置除第一列之外的初始排序顺序。

例如:

devicesTable = $('#devices').DataTable({
    // disable sorting first column
    'columnDefs': [{
        'orderable': false,
        'targets': 0 /* 1st one, start by the right */
    }],
    order: [[2, 'asc']], 
    stateSave: false,
    language: {
        "url": "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json"
    }
});

复选框

您需要在 drawCallback 处理程序中初始化复选框并使用委托事件处理程序。否则只有第一页上的复选框才有效。

请注意,我只是复制了您与 iCheck 插件相关的部分代码,并不能保证它一定有效。以下示例的重要部分是使用 drawCallback 和委托事件处理程序。

devicesTable = $('#devices').DataTable({
    // disable sorting first column
    'columnDefs': [{
        'orderable': false,
        'targets': 0 /* 1st one, start by the right */
    }],
    order: [[2, 'asc']], 
    stateSave: false,
    language: {
        "url": "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json"
    },
    drawCallback: function(settings){
       var api = this.api();

      //iCheck for checkbox and radio inputs
      $('input[type="checkbox"].minimal, input[type="radio"].minimal', api.table().node()).iCheck({
         checkboxClass: 'icheckbox_minimal-blue',
         radioClass: 'iradio_minimal-blue'
      });
    }
});

var table_node = devicesTable.table().node();

$('thead', table_node).on('ifChecked ifUnchecked', 'input.check-all', function(event) {
    var checkboxes = $('tbody input.check-single', table_node);

    if (event.type == 'ifChecked') {
        checkboxes.iCheck('check');
    } else {
        checkboxes.iCheck('uncheck');
    }
});

$('tbody', table_node).on('ifChanged', 'input.check-single', function(event) {
    var checkAll = $('thead input.check-all', table_node);

    var checkboxes = $('tbody input.check-single', table_node);

    if(checkboxes.filter(':checked').length == checkboxes.length) {
        checkAll.prop('checked', 'checked');
    } else {
        checkAll.removeProp('checked');
        checkAll.prop('checked', false);
    }
    checkAll.iCheck('update');
});