|
| 1 | +--- |
| 2 | +title: Accessing External Variables in Kendo UI Grid Footer Template |
| 3 | +description: Learn how to use variables from external methods in the footerTemplate of the Kendo UI Grid for jQuery. |
| 4 | +type: how-to |
| 5 | +page_title: Using External Method Variables in Kendo UI Grid FooterTemplate |
| 6 | +slug: how-to-use-external-variables-in-kendo-ui-grid-footer-template |
| 7 | +tags: kendo-ui, grid, footer-template, external-variables, jquery |
| 8 | +res_type: kb |
| 9 | +ticketid: 1661610 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Product | Version | |
| 15 | +| --- | --- | |
| 16 | +| Kendo UI for jQuery Grid | 2024.1.319 | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I am employing the Kendo UI Grid for jQuery to display data and need to utilize a variable from an external method in the Grid footer. The challenge involves integrating a variable returned from a method outside the grid into the footerTemplate for dynamic calculations and display. |
| 21 | + |
| 22 | +This KB article also answers the following questions: |
| 23 | +- How can I use an external variable in a Kendo Grid footerTemplate? |
| 24 | +- What is the method to include dynamic content in the footer of a Kendo UI Grid? |
| 25 | +- Can I access a variable from outside the Kendo Grid in its footerTemplate? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To use a variable from an external method in the [`footerTemplate`](/api/javascript/ui/grid/configuration/columns.footertemplate) of a Kendo UI Grid, you need to ensure that the variable is accessible in the scope where the Grid is defined. You can then reference this variable directly within the `footerTemplate`. |
| 30 | + |
| 31 | +1. **Ensure global or higher scope availability**: The variable you wish to use must be accessible in the scope where the Grid is initialized. If it is returned from an external method, store it in a variable that's accessible in the Grid's scope. |
| 32 | + |
| 33 | +2. **Reference the variable in `footerTemplate`**: Directly use the variable within the `footerTemplate` definition. Since the `footerTemplate` is a function, it can access any variables in its closure. |
| 34 | + |
| 35 | +Below is an example: |
| 36 | + |
| 37 | +```dojo |
| 38 | + <div id="grid"></div> |
| 39 | + <script> |
| 40 | + let additional = 'Some additional text' |
| 41 | + |
| 42 | + function customCalculation(dataItem){ |
| 43 | + return dataItem.min * 5 |
| 44 | + } |
| 45 | + |
| 46 | + $("#grid").kendoGrid({ |
| 47 | + columns: [ |
| 48 | + { field: "name" }, |
| 49 | + { field: "age", |
| 50 | + footerTemplate: function({age}) { |
| 51 | + return 'Min: ' + age.min + ' Max: ' + age.max + '---> ' + additional + ' -- custom: ' + customCalculation(age) |
| 52 | + }, |
| 53 | + } |
| 54 | + ], |
| 55 | + dataSource: { |
| 56 | + data: [ |
| 57 | + { name: "Jane Doe", age: 30 }, |
| 58 | + { name: "John Doe", age: 33 } |
| 59 | + ], |
| 60 | + aggregate: [ |
| 61 | + { field: "age", aggregate: "min" }, |
| 62 | + { field: "age", aggregate: "max" } |
| 63 | + ] |
| 64 | + } |
| 65 | + }); |
| 66 | + </script> |
| 67 | +``` |
| 68 | + |
| 69 | +## Notes |
| 70 | + |
| 71 | +- Ensure the external method and the variable storing its result are defined before initializing the Kendo UI Grid to avoid reference errors. |
| 72 | + |
| 73 | + |
| 74 | +## See Also |
| 75 | + |
| 76 | +- [Official Documentation for Kendo UI Grid](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid) |
| 77 | +- [Kendo UI Grid footerTemplate Documentation](https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/configuration/columns.footertemplate) |
0 commit comments