簡體   English   中英

如何刪除JQuery數據表中的列?

[英]How to remove columns in JQuery Datatables?

我想刪除總計為0的列。

所以我嘗試了不同的方式。

首先,例如,我為所有列分配了ID; 每個<td>的列將具有其ID,例如:第一列<td ID = 'col_1'> ,第二列全部<td ID = 'col_2'>等。然后在頁腳回調中,我嘗試刪除此列的總和為零,則此$("col_"+i).remove() ; 這段代碼僅刪除了表頭,因此我再次嘗試使用$(“ col _” + i).empty(),但是它只是空的。 <th>

然后,我嘗試通過創建動態來隱藏列,但沒有任何值。

    "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;

請有人幫助我解決此問題,或者請有人告訴我如何隱藏或刪除頁腳總數為0的列。

先感謝您。

我將建議您將visible() API方法與sum()插件一起使用:

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

現在,在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);
          }
      }     
    }
});

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM