javascript 使用 jqgrid 上的页脚数据获取总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16142993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:25:20  来源:igfitidea点击:

getting the sum using footerdata on jqgrid

javascriptjqgridfooter

提问by StackOverflowUser

I have a column name Total.
And I'm also using the footer
value of the jqgrid.

我有一个列名 Total。
而且我也在使用
jqgrid的页脚值。

Example this is the column

示例这是列

Total
100
-98
-76
98
76

总计
100
-98
-76
98
76

how can I get the sum of the row
using the footer data.

如何
使用页脚数据获取行的总和。

here is my code.
note: if i use 'sum' it gives me 'NaN'
value.

这是我的代码。
注意:如果我使用“sum”,它会给我“NaN”
值。

var parseTotal= grid.jqGrid('getCol', 'Total', false, 'sum');
grid.jqGrid('footerData', 'set', { Total: parseTotal});

回答by Oleg

You should post more full code which reproduce the problem. I tried some options: the input data as string, the data as integers, using formatter: "integer", using no formatters and so on.

您应该发布更多完整的代码来重现问题。我尝试了一些选项:输入数据为字符串,数据为整数,使用formatter: "integer",不使用格式化程序等等。

I found no input data of no column definition which produce the described results. Look at the demo

我发现没有产生描述结果的无列定义的输入数据。看演示

enter image description here

在此处输入图片说明

which works and compare with your non working demo. I hope you will find the error in your code.

这有效并与您的非工作演示进行比较。我希望你能在你的代码中找到错误。

回答by Sharun

I am assuming that you put the above code in gridComplete function like this:

我假设您将上述代码放在 gridComplete 函数中,如下所示:

 gridComplete: function(){
            var parseTotal=  $(this).jqGrid('getCol', 'Total', false, 'sum');
             $(this).jqGrid('footerData', 'set', { Total: parseTotal});
          }

Now, the issue of returning NaN occurs when one of the cells in the column contain a null value(white space). So , to convert white spaces to value 0, use numberformatter in the colmodel for column total:

现在,当列中的一个单元格包含空值(空格)时,就会出现返回 NaN 的问题。因此,要将空格转换为值 0,请number在列的 colmodel 中使用格式化程序total

ie;

IE;

colModel:[
  ...............
    {name:"Total",index:"Total", formatter: 'number'},
   ......
],

Also ensure that the column index is correctly spelled.

还要确保列索引拼写正确。

回答by jenisysJohn

Not a jqGrid expert by any means, but shouldn't the column referenced in the 'getCol' method be the column you want to sum -- 'amount' -- instead of the column into which you want to put the sum -- 'Total'? The Nan comes from trying to sum a column that is as yet undefined.

无论如何都不是jqGrid专家,但'getCol'方法中引用的列不应该是您想要求和的列 - 'amount' - 而不是您想要将总和放入的列 - '全部的'?Nan 来自试图对尚未定义的列求和。