2

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.

2 Answers 2

4

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
    };
Sign up to request clarification or add additional context in comments.

Comments

2

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 = [];
        });
};

3 Comments

The problem with this is that restoring the state can have a user's specific sorting defined - this will make another call to the server for the data based on that sorting. There needs to be a way restore the state before the data is retrieved, then the data is retrieved based on the sorting.
I have the same probem Jeremy describes here. Is there any solution to that problem?
I think you can retrieve your state data anytime and use that to setup your custom sorting. then call the restore for the grid later. I'm pretty sure it just sets up the grids view of it (sets the right directional arrows etc) You may have to put a check for the sort changed event in order not to resort the list if its the same sort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.