JqGrid sums a column in JS when the column is null and ""

I found a good way to sum the jqGrid column in javascript in this post , for example:

var total = $('#grid_id').jqGrid('getCol','col_name',false,'sum');

This is great, but the problem is that the column I want to summarize sometimes has a space instead of a number, and the variable totalin the above example returns NaN.

Does anyone know how to configure this function so that the empty space just acts as zero?

Thank!

** UPDATE **

as Oleg pointed out in the comments, you need to use the formatter in colModel. As soon as I put formatter: 'number'in my colModel, all spaces are filled with zeros and it works! See Sample Code:

$("#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);
        }
    }
})
+3
source
2

jqGrid , unformatter ( ), innerHTML HTML ( "<" "<" ). jqGrid parseFloat . .

, . , beforeProcessing.

+2

, unformat:

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

function unformatNullColumn(cellvalue, options) {
    return (parseFloat(cellvalue) || 0);
}

.

+7

All Articles