I have worked with the MVC grid for many years and I am finding it hard to replicate basic functionality with the angular grid. For example the instructions for persisting grid state at this page runs through how to persist the angular grid settings in local storage. Good grief, I was prepared for a steep learning curve but I was not prepared for hundreds of lines of code where previously a few would do.
I do not want to create an interface for columnConfig and then use *ngFor to loop through a column collection. This loses my ability to use ng-template in each grid column. My use case is to save the current settings of the grid when a user selects a button which routes to the details page of a particular row in the grid. When the user then selects an option to return to the grid the grid should display the previous state with the correct row selected:
$(
"#peopleGrid"
).on(
"click"
,
".k-grid-ReDirect"
,
function
(e) {
e.preventDefault();
var
row = $(e.target).closest(
"tr"
);
var
grid = $(
"#peopleGrid"
).data(
"kendoGrid"
);
var
dataItem = grid.dataItem(row);
var
personId = dataItem.PersonId;
var
dataSource = grid.dataSource;
var
state = {
columns: grid.columns,
page: dataSource.page(),
pageSize: dataSource.pageSize(),
sort: dataSource.sort(),
filter: dataSource.filter(),
selectedRow: personId
}
localStorage[
"people-grid-options"
] = kendo.stringify(state);
window.location.href =
"@Url.Action("
Details
", "
People
")"
+
"/"
+ personId;
});
and then when the user returned to the page we pick up the previous state from local storage:
$(document).ready(
function
() {
var
grid = $(
"#peopleGrid"
).data(
"kendoGrid"
);
var
toolbar = $(
"#peopleGrid .k-grid-toolbar"
).html();
var
options = localStorage[
"people-grid-options"
];
if
(options) {
var
state = JSON.parse(options);
var
options = grid.options;
options.columns = state.columns;
options.dataSource.page = state.page;
options.dataSource.pageSize = state.pageSize;
options.dataSource.sort = state.sort;
options.dataSource.filter = state.filter;
options.dataSource.group = state.group;
grid.destroy();
$(
"#peopleGrid"
).empty().kendoGrid(options);
$(
"#peopleGrid .k-grid-toolbar"
).html(toolbar);
$(
"#peopleGrid .k-grid-toolbar"
).addClass(
"k-grid-top"
);
}
$(
"#peopleGrid"
).data(
"kendoGrid"
).dataSource.read();
});
And then, when the grid is data bound I find the relevant row and add the selected class.
function
onPeopleGridDataBound(e) {
var
grid = $(
"#peopleGrid"
).data(
"kendoGrid"
);
var
options = localStorage[
"people-grid-options"
];
if
(options) {
var
state = JSON.parse(options);
var
selectedRow = state.selectedRow;
//throws an error on first row - probably down to Filterable settings but completes ok
$.each(grid.tbody.find(
'tr'
),
function
() {
var
model = grid.dataItem(
this
);
if
(model.PersonId == selectedRow) {
$(
'[data-uid='
+ model.uid +
']'
).addClass(
'k-state-selected'
);
}
});
}
}
Now, how in the hell do I do that with the angular grid or am I simply asking for functionality which is not available?