I would like to persist the layout of my grid. I save the layout when it changes, - the problem is when to restore it. If I try calling restoreGridLayout in document.ready() the grid hasn't been initialized yet. If I do it in the onNotesGridDataBound-event the data disappears. I've tried calling grid.dataSource.read() manually on a buttonClick after the onNotesGridDataBound-event, which reloads the data, but if I do it in the event in question I end up in an infinite loop reading data.
Any ideas?
<script> function OnNotesGridLayoutChange(e) {
var grid = $("#notesGrid").data("kendoGrid");
var gridStoreName = getGridStoreName("notesGrid");
var name = gridStoreName + "-grid-options";
localStorage[name] = kendo.stringify(grid.getOptions());
}
function OnNotesGridDataBound(e) {
var gridStoreName = getGridStoreName("notesGrid");
var grid = $("#notesGrid").data("kendoGrid");
restoreGridLayout(grid, gridStoreName);
}
function restoreGridLayout(grid, gridStoreName) {
var name = gridStoreName + "-grid-options";
var options = localStorage[name];
if (options) {
grid.setOptions(JSON.parse(options));
}
}
</script>
@(Html.Kendo().Grid<WebClient.Models.NoteViewModel>() .Name("notesGrid") .AutoBind(false) .HtmlAttributes(new { style = "height: 1000px;" }) .Filterable() .Scrollable(s => s.Enabled(true)) .Resizable(r => r.Columns(true)) .Groupable() .Sortable() .ColumnMenu() .Pageable(pageable => pageable .PageSizes(new int[] { 2, 30, 100, 200 }) .ButtonCount(5) ) .DataSource(dataSource => dataSource .Ajax() .Read(read => read.Action("SearchForNotes", "Search")) .PageSize(20) .ServerOperation(false) ) .Events(e => e.ColumnHide("OnNotesGridLayoutChange") .ColumnResize("OnNotesGridLayoutChange") .ColumnShow("OnNotesGridLayoutChange") .ColumnReorder("OnNotesGridLayoutChange") //.DataBinding("onNotesGridDataBinding") .DataBound("OnNotesGridDataBound") .Change("OnNotesGridLayoutChange") .DataBound("OnNotesGridDataBound") ) .Selectable(s => s.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row)) )