Hi, i am trying to follow this guide and rewrite it to the Razor syntax:
Preserve grid state in cookie example.
The difference from my code and the example code is that they use AutoBind: false.
I cant use that in Razor because i initialize the data into the model when the view is rendered.
Another difference is that the example uses schema: syntax, don't think i need that when using razor, Am i correct?
Preserve grid state in cookie example.
The difference from my code and the example code is that they use AutoBind: false.
I cant use that in Razor because i initialize the data into the model when the view is rendered.
Another difference is that the example uses schema: syntax, don't think i need that when using razor, Am i correct?
Here is my razor code:
01.
@model IEnumerable<
Backend.ViewModel.ViewSalon
>
02.
<
div
id
=
"salonDetail"
>
03.
04.
@(Html.Kendo().Grid(Model)
05.
.Name("Grid")
06.
.Columns(c =>
07.
{
08.
c.Bound(m => m.Id).Width(30);
09.
c.Bound(m => m.Name).Width(110).HtmlAttributes(new { @class = "customerName" });
10.
c.Bound(m => m.Telephone).Width(100);
11.
c.Bound(m => m.ContactPerson).Width(100);
12.
c.Bound(m => m.City).Width(50);
13.
c.Bound(m => m.StatusId).HtmlAttributes(new { @class = "statusid" }).Hidden();
14.
c.Bound(m => m.Kundnr).Width(50);
15.
16.
})
17.
.Scrollable()
18.
.DataSource(dataSource => dataSource
19.
.Ajax()
20.
.ServerOperation(false)
21.
)
22.
23.
.ColumnMenu()
24.
.Reorderable(reorder => reorder.Columns(true))
25.
.Resizable(resize => resize.Columns(true))
26.
.Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple))
27.
.Sortable()
28.
.Filterable()
29.
.Events(events => events.Change("onChange").DataBound("onDataBound").DataBinding("onDataBinding"))
30.
.HtmlAttributes(new { style = "height: 408px" })
31.
)
32.
</
div
>
01.
@* Loads cookie support *@
02.
<script src=
"http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.2/jquery.cookie.js"
></script>
03.
04.
<script type=
"text/javascript"
>
05.
//START: *** Save grid state ***
06.
07.
function
onDataBound(e) {
08.
var
grid = $(
this
).data(
"kendoGrid"
);
09.
var
dataSource =
this
.dataSource;
10.
11.
var
state = kendo.stringify({
12.
page: dataSource.page(),
13.
pageSize: dataSource.pageSize(),
14.
sort: dataSource.sort(),
15.
group: dataSource.group(),
16.
filter: dataSource.filter()
17.
});
18.
19.
$.cookie(
"customerState"
, state);
20.
21.
if
($.cookie(
'customerRows'
)) {
22.
$.each(JSON.parse($.cookie(
'customerRows'
)),
function
() {
23.
var
item = dataSource.get(
this
);
24.
var
row = grid.tbody.find(
'[data-uid='
+ item.uid +
']'
);
25.
row.addClass(
'k-state-selected'
);
26.
});
27.
}
28.
}
29.
30.
function
onChange(e) {
31.
var
grid = $(
this
).data(
"kendoGrid"
);
32.
var
ids = grid.select().map(
function
() {
33.
return
grid.dataItem($(
this
)).Id;
34.
}).toArray();
35.
$.cookie(
'customerRows'
, JSON.stringify(ids));
36.
}
37.
38.
function
parseFilterDates(filter, fields) {
39.
if
(filter.filters) {
40.
for
(
var
i = 0; i < filter.filters.length; i++) {
41.
parseFilterDates(filter.filters[i], fields);
42.
}
43.
}
44.
else
{
45.
if
(fields[filter.field].type ==
"date"
) {
46.
filter.value = kendo.parseDate(filter.value);
47.
}
48.
}
49.
}
50.
51.
function
onDataBinding() {
52.
53.
}
54.
55.
$(document).ready(
function
() {
56.
var
grid = $(
'#Grid'
).data(
"kendoGrid"
);
57.
58.
var
state = JSON.parse($.cookie(
"customerState"
));
59.
if
(state) {
60.
if
(state.filter) {
61.
parseFilterDates(state.filter, grid.dataSource.options.schema.model.fields);
62.
}
63.
grid.dataSource.query(state);
64.
}
65.
else
{
66.
grid.dataSource.read();
67.
}
68.
69.
// END: *** Save grid state ***
70.
</script>