Friday, September 19, 2008
by
Vladimir Enchev
|
My new example illustrates how to cache the grid data client-side based on the current grid state. To do this we need to build a state key using current page index, page size, sort expressions and filter expressions:
| function getCacheKey(tableView) {  |
|     return String.format("{0}{1}{2}{3}",  |
|         tableView.get_currentPageIndex(), tableView.get_pageSize(),  |
|             tableView.get_sortExpressions().toString(), tableView.get_filterExpressions().toDynamicLinq());  |
| }Â |
Every time when we get new data we can store the result in our client-side cache:
| function updateGrid(result) {  |
|     var stateKey = getCacheKey(tableView);  |
|     if (!cache[stateKey]) {  |
| Â Â Â Â Â Â Â Â cache[stateKey]Â =Â result;Â Â |
| Â Â Â Â }Â Â |
| Â |
| Â Â Â Â tableView.set_dataSource(result);Â Â |
| Â Â Â Â tableView.dataBind();Â Â |
| }Â Â |
and when the next grid command occur we can call explicitly updateGrid() method if we have already saved result for the current grid state:
| function RadGrid1_Command(sender, args) {  |
| Â Â Â Â args.set_cancel(true);Â Â |
| Â |
|     commandName = args.get_commandName();  |
| Â |
|     var stateKey = getCacheKey(tableView);  |
|     if (cache[stateKey]) {  |
| Â Â Â Â Â Â Â Â updateGrid(cache[stateKey]);Â Â |
| Â Â Â Â Â Â Â Â return;Â Â |
| Â Â Â Â }Â Â |
| Â |
|     WebService.GetData(tableView.get_currentPageIndex() * tableView.get_pageSize(), tableView.get_pageSize(),  |
|             tableView.get_sortExpressions().toString(), tableView.get_filterExpressions().toDynamicLinq(),  |
| Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â updateGrid);Â Â |
| }Â Â |
Â
The result:
1. Click on next page button to get the second page - server is requested:
2. Click on previous page button to get the first page - server is not requested:
Enjoy!
[Download]