如何在列模板中使用Kendo网格聚合值

时间:2018-07-05 09:52:01

标签: kendo-ui kendo-grid

我正在使用剑道网格。我想在表格的第一行而不是页脚中显示列聚合值。为此,我要插入一行作为第一行,并定义列模板以显示列聚合值。但这是行不通的。如果我在页脚模板中使用聚合值,则它可以正常工作。

示例代码:

 <div id="grid"></div>
<script>
$("#grid").kendoGrid({
  columns: [ {
    field: "product",
    template: "<strong>#: product # </strong>"    
  },
           {
    field: "price",
    template: "<strong>#: price # </strong>",
    footerTemplate: "<strong>#: sum # </strong>"
  }],
  dataSource: {
    aggregate:[
      {field:"price", aggregate:"sum"}
    ],
    data:[ { product: "product1",price:30 }, { product: "product2", price:40 } ]}
});
</script>

如果我在“价格”模板中使用“总和”值,则此示例将失败。

{
    field: "price",
    template: "<strong>#: sum# </strong>"
  }

如何解决这个问题?我的方法正确吗?

1 个答案:

答案 0 :(得分:0)

在KendoGrid的dataSource属性中包括所有聚合值和字段值。 您可以尝试以下代码行:-

for (k=0 ; k<2 ; k++)
{
  for (l=0 ; l<2 ; l++)
  {
    printf("%d\n", tab_para_automate[k][l]);
  }
}

否则我们也可以使用div标签尝试

 <div id="example">
            <div id="grid"></div>
            <script>
                $(document).ready(function() {
                    $("#grid").kendoGrid({
                        dataSource: {
                            type: "odata",
                            transport: {
                                read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products"
                            },
                            schema:{
                                model: {
                                    fields: {
                                        UnitsInStock: { type: "number" },
                                        ProductName: { type: "string" },
                                        UnitPrice: { type: "number" },
                                        UnitsOnOrder: { type: "number" },
                                        UnitsInStock: { type: "number" }
                                    }
                                }
                            },
                            pageSize: 7,
                            group: {
                                     field: "UnitsInStock", aggregates: [
                                        { field: "ProductName", aggregate: "count" },
                                        { field: "UnitPrice", aggregate: "sum"},
                                        { field: "UnitsOnOrder", aggregate: "average" },
                                        { field: "UnitsInStock", aggregate: "count" }
                                     ]
                                   },

                            aggregate: [ { field: "ProductName", aggregate: "count" },
                                          { field: "UnitPrice", aggregate: "sum" },
                                          { field: "UnitsOnOrder", aggregate: "average" },
                                          { field: "UnitsInStock", aggregate: "min" },
                                          { field: "UnitsInStock", aggregate: "max" }]
                        },
                        sortable: true,
                        scrollable: false,
                        pageable: true,
                        columns: [
                            { field: "ProductName", title: "Product Name", aggregates: ["count"], footerTemplate: "Total Count: #=count#", groupFooterTemplate: "Count: #=count#" },
                            { field: "UnitPrice", title: "Unit Price", aggregates: ["sum"] },
                            { field: "UnitsOnOrder", title: "Units On Order", aggregates: ["average"], footerTemplate: "Average: #=average#",
                                groupFooterTemplate: "Average: #=average#" },
                            { field: "UnitsInStock", title: "Units In Stock", aggregates: ["min", "max", "count"], footerTemplate: "<div>Min: #= min #</div><div>Max: #= max #</div>",
                                groupHeaderTemplate: "Units In Stock: #= value # (Count: #= count#)" }
                        ]
                    });
                });
            </script>
        </div>