【问题标题】:Sorting arrow is shown for first column even when sorting is disabled即使禁用排序,第一列也会显示排序箭头
【发布时间】:2016-04-06 08:34:24
【问题描述】:

我需要使用 DataTable 插件在表格中添加“全选”复选框。我没有找到标准方法,我为此手动使用添加。好的,但是如果我尝试使用本地化(“语言”属性),我的“全选”复选框就会消失。我尝试修复是通过在 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');
});

结果 - 好的!:

为表格本地化添加使用语言:

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

我的设置已重置:

【问题讨论】:

    标签: javascript jquery html datatables icheck


    【解决方案1】:

    排序

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

    控制表格排序方式的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');
    });
    

    【讨论】:

    • 什么意思? 顺序:[[2, 'asc'],可能是? 顺序:[2, 'asc'],
    • @StanZeez,对不起,应该是[[2, 'asc']],我已经更正了代码。
    • 谢谢,这是从第一列中删除排序,但不能解决我的问题 - 如果我使用“语言”,这是初始化后重置表头:删除 iCheck 样式和我的 .on('ifChanged ') 处理程序。
    • @StanZeez,添加了复选框问题的解决方案。
    猜你喜欢
    • 2011-02-10
    • 2023-03-31
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多