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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在列模板中使用kendo网格聚合值相关的知识,希望对你有一定的参考价值。

我正在使用kendo网格。我想在网格的第一行而不是页脚中显示列聚合值。为此,我插入一行作为第一行并定义列模板以显示列聚合值。但它没有用。如果我在页脚模板中使用聚合值它是有效的。

示例代码:

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

如果我在'price'模板中使用'sum'值,则此示例将失败。

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

如何解决这个问题呢?我的方法是否正确?

答案

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

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

或者我们也可以尝试使用div标签

$("#mygrid").kendoGrid({
dataSource: data,
columns:[
{
  field: "Column 1",
  title: "Column 1",
  footerTemplate: "#= <div>Totals A</div><div>Totals B</div> #"
},
{
  field: "Column 2",
  title: "Column 2",
  footerTemplate: "#= <div>$1000</div><div>$700</div> #"
}
]

以上是关于如何在列模板中使用kendo网格聚合值的主要内容,如果未能解决你的问题,请参考以下文章

如何强制 Kendo Grid 在列上使用数字过滤器

剑道网格日期格式

如何在Kendo UI网格中自定义导出excel

可编辑的详细信息模板 Kendo UI 网格

页脚的 Kendo 网格聚合

Kendo网格在网格中触发多个控件的数据源事件。(MVVM绑定)