DAK
DAK

Reputation: 1505

How to restore grid state during initialization?

I have Angular UI Grid where a user can change sorting order by clicking on column headers. I want to preserve user’s choice when the user leaves the controller and restore chosen sorting order when the user returns to the controller. UI Grid has saveState module so I am using it to save the state when the uses leaves the controller.

The issue is that I can’t restore this saved state. When should I call saveState.restore()? If I call it in onRegisterApi then it doesn’t work since columns aren’t constructed yet.

Upvotes: 2

Views: 2953

Answers (2)

user3389
user3389

Reputation: 489

You can try this way

$scope.gridOptions = {
        exporterMenuCsv: false,
        enableGridMenu: true,
        enableColumnResizing: true,
        enableFiltering: true,
        saveVisible: true,
        saveOrder: true,
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;
            $timeout(function () {
                $scope.restoreState();  //call my restore function 
            }, 100);
        },
        data: $scope.bookings
    };

Upvotes: 4

S. Baggy
S. Baggy

Reputation: 958

I call it just after I have filled the grid with data.

$scope.loadData = function () {
    $q.all([
            myDataLoaderFunction.$promise
    ])
        .then(function (data) {
            $scope.gridOptions.data = data[0];
            var savedState = SomeServiceThatIStoredMyStateInto.getSavedGridState();
            if (savedState) {
                $scope.gridApi.saveState.restore($scope, savedState);
            }
        }, function () {
            $scope.gridOptions.data = [];
        });
};

Upvotes: 2

Related Questions