Reputation: 1505
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
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
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