在 JQGrid 中,除了列格式化程序之外,是否可以在对汇总单元格进行分组时使用不同的格式化程序? [英] In JQGrid, Is it possible to use different formatter on grouping summary cell other than column formatter?

查看:9
本文介绍了在 JQGrid 中,除了列格式化程序之外,是否可以在对汇总单元格进行分组时使用不同的格式化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对数据行和摘要行使用不同的格式化程序?例如,我想将摘要信息(summaryType = count)添加到复选框格式的列中,摘要值显示为选中的复选框.有什么想法吗?

种类,阿尔珀

你可以从,它显示以下结果:

如何查看摘要行有许多以不同方式格式化的元素:

要在每个分组块的末尾有摘要行,我们需要在 jqGrid 的 groupingView 参数中定义 groupSummary: [true] 属性.然后我们需要为所有列定义 summaryType 属性汇总行没有空单元格的 colModel.

例如,在最简单的情况下,我为列 'amount' 定义了属性 summaryType: 'sum'.

对于列 'tax' 我另外定义了 summaryTpl:

summaryTpl: '<i>{0}</i>', summaryType: 'sum'

因此,'tax' 列的摘要包含斜体文本.

对于 'total' 列,我使用不同的颜色取决于显示的值.值为 grater 为 1000 的结果以绿色显示.其他值以红色显示.该实现是摘要行的真正自定义格式化程序:

//formatter: 'number',格式化程序:函数(cellval,opts,rwdat,act){if (opts.rowId === "") {if (cellval > 1000) {return '<span style="color:green">'+$.fn.fmatter('number', cellval, opts, rwdat, act) +'</span>';} 别的 {return '<span style="color:red">'+$.fn.fmatter('number', cellval, opts, rwdat, act) +'</span>';}} 别的 {return $.fn.fmatter('number', cellval, opts, rwdat, act);}},摘要类型:总和"

我使用自定义格式化程序而不是 formatter: 'number'.我不想再实现一次 formatter: 'number' ,所以我调用了关于 $.fn.fmatter('number', cellval 的预定义的 'number' 格式化程序, opts, rwdat, act).

以上代码最重要的部分是这一行

if (opts.rowId === "") {

在格式化网格单元格期间,将调用自定义格式化程序,并将 opts.rowId 初始化为行 ID.只有在格式化摘要行的情况下,opts.rowId 才会是空字符串 ("").我使用事实来实现自定义格式.

'closed' 列中,我再展示一个技巧.我使用定义为函数的 summaryType.可以使用它来进行一些自定义汇总计算,另一种作为标准类型:sum"、min"、max"、count"和avg".在演示中,我显示所有复选框的计数"和选定复选框的计数",并在摘要中显示结果.此外,摘要单元格还有一个复选框,如果该组中至少有一个复选框被选中,则该复选框被选中.包含自定义格式化程序的相应代码如下:

formatter: function (cellval, opts, rwdat, act) {if (opts.rowId === "") {return '<span style="display:inline-block;top:-2px;position:relative;">'+cellval.checkedCount + ' of ' + cellval.totalCount + '</span>&nbsp;'+$.fn.fmatter('checkbox', cellval.max, opts, rwdat, act);} 别的 {return $.fn.fmatter('checkbox', cellval, opts, rwdat, act);}},summaryType:函数(val,名称,记录){if (typeof (val) === "string") {val = {max:false,totalCount:0,checkedCount:0};}val.totalCount += 1;如果(记录[名称]){val.checkedCount += 1;val.max = 真;}返回值;}

我们需要保存我们计算的树不同的值:totalCountcheckedCountmax.上面的代码显示可以将初始字符串 val 参数更改为包含我们需要的所有信息的对象.在格式化摘要行期间,将调用自定义格式化程序,并将 cellval 初始化为我们之前创建的 val 对象.以这种方式我们可以保存任何自定义信息然后显示它.

我希望关于演示,您将能够创建您需要的任何摘要分组行.

is it possible to use different formatter for the data rows and the summary row? forexample, I want to add a summary info (summaryType= count) to a checkbox formatted column, summary value is shown as a checked checkbox. any ideas?

kinds, Alper

you can see screenshot from here:

解决方案

I found your question very interesting, because I didn't known the answer immediately. Now I found time to reread the source code of the grouping module of jqGrid and create an example which you need.

First of all I prepared the demo which display the following results:

How you can see the summary row has many elements which are formatted in the different way:

To have summary line at the end of every grouping block we need to define groupSummary: [true] property in the groupingView parameter of jqGrid. Then we need to define summaryType property for all columns in the colModel where the summary row have not empty cell.

For example, in the simplest case I defined for the column 'amount' the property summaryType: 'sum'.

For the column 'tax' I defined summaryTpl additionally:

summaryTpl: '<i>{0}</i>', summaryType: 'sum'

As the result the summary for the 'tax' column contains italic text.

For the 'total' column I used different colors depend on the displayed value. The results having the value grater as 1000 are displayed in green. Other values are displayed in red color. The implementation is real custom formatter for the summary row:

//formatter: 'number',
formatter: function (cellval, opts, rwdat, act) {
    if (opts.rowId === "") {
        if (cellval > 1000) {
            return '<span style="color:green">' +
                $.fn.fmatter('number', cellval, opts, rwdat, act) +
                '</span>';
        } else {
            return '<span style="color:red">' +
                $.fn.fmatter('number', cellval, opts, rwdat, act) +
                '</span>';
        }
    } else {
        return $.fn.fmatter('number', cellval, opts, rwdat, act);
    }
},
summaryType: 'sum'

Instead of formatter: 'number' I used custom formatter. I didn't want to implement the formatter: 'number' one more time, so I called the predefined 'number' formatter with respect of $.fn.fmatter('number', cellval, opts, rwdat, act).

The most important part of the above code is the line

if (opts.rowId === "") {

During formatting the grid cells the custom formatter will be called with the opts.rowId initialized as the row id. Only in case of formatting the summary row the opts.rowId will be the empty string (""). I use the fact to implement custom formatting.

In the column 'closed' I show one more trick. I use the summaryType defined as a function. One can use this to make some custom summary calculation another as the standard types: "sum", "min", "max", "count" and "avg". In the demo I display "count" of all and "count" of selected checkboxes and display the results in the summary. Moreover the summary cell has additionally checkbox which is checked if at least one checkbox in the group is checked. The corresponding code inclusive the custom formatter is the following:

formatter: function (cellval, opts, rwdat, act) {
    if (opts.rowId === "") {
        return '<span style="display:inline-block;top:-2px;position:relative;">' +
            cellval.checkedCount + ' of ' + cellval.totalCount + '</span>&nbsp;' +
            $.fn.fmatter('checkbox', cellval.max, opts, rwdat, act);
    } else {
        return $.fn.fmatter('checkbox', cellval, opts, rwdat, act);
    }
},
summaryType: function (val, name, record) {
    if (typeof (val) === "string") {
        val = {max: false, totalCount: 0, checkedCount: 0};
    }
    val.totalCount += 1;
    if (record[name]) {
        val.checkedCount += 1;
        val.max = true;
    }
    return val;
}

We needs to hold tree different values which we calculate: totalCount, checkedCount and max. The code above shows that one can change the initial string val parameter to the object which hold all information which we need. During formatting the summary row the custom formatter will be called with the cellval initialized to the val object which we created before. In the way we can save any custom information and then display it.

I hope with respect of the demo you will be able create any summary grouping row which you need.

这篇关于在 JQGrid 中,除了列格式化程序之外,是否可以在对汇总单元格进行分组时使用不同的格式化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆