brammekuhh brammekuhh - 12 days ago 9
AngularJS Question

Kendo UI Grid Persist state in AngularJS

I have some problems loading the saved state of the grid in Angular.

This is the grids HTML:

<div id="grid" kendo-grid k-options="GridOptions" k-ng-delay="GridOptions"></div>


Later I start my Http call and the $scope.GridOptions are filled in and the grid works fine.

Then I save the state of my grid this way:

$scope.GridOptionsBackup = kendo.stringify($scope.GridOptions);


This works fine and when i print the output in the console. It looks like this:


{"dataSource":{"schema":{"data":"Data"},"transport":{},"serverSorting":true,"table":null,"fields":[{"encoded":true,"field":"WidgetName","title":"Name","template":"#: data.WidgetName#"},{"encoded":true,"field":"WidgetDescription","title":"Description","template":"#: data.WidgetDescription#"}]},"columns":[{"field":"WidgetName","title":"Name","template":"#: data.WidgetName#"},{"field":"WidgetDescription","title":"Description","template":"#: data.WidgetDescription#"}],"sortable":{"mode":"multiple","allowUnsort":true},"scrollable":true}


When i try to reload the grid with the saved state, i read the JSON, parse it and reassign it to $scope.GridOptions. But this don't work:

$scope.GridOptions = JSON.parse($scope.GridOptionsBackup);


Why is the grid not updated after this line of code?

I really appreciate any help you can provide!

Answer

I found the answer:

I had to give the kendo-grid a name:

<div kendo-grid="GridBram" k-options="GridOptions" k-ng-delay="GridOptions"></div>

In my Angular code, the name is automatically binded to a scope. There i can use the same (strange) get and setOptions methods that are used in jQuery. I also used a var to store the JSON.

This is my code:

var savedState = null;

$scope.saveO = function () {
    savedState = kendo.stringify($scope.GridBram.getOptions());
    console.log(test);
}

$scope.loadO = function () {
    $scope.GridBram.setOptions(JSON.parse(savedState));
}

Like this, u can save and load the state of your grid in Angular!