1
votes

I am using kendo grid. I want to show column aggregate values in first row of the grid instead of footer. For this I am inserting a row as a first row and defining the column template to show column aggregate values. But it is not working. If I use aggregate values in footer template it is working.

Sample code:

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

If I use 'sum' value in 'price' template this sample fails.

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

How to solve this problem? Is my approach correct?

1

1 Answers

0
votes

Include all the aggregate and field value in dataSource Property of KendoGrid. U can try the following line of code :-

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

or else we cane try this also Using div tag

$("#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> #"
}
]