When is a Grid Object Ready to Use

6 posts, 0 answers
  1. John
    John avatar
    17 posts
    Member since:
    Jul 2014

    Posted 26 Mar 2016 Link to this post

    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));
    }

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2483 posts

    Posted 28 Mar 2016 Link to this post

    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!
     
  3. John
    John avatar
    17 posts
    Member since:
    Jul 2014

    Posted 28 Mar 2016 in reply to Viktor Tachev Link to this post

    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.

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2483 posts

    Posted 29 Mar 2016 Link to this post

    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!
     
  5. John
    John avatar
    17 posts
    Member since:
    Jul 2014

    Posted 29 Mar 2016 in reply to Viktor Tachev Link to this post

    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.

  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2483 posts

    Posted 30 Mar 2016 Link to this post

    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!
     
Back to Top