Hello.
I'm having a problem implementing the following scenario:
After the grid is loaded (binding to local data), in some cases, I need to apply the filters, paging, and selection from a previous state of the grid. If I do this inside the databound event, this will cause an infinte loop (as I've read and experience first-hand). How / where can I add the code from ApplyPreviousSettings (see below js code) so that it can work automatically?
E.g. of scenario in cause.
When on page "Users\Index" filter the grid (filterMode = row), change page (if necessary), and select an item.
Click on Edit (which redirects the edit page for the selected user) - at the point of leaving the "Users\Index" , I save in localstorage the grid's state (not all of it - only filter, paging and selection)
When clicking on Save or Cancel, the Index page (with the grid) will be displayed AND it is at this time that I want to "restore" the grid according to the data in the localstorage.
I've attached screenshots for the above scenario and a video.
The grid looks like this:
@(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(p => p.Id).Title("#= Id #").Hidden(true).HtmlAttributes(new { id = "#= Id #" });
columns.Bound(p => p.FirstName).Title(Resource.Firstname).Filterable(f => f.Cell(c => c.Operator("Contains").ShowOperators(false).Template("EnableAutoFiltering")));
columns.Bound(p => p.Name).Title(Resource.Lastname).Filterable(f => f.Cell(c => c.Operator("Contains").ShowOperators(false).Template("EnableAutoFiltering")));
columns.Bound(p => p.EmailAddress).Title(Resource.Email).Filterable(f => f.Cell(c => c.Operator("Contains").ShowOperators(false).Template("EnableAutoFiltering")));
columns.Bound(p => p.CompletelyEquiped).Title(Resource.CompletelyEquipped)
.ClientTemplate("<div style='text-align: center;'><input type='checkbox' value='#= CompletelyEquiped #' disabled='disabled' " +
"# if (CompletelyEquiped) { #" +
"checked='checked'" +
"# } #" +
"/> </div>").Filterable(filterable =>
filterable.Messages(m => m.IsFalse(Resource.No_UserIndex))
.Messages(m => m.IsTrue(Resource.Yes_UserIndex)));
columns.Bound(p => p.BusinessUnit).Title(Resource.BusinessUnit_UserCreate).Filterable(f => f.Cell(c => c.Operator("Contains").ShowOperators(false).Template("EnableAutoFiltering")));
columns.Command(c => c.Destroy());
})
.ToolBar(toolbar =>
{
toolbar.Template(@<text>
<div class="toolbar">
<a href="@Url.Action("Create", "User")" class="k-button k-button-icontext" id="CreateButton"><span class='k-icon k-add'></span>@Resource.Create</a>
@Html.Kendo().Button().Name("EditButton").Content(Resource.Edit).Events(e => e.Click("EditClick")).SpriteCssClass("k-icon k-edit")
</div>
</text>);
})
.HtmlAttributes(new {style = "width: 125%;"})
.Pageable(pageable => pageable.Input(true).Numeric(false))
.Sortable()
.Events(e => e.DataBound("CustomizeFilterMenu"))
.Selectable(s => s.Mode(GridSelectionMode.Single))
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model => model.Id(p => p.Id))
.PageSize(10)
.Destroy("Delete", "User")
))
Javascript:
function EnableAutoFiltering(args) {
args.element.css("width", "90%").addClass("k-textbox").keydown(function (e) {
setTimeout(function () {
$(e.target).trigger("change");
});
});
}
function EditClick() {
var grid = $('#Grid').data("kendoGrid");
var selectedItem = grid.dataItem(grid.select());
if (selectedItem == null) {
alert("Please select row!");
} else {
if (window.location.pathname.endsWith("Index")) {
window.location = "../User/Edit/" + selectedItem["Id"];
} else {
window.location = "../" + window.location.pathname.split("/")[1] + "/User/Edit/" + selectedItem["Id"];
}
}
}
function ApplyPreviousSettings() {
var gridOptions = localStorage["kendo-grid-options"];
if (gridOptions === "null" || gridOptions === undefined) {
} else {
var grid = $("#Grid").data("kendoGrid");
var state = JSON.parse(gridOptions);
grid.dataSource.filter(state.filter);
grid.pager.page(parseInt(state.page));
var row = $("#Grid").find("tbody>tr>td[id= " + state.selectedUserId + "]").parent();
grid.select(row);
}
}
function CustomizeFilterMenu(args) {
var parents = $(":radio").parent();
if (!$(parents[0]).hasClass("RadioButtonFilterStyle")) {
$(parents[0]).addClass("RadioButtonFilterStyle");
}
}
window.onbeforeunload = function (e) {
if (e.target.activeElement.id === "EditButton" || e.target.activeElement.id === "CreateButton") {
var grid = $("#Grid").data("kendoGrid");
var dataSource = grid.dataSource;
var state =
{
page: dataSource.page(),
filter: dataSource.filter(),
selectedItem : grid.dataItem(grid.select()),
selectedUserId : grid.dataItem(grid.select()).Id
};
localStorage["kendo-grid-options"] = kendo.stringify(state);
} else {
localStorage["kendo-grid-options"] = null;
}
};
$(document).ready(function () {
$("#KendoGrid").removeAttr("hidden");
kendo.ui.progress($("KendoGrid"), false);
});
Important things to note:
Under the grid (as seen in the screenshots) there is a button which does (if I click it manually) exactly what I need to do automatically (the button with text: "ceva") - the button calls the js method ApplyPreviousSettings.
Also, here is a link to a video I've recorded, that I hope will clarify even further what I want to do.
https://www.dropbox.com/s/14wb1vuz76sr3st/sampleVideo2.zip?dl=0
Thank you.
Vlad