2015-09-04 34 views
1

我有代碼來總計一個列,但我有已經有總計的子組。除了灰色的總行數外,我可以累計列中的每個數字嗎? enter image description hereDataTables:總共一列 - 除了具有特定類的行?

var table = $('#datatable'); 
var leng = table.find("tr:first").children().length; 

    // add totals to new row 
      for (var i = 0; i < leng; i++) { 

       var total = api 
       .column(i) 
       .data() 
        .reduce(function (a, b) { 
         // return if it's not a value from the gray row 
         return intVal(a) + intVal(b); 
        }); 

       // correct any html mistakes that slip through 
       if (isNaN(intVal(total))) 
        total = ''; 

       table.find("tfoot tr:first th").eq(i).html(total); 
      }; 
+0

也許灰色的行有一個特定的類? –

+0

他們這樣做,但這看起來代碼明智嗎?這個類是'sgrouptotal' – triplethreat77

+0

使用'.not('。sgrouptotal')'排除所有具有class'sgrouptotal'的東西 –

回答

0

什麼只是在做什麼?

i = 0;   
$('#datatable tr:first tr').each(function(key, value){ 
    if(!$(this).hasClass("sgrouptotal"){ 
     i += parseInt(value.text()); 
    } 
}); 
+0

這不起作用,因爲我指的是列,其中有4個 - 不是12行。 – triplethreat77

0

爲什麼不直接使用在rows() API方法:not選擇和計算基礎上,其餘行的總和?非常小例如,山口#1的總和添加到頁腳中的回調:

var table = $('#example').DataTable({ 
    drawCallback: function() { 
     var api = this.api(), 
      sum = 0; 
     api.rows(":not('.sgrouptotal')").every(function() { 
      sum += parseFloat(this.data()[0]); 
     });  
     $(api.column(0).footer()).text(sum); 
    } 
}); 

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

上面應該很容易擴展到多個列。在相同的循環中計算col#4的總和,如sum4 += this.data()[4]等等。

相關問題