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

前端之家收集整理的这篇文章主要介绍了javascript – 即使禁用排序,也会显示第一列的排序箭头前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用DataTable pligin在表中添加“全选”复选框.我没有为此找到标准方法,我手动使用添加.好的,但是如果我尝试使用本地化(‘语言’属性),我的“全选”复选框就会消失.我尝试修复是通过在DataTable库中添加我的代码,但这是不好的方法.
  1. <table id="devices" class="table table-striped table-bordered" cellspacing="0" width="100%">
  2. <thead>
  3. <tr>
  4. <th style="padding:8px; text-align:center;">
  5. <input type='checkBox' class='minimal check-all' id='check-all-device' name='check-all-device'></input>
  6.  
  7. </th>
  8. <th>{% trans "STATUS" %}</th>
  9. <th>{% trans "DEVICE NAME" %}</th>
  10. <th>{% trans "ACTIONS" %}</th>
  11. <th></th>
  12. </tr>
  13. </thead>
  14.  
  15. <tfoot>
  16. <tr>
  17. <th></th>
  18. <th>{% trans "STATUS" %}</th>
  19. <th>{% trans "DEVICE NAME" %}</th>
  20. <th>{% trans "ACTIONS" %}</th>
  21. <th></th>
  22. </tr>
  23. </tfoot>
  24.  
  25. <tbody id="devices-table-rows">
  26. {% for device in object_list %}
  27. {% include "device_add_row.html" %}
  28. {% endfor %}
  29. </tbody>
  30. </table>

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

  1. devicesTable = $('#devices').DataTable({
  2. // disable sorting first column
  3. 'aoColumnDefs': [{
  4. 'bSortable': false,'aTargets': [0] /* 1st one,start by the right */
  5. }],stateSave: false
  6. });
  7.  
  8. // Action's select insert in to search row
  9. $('#devices_filter').append($('#devices-actions'));
  10.  
  11. // Settings Check ALL
  12. var firstTh = $($($('#devices > thead').find('tr')[0]).find('th')[0]);
  13. firstTh.removeClass("sorting_asc");
  14.  
  15. //iCheck for checkBox and radio inputs
  16. $('input[type="checkBox"].minimal,input[type="radio"].minimal').iCheck({
  17. checkBoxClass: 'icheckBox_minimal-blue',radioClass: 'iradio_minimal-blue'
  18. });
  19.  
  20. // Check handlers All
  21. var checkAll = $('input.check-all');
  22. var checkBoxes = $('input.check-single');
  23.  
  24. checkAll.on('ifChecked ifUnchecked',function(event) {
  25. if (event.type == 'ifChecked') {
  26. checkBoxes.iCheck('check');
  27. } else {
  28. checkBoxes.iCheck('uncheck');
  29. }
  30. });
  31.  
  32. checkBoxes.on('ifChanged',function(event){
  33. if(checkBoxes.filter(':checked').length == checkBoxes.length) {
  34. checkAll.prop('checked','checked');
  35. } else {
  36. checkAll.removeProp('checked');
  37. checkAll.prop('checked',false);
  38. }
  39. checkAll.iCheck('update');
  40. });

结果 – 没事!:

使用表格本地化语言添加

  1. var languageUrl = "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json";
  2. }
  3.  
  4. devicesTable = $('#devices').DataTable({
  5. // disable sorting first column
  6. 'aoColumnDefs': [{
  7. 'bSortable': false,stateSave: false,language: {
  8. "url": languageUrl
  9. }
  10. });

我的设置已重置:

解决方法

排序

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

控制表格排序方式的order选项的默认值为[[0,’asc’]].使用此选项可设置第一列以外的初始排序顺序.

例如:

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

复选框

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

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

  1. devicesTable = $('#devices').DataTable({
  2. // disable sorting first column
  3. 'columnDefs': [{
  4. 'orderable': false,language: {
  5. "url": "https://cdn.datatables.net/plug-ins/1.10.11/i18n/Russian.json"
  6. },drawCallback: function(settings){
  7. var api = this.api();
  8.  
  9. //iCheck for checkBox and radio inputs
  10. $('input[type="checkBox"].minimal,input[type="radio"].minimal',api.table().node()).iCheck({
  11. checkBoxClass: 'icheckBox_minimal-blue',radioClass: 'iradio_minimal-blue'
  12. });
  13. }
  14. });
  15.  
  16. var table_node = devicesTable.table().node();
  17.  
  18. $('thead',table_node).on('ifChecked ifUnchecked','input.check-all',function(event) {
  19. var checkBoxes = $('tbody input.check-single',table_node);
  20.  
  21. if (event.type == 'ifChecked') {
  22. checkBoxes.iCheck('check');
  23. } else {
  24. checkBoxes.iCheck('uncheck');
  25. }
  26. });
  27.  
  28. $('tbody',table_node).on('ifChanged','input.check-single',function(event) {
  29. var checkAll = $('thead input.check-all',table_node);
  30.  
  31. var checkBoxes = $('tbody input.check-single',table_node);
  32.  
  33. if(checkBoxes.filter(':checked').length == checkBoxes.length) {
  34. checkAll.prop('checked',false);
  35. }
  36. checkAll.iCheck('update');
  37. });

猜你在找的JavaScript相关文章