Kendo Grid计算两个行值之间的差异

时间:2018-01-22 13:55:56

标签: kendo-ui kendo-grid

我正在尝试获取第一行和第二行的行项值并计算差异并将其显示为自定义聚合。这应该在每次排序后发生,因此它应检查是否只显示两行。例如,如果2017年的营业额为10000欧元,2018年的营业额为5000欧元,那么结果应为-50%。

我的代码:

$(document).ready(function() {
            $("#grid").kendoGrid({                              
            toolbar: ["excel"],
            excel: {
                fileName: "turnover.xlsx",
                allPages: true
            },  
            dataSource: {
                transport: {
                    read: "data/turnovers.php",
                    dataType: "jsonp"
                },                  
                schema: {
                    data: "data",                       
                    total: function(result) { 
                    var data = this.data(result);
                    return data ? data.length : 0;
                    },                      
                    model: {
                                fields: {
                                    January: { type: "number" },
                                    February: { type: "number" },
                                    March: { type: "number" },
                                    April: { type: "number" },
                                    May: { type: "number" },
                                    June: { type: "number" },
                                    July: { type: "number" },
                                    August: { type: "number" },
                                    September: { type: "number" },
                                    October: { type: "number" },
                                    November: { type: "number" },
                                    December: { type: "number" },
                                    sum: { type: "number" },
                                    jahr: { type: "string" }                                        
                                }
                    }

                },
                pageSize: 50,
                aggregate: [ { field: "January", aggregate: "sum" },
                             { field: "February", aggregate: "sum" },
                             { field: "March", aggregate: "sum" },
                             { field: "April", aggregate: "sum" },
                             { field: "May", aggregate: "sum" },
                             { field: "June", aggregate: "sum" },
                             { field: "July", aggregate: "sum" },
                             { field: "August", aggregate: "sum" },
                             { field: "September", aggregate: "sum" },
                             { field: "October", aggregate: "sum" },
                             { field: "November", aggregate: "sum" },
                             { field: "December", aggregate: "sum" },
                             { field: "sum", aggregate: "sum" }

                             ]
            },
             height: 870,                
             scrollable: true,
             sortable: true,
             filterable: true,
             pageable: {
                        refresh: true,
                        pageSizes: true
                    },
            columns: [
                        {
                            field: "January",
                            title: "January",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
                        },
                        {
                            field: "February",
                            title: "February",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "March",
                            title: "March",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "April",
                            title: "April",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
                        },
                        {
                            field: "May",
                            title: "May",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "June",
                            title: "June",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "July",
                            title: "July",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
                        },
                        {
                            field: "August",
                            title: "August",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "September",
                            title: "September",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "October",
                            title: "October",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
                        },
                        {
                            field: "November",
                            title: "November",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "December",
                            title: "December",
                            filterable: false,
                            format: "{0:c2}",
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"

                        },
                        {
                            field: "sum",
                            title: "sum",
                            format: "{0:c2}",
                            filterable: false,
                            footerTemplate: "sum: #: kendo.toString(sum, '\\#\\#,\\# €') #"
                        }
                   ]
        });
    });

的方法:

function calculatediff(){

            var entityGrid = $("#grid").data("kendoGrid");       
            var data = entityGrid.dataSource.data();
            var totalNumber = data.length;

            if(totalNumber = 2) {
                var currentDataItem = data[0];
                var currentDataItem = data[1];
                row1 = currentDataItem.January;
                row2 = currentDataItem.January;
                diff = ((row1-row2)/((row1+row2)/2))*100
            }               
        }

1 个答案:

答案 0 :(得分:0)

支持数据源的aggregate函数仅 average,count,max,min,sum ,并且没有用于添加以相同方式工作的自定义函数的框架。

计算的聚合对footerTemplate可用作模板解析时可用的data对象。因此,页脚可以包含传递此对象的函数并执行自定义计算。

dojo snippet演示了这项技术。

将dataSource调整为两行,并为每列指定count和sum(支持的聚合函数)聚合。

          data: [
          { January: 20, February: 30, March: 140, sum: 190 },
          { January: 30, February: 20, March: 50, sum: 100 }
          ],
          pageSize: 50,
          aggregate: [ { field: "January", aggregate: "count" },  { field: "January", aggregate: "sum" },
                       { field: "February", aggregate: "count" }, { field: "February", aggregate: "sum" },
                       { field: "March", aggregate: "count" },    { field: "March", aggregate: "sum" },
                       { field: "sum", aggregate: "count" },      { field: "sum", aggregate: "sum" },
                     ]
          },

在列声明中,调整了所有页脚模板,以调用传递聚合数据对象和字段的自定义函数。

{
    field: "January",
    /* ... */
    footerTemplate: "#= customFooter(data,'January') #"
},
{
    field: "February",
    /* ... */
    footerTemplate: "#= customFooter(data,'February') #"

},
{
    field: "March",
    /* ... */
    footerTemplate: "#= customFooter(data,'March') #"

},
{
    field: "sum",
    /* ... */
    footerTemplate: "#= customFooter(data,'sum') #"
}

在footerTemplates中运行的代码不知道该字段,因此您必须在模板内重复字段名称。

最后,customFooter()函数使用字段聚合1)来确定是否应该计算特殊结果,以及2)作为对计算的部分贡献。差异直接从数据源计算。

function customFooter (ds_agg, field) {
  if (ds_agg[field].count == 2) {
    var ds = $("#grid").data("kendoGrid").dataSource;
    var diff = ds.view()[0][field] - ds.view()[1][field];
    var ratio = ( diff / ( ds_agg[field].sum / 2 ) ) * 100;

return "diff: " + diff + "<br />sum: " + ds_agg[field].sum + "<br /> ratio: " + ratio;
  }      
}

enter image description here