Persist state issues

22 posts, 3 answers
  1. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 05 Jan 2015 Link to this post

    Hi, after loading state for a grid, the toolbar disappears.  Also, the Scrollable.Height property is no longer recognized and grid auto-sizes.  Basically, I'm trying to save the user's grid state on every dataBound event.  Then on initial load of page, the user's grid state is loaded.  Any ideas?

    function onDataBound(arg)
    {
        var grid = $("#UserAdministrationUserGrid").data("kendoGrid");
     
        if (initialLoad) {
            var options = localStorage["kendo-grid-options"];
            if (options) {
                grid.setOptions(JSON.parse(options));
            }
     
            initialLoad = false;
        }
     
        localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions());
    }

  2. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 05 Jan 2015 in reply to Bob Link to this post

    Note: I am using the server widgets
  3. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 05 Jan 2015 in reply to Bob Link to this post

    Including toolbar template I have in case that will assist:

    .ToolBar(toolbar =>
    {
        toolbar.Template(@<text>
            <button class="k-button k-button-icontext createNewUserButton"><span class="k-icon k-i-plus"></span>Create New User</button>
            <button class="k-button k-button-icontext kendoGridExportCustomButton"><span class="k-icon k-i-excel"></span>Export to Excel</button>
        </text>);
    })
  4. Answer
    Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 07 Jan 2015 Link to this post

    Hi Bob,

    This happens because the Toolbar is rendered on the sever-side, thus it is absent from the Grid's options (see documentation). As a result, it cannot be rendered again when restoring saved options. I would suggest checking the workaround provided in this example:

    Regards,
    Alexander Popov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 07 Jan 2015 in reply to Alexander Popov Link to this post

    Thanks!  I already went a different route that seems to be working fine.  I've included relevant JavaScript below in case anyone is interested.  In my scenario, I'm auto-saving state whenever grid is databound, then loading state on initial load of page.

    <script type="text/javascript">
        var localStorageKey = "UserAdministrationUserGridOptions";
     
        $(function () {
            // pull client grid state and apply to grid (filters, current page, sorts, etc).
            setGridOptions();
            // toolbar is cleared after pulling grid options, so we must run after grid options are set
            intializeGridToolbars();
        });
     
        function onDataBound(arg)
        {
            var grid = $("#UserAdministrationUserGrid").data("kendoGrid");
            localStorage[localStorageKey] = kendo.stringify(grid.getOptions());
        }
     
        function setGridOptions()
        {
            var options = localStorage[localStorageKey];
     
            if (options) {
                $("#UserAdministrationUserGrid").data("kendoGrid").setOptions(JSON.parse(options));
            }
        }
     
        function intializeGridToolbars()
        {
            var html = '<div class="k-header k-grid-toolbar k-grid-top"><button class="k-button k-button-icontext createNewUserButton"><span class="k-icon k-i-plus"></span>Create New User</button><button class="k-button k-button-icontext kendoGridExportCustomButton"><span class="k-icon k-i-excel"></span>Export to Excel</button><div>';
            $(html).insertBefore("#UserAdministrationUserGrid .k-grid-header");
        }
    </script>
  6. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 11 Mar 2015 Link to this post

    Hi, I'm now having an issue where the header template for a column is not persisted.  Is that a known issue? If so, is there a hack to address?
  7. Answer
    Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 13 Mar 2015 Link to this post

    Hi Bob,

    The header template is also rendered on the server, hence it is not persisted for the same reasons and the same workaround could be used. For example: 
    @helper HeaderTemplate() {
        <span>My Template</span>
    }
    <script type="text/x-kendo-template" id="headerTemplate">   
       @Html.Raw(@HeaderTemplate().ToHtmlString().Replace("#", "\\#").Replace("</scr", "<\\/scr"))
    </script>
     
    @(Html.Kendo().Grid<GridPerserveToolbarServerTemplate.Models.Order>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.OrderID).HeaderTemplate(@<text>@HeaderTemplate()</text>);
    ....
    parsedOptions.columns[0].headerTemplate = $("#headerTemplate").html();


    Regards,
    Alexander Popov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Bob
    Bob avatar
    123 posts
    Member since:
    Sep 2012

    Posted 13 Mar 2015 in reply to Alexander Popov Link to this post

    Alexander, thanks for reply.  I don't believe it would be safe to go by column index if the grid allows column re-ordering though.  The solution I have at the moment goes on field name
  9. Answer
    Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 13 Mar 2015 Link to this post

    You are absolutely right Bob!

    The code I shared was just a proof of concept - using the field name is a much safer approach.

    Regards,
    Alexander Popov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  10. Fillip
    Fillip avatar
    10 posts
    Member since:
    Nov 2013

    Posted 09 Jun 2015 Link to this post

    For anyone looking for a solution in the future, I have a fiew pieces of advice:

     

    You can get the basics of the toolbar from the `KendoGrid.options.toolbar.command` property. Though, from there, you will be missing hrefs (for some reason). So I then looped through each of those commands, found the element based on the `name` property, got the HTML as a string, and then appended a `template` property to each `command` object. Here's my code:

     

    $Grid.grid.options.toolbar.command.forEach(function(item, idx) {
           var buttonHtml = $('<div>').append($(".k-grid-" + item.name).clone()).html();
           $Grid.grid.options.toolbar.command[idx].template = function() { return buttonHtml; };
    });

     

    Hope it helps!

  11. Tony
    Tony avatar
    7 posts
    Member since:
    Apr 2015

    Posted 30 Jul 2015 in reply to Fillip Link to this post

    I took a simpler approach imho. I abandoned the grid toolbar complete and put a toolbar widget above the grid. the gotcha is when you use the setOptions, the function inserts an empty toolbar even when one did not exist before. I fixed that problem by executing the jquery remove function on the toolbar. Here's my code that I'm using which seems to work find.

        function restoreGridState(Name, grid) {
            var options = localStorage[Name];
            if (options) {
                var parsedOptions = JSON.parse(options);

                grid.setOptions(parsedOptions);

                $(".k-grid-toolbar").remove();

            }
        }​

  12. Jose M
    Jose M avatar
    1 posts
    Member since:
    Oct 2014

    Posted 12 Nov 2015 Link to this post

    Hi,

    I faced the same issue and I found this way to solve it:

                        $("#load").click(function (e) {
                            e.preventDefault();
                            var toolBar = $("#Grid .k-grid-toolbar").html();
                            
                            var options = localStorage["kendo-grid-options"];
                            if (options) {
                                grid.setOptions(JSON.parse(options));
                                $("#Grid .k-grid-toolbar").html(toolBar);
                                $("#Grid .k-grid-toolbar").addClass("k-grid-top");
                            }
                        }); 

    Regards.

  13. timmy
    timmy avatar
    41 posts
    Member since:
    Dec 2006

    Posted 21 Jan 2016 in reply to Jose M Link to this post

    Thanks Jose M. That's a nice, simple approach and worked great for me.
  14. RMC
    RMC avatar
    3 posts
    Member since:
    Mar 2011

    Posted 17 Jun 2016 Link to this post

    It's unbelievable that so far all we have to fix it, is doing a big workaround. There is no sense to change the whole grid once we just want to set any specific property.

    I'm getting the same trouble and feeling very disappointed that I should follow this EXTRA BIG CODE to prevent this behavior. 

    I'd really like to ensure there is no any other solution to keep grid toolbar intact after setOptions() !?

    Regards,
    Rubens

  15. Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 22 Jun 2016 Link to this post

    Hi Rubens,

    Indeed, the current implementation does not allow persisting the toolbar, hence the workaround we suggested.

    Regards,
    Alexander Popov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  16. Raj
    Raj avatar
    23 posts
    Member since:
    Nov 2016

    Posted 22 Dec 2016 in reply to Jose M Link to this post

    Hi Jose

    I found your solution useful to get Tool bar back on page after set Options. But the button controls in toolbar are not working(not invoking js functions) after we load the page that we persisted(get options).

    Did it work in your case?

    Thanks

    Raj

     

  17. Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 23 Dec 2016 Link to this post

    Hi Raj,

    We're not aware of such behavior. Would you please share more details about your setup?

    Regards,
    Alexander Popov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  18. Raj
    Raj avatar
    23 posts
    Member since:
    Nov 2016

    Posted 23 Dec 2016 in reply to Alexander Popov Link to this post

    After loading state for a grid, Moving to other page and coming back to same page saving shows the loaded state but the custom controls(buttons) in toolbar are not working.

     function EditFacility(e) {
            var grid = $("#FacilityGrid").data("kendoGrid");
           localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions());

    }

      $(document).ready(function () {

            var grid = $("#FacilityGrid").data("kendoGrid");
            var toolBar = $("#FacilityGrid .k-grid-toolbar").html();
            var options = localStorage["kendo-grid-options"];
            if (options) {
                grid.setOptions(JSON.parse(options));
                $("#FacilityGrid .k-grid-toolbar").html(toolBar);
                $("#FacilityGrid .k-grid-toolbar").addClass("k-grid-top");
                $('#FacilityGrid .k-grid-content').height(400);
            }
        })

     

      .ToolBar(toolbar =>
                    {
                        toolbar.Custom().Text("Add New Facility").Action("AddFacility", "Facility");
                        toolbar.Custom().Text("Clear Filters").HtmlAttributes(new { id = "clearAllGridFilters" });
                    })

     

     $("#clearAllGridFilters").click(function (e) {
            e.preventDefault();
            $("form.k-filter-menu button[type='reset']").trigger("click");
        });

     

    clearallgridfilters js function is not working after i reload the page with saving state for grid.

  19. Alexander Popov
    Admin
    Alexander Popov avatar
    1434 posts

    Posted 26 Dec 2016 Link to this post

    Hello Raj,

    In this case you might try attaching the click event handler after the Grid's ToolBar gets restored or attaching it to a parent element and filtering the event. For example: 
    $("#FacilityGrid").on("click", "#clearAllGridFilters", function() {...});

    Regards,
    Alexander Popov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  20. Curt Rabon
    Curt Rabon avatar
    69 posts
    Member since:
    Dec 2008

    Posted 25 Feb Link to this post

    Since the "jQuery" version of Kendo and the MVC Wrappers seem to be mostly in "maintenance mode" only these days, meaning bug fixes but rarely any deep enhancements, I would guess that Telerik will never make this better.  Since Kendo is the one that renders the toolbar template div onto the page, it could easily find this HTML and take care of it, but again, I seriously doubt it will ever get fixed.

    Thanks to everyone who posted options.  Thanks to Jose M., because I'm using most of your solution, which works well.

  21. Raj
    Raj avatar
    23 posts
    Member since:
    Nov 2016

    Posted 11 May in reply to Alexander Popov Link to this post

    Hi Alexander

    Grid Filter with Drop down List using filterable "UI" is not working when using Grid persist state.

    I came to know that grid.setOptions will destroy the grid and recreates grid with old values and hence dropdown is not calling the controller action method to fill the dropdown with data.

    But is there an alternative to solve this? that can bring back data to dropdown.

    Thanks

    Raj

  22. Stefan
    Admin
    Stefan avatar
    928 posts

    Posted 15 May Link to this post

    Hello Raj,

    I can suggest using a flag and on the dataBound event to determine if the event is fired from using the setOptions method.

    Then on the dataBound event to manually call the read method of the custom UI DropDown used for filtering.

    This will ensure that every time the setOptions method is used, the read action in the controller will be called.

    Regards,
    Stefan
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top