简体   繁体   English

如何删除JQuery数据表中的列?

[英]How to remove columns in JQuery Datatables?

I want to remove the columns which have total = 0. 我想删除总计为0的列。

So I've tried in different ways. 所以我尝试了不同的方式。

First, I assigned ID to all columns, for example; 首先,例如,我为所有列分配了ID; every <td> is of column will have their ID eg: First columns <td ID = 'col_1'> , second column all <td ID = 'col_2'> etc. And then in when footer callback I've tried to remove if this column total is ZERO then this $("col_"+i).remove() ; 每个<td>的列将具有其ID,例如:第一列<td ID = 'col_1'> ,第二列全部<td ID = 'col_2'>等。然后在页脚回调中,我尝试删除此列的总和为零,则此$("col_"+i).remove() ; this code removed table headers only so I've tried again with $("col_"+i).empty() but again it's just empty. 这段代码仅删除了表头,因此我再次尝试使用$(“ col _” + i).empty(),但是它只是空的。 <th> only <th>

Then I've tried to hide the columns by creating dynamic but I don't get any values. 然后,我尝试通过创建动态来隐藏列,但没有任何值。

    "footerCallback": function ( row, data, start, end, display ) 
    {
        var api = this.api(), data;  
        var intVal = function ( i ) { return typeof i === 'string' ? i.replace(/[\$,]/g, '')*1 : typeof i === 'number' ?    i : 0;};

        var col_gonna_invis = '[';
        for(i=1;i<length_of_coloumns;i++)
            {
                total_salary = api.column( i ).data().reduce( function (a, b) {return intVal(a) + intVal(b);},0 );
                $('#total_cont_'+i).html(total_salary); 
                if(total_salary == 0)
                {
                    col_gonna_invis += '{"targets": [ '+i+' ], "visible": false, "searchable": false },';
                }
            }
        col_gonna_invis += ']';alert(col_gonna_invis);  
    },

    "aoColumnDefs": col_gonna_invis;

Please someone help me fix this issue or please someone tell me how to hide or remove columns which footer total is 0. 请有人帮助我解决此问题,或者请有人告诉我如何隐藏或删除页脚总数为0的列。

Thank you in advance. 先感谢您。

I will suggest you use the visible() API method along with the sum() plugin : 我将建议您将visible() API方法与sum()插件一起使用:

Enhance the API with a column().sum() method : 使用column().sum()方法增强API:

jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
    return this.flatten().reduce( function ( a, b ) {
        if ( typeof a === 'string' ) {
            a = a.replace(/[^\d.-]/g, '') * 1;
        }
        if ( typeof b === 'string' ) {
            b = b.replace(/[^\d.-]/g, '') * 1;
        }

        return a + b;
    }, 0 );
} );

now, in initComplete() you can very easy hide columns which have a total or sum() of 0 : 现在,在initComplete()您可以非常容易地隐藏totalsum()为0的列:

var table = $('#example').dataTable({
   //... 
   initComplete : function() {
      var api = this.api(), 
          colCount = api.row(0).data().length;
      for (var i=0; i<colCount; i++) {
          if (api.column(i).data().sum() == 0) {
              api.column(i).visible(false);
          }
      }     
    }
});

demo -> http://jsfiddle.net/qer7e5os/ 演示-> http://jsfiddle.net/qer7e5os/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM