使用KendoGrid和AngularJS运行列总和

时间:2016-10-19 14:58:35

标签: angularjs gridview datagridview kendo-ui kendo-grid

我会直接用例子开始:

enter image description here

正如您所见,左侧列包含右侧列的运行总和。

例如:

  1. 9,435 + 956 = 10,391
  2. 9,435 + 956 + 147 = 10,538
  3. 9,435 + 956 + 147 + 694 = 11,232
  4. 我熟悉kendo-grid的基础知识,但这个任务现在就在我的上方。我不知道从哪里开始。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我不知道您是否想要存储该值,但如果只是出于视觉目的,列的模板就足够了。

在网格列的定义中,您应该定义"运行总和"像这样的专栏:

{  
    "_id": {
        "Year": 2016,
        "locationID": " WL 001",
        "Day": 25,
        "Hour": 12,
        "Month": 1
    }, 
    "Readings": {
        "pH": { "value": 8.879 },
        "temperature": { "value": 16.81 },
        "Conductivity": { "value": 1084 }
    }
}

我无法测试它,但它应该是这样的,并且尽管你的数据源中没有成千上万的数据项,但是你的性能不应该被这个简单的函数所破坏。

但是,我会考虑在Grid的模型中添加一个新属性,只读取前一个元素的属性" runningSum"(或其他任何名称):

$scope.mainGridOptions = {
    ...
    columns: [
        { 
            title: "Running Sum", 
            template: function (dataItem) {
                //This function is applied for each dataitem in your datasource
                var dataSource=dataItem.parent();//Get the full dataSource
                var index=dataSource.indexOf(dataItem);//Get the index of the dataItem in the dataSource
                if(index>0){
                    //Iterate to get the total
                    for(var i=0,total=0;i<=index;i++){
                        total+=dataSource[i].Price;
                    }
                    return total;
                }
                else return dataItem.Price;
            }
        },
       ...
    ],
    ...
};

我假设这种方法有很多东西,比如你的网格指令有一个k-options =&#34; mainGridOptions&#34;你在里面放了所有的配置,但作为草稿,我希望它对你有用。