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

Javascript 即使禁用排序,也会为第一列显示排序箭头,javascript,jquery,html,datatables,icheck,Javascript,Jquery,Html,Datatables,Icheck,我需要使用DataTable pligin在表中添加“全选”复选框。我找不到用于此的标准方法,为此我使用手动添加。都可以,但如果我尝试使用本地化(“语言”属性),我的“全部选择”复选框将消失。我尝试通过在DataTable库中添加代码来修复,但这是一种糟糕的方法 <table id="devices" class="table table-striped table-bordered" cellspacing="0" width="100%"> <thead>

我需要使用DataTable pligin在表中添加“全选”复选框。我找不到用于此的标准方法,为此我使用手动添加。都可以,但如果我尝试使用本地化(“语言”属性),我的“全部选择”复选框将消失。我尝试通过在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>
结果-好的!:

添加使用语言进行表本地化:

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
    }
});
我的设置已重置:

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

控制表格排序方式的选项的默认值为
[[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"
    }
});
复选框 您需要初始化处理程序中的复选框并使用委托事件处理程序。否则,只有第一页上的复选框才起作用

请注意,我只是复制了与iCheck插件相关的部分代码,不能保证它能正常工作。下面示例的重要部分是使用和委派事件处理程序

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');
});

这是什么意思?订单:[[2,'asc'],可能是?订单:[2,'asc'],@StanZeez,对不起,它应该是
[[2,'asc']]
,我已经更正了代码。谢谢,这是从第一列删除排序,但不能解决我的问题-如果我使用“语言”,这是在初始化后重置表头:删除iCheck样式和my.on('ifChanged'))处理程序。@StanZeez,通过复选框添加了问题的解决方案。
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');
});