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

问题描述 投票:1回答:1

我正在使用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>"
  }

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

kendo-ui kendo-grid
1个回答
0
投票

在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> #"
}
]
© www.soinside.com 2019 - 2024. All rights reserved.