【问题标题】:How to add checkbox in datatables for row grouping?如何在数据表中添加复选框以进行行分组?
【发布时间】:2015-09-28 16:09:24
【问题描述】:

我的代码是这样的: http://jsfiddle.net/rkovmhkp/3/

     ...

       }).rowGrouping({
                    fnOnGrouped: function(groups){
                        console.log("Groups", groups);

                        for(key in groups){
                            if(groups.hasOwnProperty(key)){
                               $(groups[key].nGroup).css('background-color', '#F99');
                            }
                        }                   
                    }
                }); 

      ...

我想在左上角添加复选框。当用户选中复选框时,系统将显示行分组数据表。当用户取消选中该复选框时,系统将显示没有行分组的数据表。谢谢

【问题讨论】:

    标签: javascript jquery html datatables


    【解决方案1】:

    解决方案

    使用下面的代码:

    $(document).ready(function () {    
       initTable(true);
    
       $('.btn-row-grouping-enable').on('click', function(){       
          if(!this.checked){ 
              removeRowGrouping('#example');
          }
    
          initTable(this.checked);          
       });
    
    });
    
    function initTable(hasRowGrouping){   
         $('#example').dataTable({
             "bDestroy": true,
             "bLengthChange": false,
             "bPaginate": false,
             "bJQueryUI": true,
             "fnCreatedRow": function (nRow, aData, iDataIndex){
                 $(nRow).css('background-color', /*oData.colour*/ '#99F');
             }
         });
    
         if(hasRowGrouping){
            $('#example').dataTable().rowGrouping({
               fnOnGrouped: function (groups) {
                  console.log("Groups", groups);
    
                  for (key in groups) {
                     if (groups.hasOwnProperty(key)) {
                         $(groups[key].nGroup).css('background-color', '#F99');
                     }
                  }
              }
            });
         }
    }
    
    // Remove rowGrouping plugin
    function removeRowGrouping(selector){
       var oSettings = jQuery(selector).dataTable().fnSettings();
    
       for (f = 0; f < oSettings.aoDrawCallback.length; f++) {
          if (oSettings.aoDrawCallback[f].sName == 'fnRowGrouping') {
               oSettings.aoDrawCallback.splice(f, 1);
               break;
          }
       }
    
       oSettings.aaSortingFixed = null;              
    }
    

    演示

    有关代码和演示,请参阅 this jsFiddle

    链接

    【讨论】:

    • 我像这样添加列过滤器:link。但是当启用行分组时,列过滤器中的标题消失了。
    • 有解决这个问题的办法吗?对不起,我在这里问了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多