2011-09-12 159 views
4

iv看到@Oleg的例子在jqgrid中的行总数,但我试图应用它,它不会工作我有以下网格我需要计算它的金额值。JQgrid总金额排

colNames: ['ID', 'FTE', 'Workload'], 
    colModel: [ 
       { name: 'ID', index: 'ID', width: 200, align: 'left', hidden: true }, 

       { name: 'FTEValue', index: 'FTEValue', width: 200, align: 'left', formatter: 'number' }, 
       { name: 'Workload', index: 'Workload', width: 200, align: 'left' }, 



    caption: "Activity FTE", 
    gridview: true, 
    rownumbers: true, 
    rownumWidth: 40, 
    scroll: 0, 
    rowNum: 100, 
    sortname: 'ID', 
    pager: '#pager', 
    sortorder: "asc", 
    viewrecords: true, 
    autowidth: true, 
    height: '100%', 
    footerrow: true, 
    jsonReader: { root: "GridData", page: "CurrentPage", total: "TotalPages", records: "TotalRecords", repeatitems: false, id: "0" } 
}; 



DutyFTEGrid.prototype.SetupGrid = function (selector) { 
    jQuery(selector).html('<table id="grid"></table><div id="pager"></div>'); 
    var grid = jQuery("#grid").jqGrid(this.gridConfiguration); 

    jQuery("#grid").jqGrid('navGrid', '#pager', 
    { edit: false, add: false, search: false }, {}, {}, 
    { // Delete parameters 
     ajaxDelOptions: { contentType: "application/json" }, 
     mtype: "DELETE", 
     serializeDelData: function() { 
      return ""; 
     }, 
     onclickSubmit: function (params, postdata) { 
      params.url = serviceURL + 'DutyFTE(' + encodeURIComponent(postdata) + ')/'; 
     } 
    }); 

    var grid = $("#grid"); 
    var sum = grid.jqGrid('getCol', 'FTE', false, 'sum'); 
    grid.jqGrid('footerData', 'set', { DriverEn: 'Total FTE:', FTEValue: sum }); 
}; 

奥列格你的帮助,我已经试过你的例子,但它没有工作出于某种原因。

回答

17

如果我理解你纠正你想要在页脚放置getColfooterData方法:

var grid = $("#list"), 
    sum = grid.jqGrid('getCol', 'amount', false, 'sum'); 

grid.jqGrid('footerData','set', {ID: 'Total:', amount: sum}); 

getCol可用于从“量”柱,并且尊重计算所有数字的总和footerData您可以在'ID'列的下方放置文本“Total:”并在'amount'列的底部。

已更新:可能您遇到问题,因为您将代码放置在错误的位置。代码最安全的地方是loadComplete事件处理程序。看看the demo

+0

谢谢你的男人,我累了,但我不知道为什么总是结果0! – Madi

+0

我错误与col名称,无论如何,我把正确的一个现在和那里没有任何值出现,请注意:单元格内的值是浮动像0.3 – Madi

+0

@Madi:如果你想计算的数据为浮动,你应该在列中定义float格式化程序。只需在'金额'栏中添加'formatter:'number''即可。如果不能帮助你应该用完整的jqGrid定义和测试数据附加你的问题。 – Oleg

0

总价格列:

//Count total for a price column 
var total = 0; 
$('#table tr').each(function(){ 

    //cells that contains the price 
    var tdprice = $(this).find("td:eq(2)").html(); 

    //Sum it up! 
    if (isNaN(tdprice)){ total += parseInt(tdprice); } 
}); 

alert(total + "$");