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

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

问题描述

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

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:

推荐答案

我发现您的问题非常有趣,因为我不立即知道答案.现在,我有时间重新阅读jqGrid分组模块的源代码并创建您需要的示例.

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:

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

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.

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

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

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

For the column 'tax' I defined summaryTpl additionally:

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

结果,'tax'列的摘要包含斜体文本.

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

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

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'

我使用了自定义格式化程序,而不是formatter: 'number'.我不想再执行一次formatter: 'number',所以我就$.fn.fmatter('number', cellval, opts, rwdat, act)调用了预定义的数字"格式器.

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 === "") {

在格式化网格单元格期间,将使用初始化为行ID的opts.rowId调用自定义格式化程序.仅在格式化摘要行的情况下,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.

'closed'列中,我还显示了另外一个技巧.我使用定义为函数的summaryType.可以使用它来进行另一种自定义汇总计算,将其作为标准类型:求和",最小",最大",计数"和平均".在演示中,我显示所有复选框的计数"和所选复选框的计数",并在摘要中显示结果.此外,摘要单元格还具有一个复选框,如果该组中的至少一个复选框被选中,则该复选框被选中.包含自定义格式程序的相应代码如下:

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;
}

我们需要保存要计算的树的不同值:totalCountcheckedCountmax.上面的代码表明,可以将初始字符串val参数更改为包含我们需要的所有信息的对象.在格式化摘要行期间,将使用cellval初始化为我们之前创建的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天全站免登陆