2017-04-12 73 views
1

我正在實現一個Bootgrid表使用Ajax從Mysql表中獲取數據,一切工作正常,但現在我試圖總結最後一列的最後一列和打印結果或頁腳。有人知道我應該打電話給我哪種方法,或者我該如何解決這個問題?Bootgrid總結列和顯示結果它在頁腳

回答

0

我有過類似經歷,我能想到的是使用loaded事件處理的最佳方式,然後做手工計算,這裏有一個例子假設你兩個列QTE &價格,你想獲得總(QTE *價格):

var bootGrid = ('#grid'); 

bootGrid.bootgrid({ 
    ajax: true, 
    url: 'json' 
    ,multiSort:true 
    // other options... 
    ,labels: { 
      infos: '<h3>Total: <b><span id="totalAmount"></span></b></h3><p>Showing {{ctx.start}} to {{ctx.end}} of {{ctx.total}} entries</p>', 
    } //labels 
}).on("loaded.rs.jquery.bootgrid", function(){ 
    // dynamically find columns positions 
    var indexQte = -1; 
    var indexPrice = -1; 
    $(bootGrid).find('th').each(function(e){ 
     if ($(this).attr('data-column-id') == 'qte'){ 
      indexQte = e; 
     } else if ($(this).attr('data-column-id') == 'price'){ 
      indexPrice = e;   
     } 
    }); 
    var totalAmount = 0.0; 
    $(bootGrid).find('tbody tr').each(function() { 
     var qte = 0.0; 
     var price = 0.0; 
     // loop through rows 
     $(this).find('td').each(function(i){ 
      if (i == indexQte){ 
       qte = parseFloat($(this).text()); 
      } else if (i == indexPrice){ 
       price = parseFloat($(this).text()); 
      } 
     }); 
     totalAmount += qte * price; 
    }); 
    $('#totalAmount').text(totalAmount.toFixed(2)); 

}); 

希望這會有所幫助。