There is not necessary to access database several times to request same data, especially for the project which has a mass of users, otherwise, it will lead to much pressure of data. This post describes one situation in my real case, which I have to get the data from SQL server to display in Kendo Grid, but at same time, I have to sum up one of column and display it on the top of Grid:
First of all, in the front-end side, ‘div’ has been created with ‘id=”totalAmount”‘ for displaying the sum of a column, and another ‘div’ with ‘id=”invoiceGrid”‘ for displaying all data.
In Kendo code side, I packaged the access with database to avoid several times connection and assign datasource to a varibale:
Then, trying to use this object to combine data into Kendo Grid (‘id=”invoiceGrid”‘):
And as mentioned above, I have to sum up one column, in the returned data, I have to sum up the total amount of invoices, and display it into ‘div’ (‘id=”totalAmount”‘):
Until here, my task is done, but there are few tips must be listed:
- In Kendo Grid, we could definitely separate dataSource, and assign to variable, which could be used for many times;
- In this case, the returned data called ‘totalAmountJson’, which includes many arrays inside, but the number of arrays could not be got by busing ‘length’, it has to be used ‘.total()’ to count the number;
- And ‘totalAmoungJson.at(0)’ means the first array inside ‘totalAmountJson’ (the index is 0).