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());
}
21 Answers, 1 is accepted
.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
>);
})
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.
<
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
>
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.
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.
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!
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();
}
}​
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.
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
Indeed, the current implementation does not allow persisting the toolbar, hence the workaround we suggested.
Regards,
Alexander Popov
Telerik
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
We're not aware of such behavior. Would you please share more details about your setup?
Regards,
Alexander Popov
Telerik by Progress
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.
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
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.
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
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