I am using the setOptions method of the Kendo UI Grid to persist user options for columns, sorting, etc. I am storing the options in local storage of the browser.
I need to call setOptions when the view is displayed and the Grid has been created. It's a bit inconsistent, as sometimes when the grid.setOptions() is called, the grid object is undefined. How can I make sure that I don't call grid.setOptions until the grid is ready to use? Code snippet shown below.
$("#grid").kendoGrid({
});
var grid = $(_gridId).data("kendoGrid");
var options = localStorage['xxxx'];
if (options) {
grid.setOptions(JSON.parse(options));
}
5 Answers, 1 is accepted
Please try to use the document.ready event and see how the behavior changes.
Regards,
Viktor Tachev
Telerik
Hi Viktor.
document.ready is already being called elsewhere in the
code, long before I get around to creating a grid. The question I have
is as follows:
When I create a Kendo Grid using $("#grid").kendoGrid({ }); with various parameters and callbacks, how do I know when the Kendo Grid object is available to use?
After creating the creating the Grid, I execute var grid = $("#grid").data("kendoGrid"); When I do that, sometimes I get a valid Grid object and sometimes the grid variable is undefined. I believe this to be a timing issue.
You can also consider the dataBound event of the Grid. It is fired when the widget is created and bound to data from its data source.
Regards,
Viktor Tachev
Telerik
Thanks Viktor. Unfortunately, the dataBound event won't work for me. As I am using setOptions to manage user's grid settings, I had to set the autoBind property to false. Otherwise I was getting duplicate REST service calls. This was recommended by another Telerik Admin. See http://www.telerik.com/forums/how-to-prevent-grid-from-fetching-data-after-grid-setoptions . This is why I need to know when the grid is ready. Sometimes when I call setOptions the grid object is undefined.
What other way is there to consistently know that the grid has been created? I need a way around this. Right now I have users wondering why their grid settings aren't always being applied.
The dataBound event is fired after the Grid is bound to data. Thus, when it fires you can be sure that the widget has been initialized. You can add a global variable and use it as a flag. Set the variable in the dataBound handler for the Grid. Then you can check that flag before specifying the grid settings.
Also you can try to set the setting when calling read() for the dataSource.
In case the behavior persists please send us a sample dojo demo that represents your scenario.
Regards,
Viktor Tachev
Telerik