This is a migrated thread and some comments may be shown as answers.

When is a Grid Object Ready to Use

5 Answers 1283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 26 Mar 2016, 11:04 AM

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

Sort by
0
Viktor Tachev
Telerik team
answered on 28 Mar 2016, 11:13 AM
Hello John,

Please try to use the document.ready event and see how the behavior changes.

Regards,
Viktor Tachev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
John
Top achievements
Rank 1
answered on 28 Mar 2016, 01:14 PM

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.

0
Viktor Tachev
Telerik team
answered on 29 Mar 2016, 08:35 AM
Hello John,

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
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
John
Top achievements
Rank 1
answered on 29 Mar 2016, 10:07 AM

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.

0
Viktor Tachev
Telerik team
answered on 30 Mar 2016, 08:21 AM
Hi John,

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
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
John
Top achievements
Rank 1
Share this question
or