当列为null和""值时,jqGrid对JS中的列求和

jef*_*ind 3 jquery sum jqgrid

我在这篇文章中找到了一个很好的方法来在javascript中对jqGrid列进行求和,如下所示:

var total = $('#grid_id').jqGrid('getCol','col_name',false,'sum');
Run Code Online (Sandbox Code Playgroud)

这很好,但我遇到的问题是我想要求和的列有时会有空格而不是数字,并且total上例中的变量返回NaN.

有谁知道如何调整这个功能,以便白色空间只是像零一样?

谢谢!

**更新**

正如Oleg在评论中指出的那样,你需要在colModel中使用格式化程序.一旦我把它formatter: 'number'放在我的colModel中,所有的空格都以零填充,它就可以工作了!请参阅示例代码:

$("#grid_id").jqGrid({
    url:'ajax_script.php?sql=' + sql1,
    height: 275,
    shrinkToFit: true,
    width: 800,
    datatype: 'xml',
    mtype: 'POST',
    colNames:["Customer","Job", "Sched QNTY Sell","Sched Total Sell"],
    colModel:[
        {name:"Customer",index:"Customer",width:"16"},
        {name:"JobNum",index:"JobNum",width:"16"},
        {name:"Sched Qnty Sell",index:"Sched Qnty Sell",width:"20", formatter: 'number'},
        {name:"Sched Total Sell",index:"Sched Total Sell",width:"20", formatter: 'number'}
    ],
    rowNum:10000,
    sortname: 'Customer',
    sortorder: 'asc',
    viewrecords: true,
    gridview: true,
    caption: 'Scheduled to Bill',
    footerrow : true,
    altRows : true,
    gridComplete: function(){
        $(this).jqGrid('footerData','set',{'Customer':'TOTALS:'});
        var colnames = $(this).jqGrid('getGridParam','colModel');
        for (var i = 2; i < colnames.length; i ++){
            var tot = $(this).jqGrid('getCol',colnames[i]['name'],false,'sum');
            var ob = [];
            ob[colnames[i]['name']] = tot;
            $(this).jqGrid('footerData','set',ob);
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

Ser*_*giu 7

如果您不想在空列上显示这些零,则可以使用unformat选项:

{ name:"Column Name", index:"Column Index", width:"20", unformat: unformatNullColumn, formatter: 'number' }

function unformatNullColumn(cellvalue, options) {
    return (parseFloat(cellvalue) || 0);
}
Run Code Online (Sandbox Code Playgroud)

在这个答案中解释得非常好.