Reputation: 2598
I have the following plunker working with save/restore and localStorage: http://plnkr.co/edit/Ad12QG1uKFEf38aOtpaL?p=preview
I can save the grid state, adjust some columns (like column resize, move, etc), and then restore back to the saved grid state just fine.
Once I've saved it though, how would I go about making a "reset" function work? I want to reset it to the default state of my grid before there were any saved changes.
My attempt, which so far is unsucessfull. I'm assuming I would try to save the default state on load before the restore occurs...
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
$scope.defaultState = $scope.gridApi.saveState.save();
...
...and then restore similar to the $scope.restoreState function...
$scope.resetState = function() {
$scope.gridApi.saveState.restore($scope, $scope.defaultState);
}
...but that's throwing errors.
Upvotes: 3
Views: 1696
Reputation: 2598
I was able to get it to work. Here's a functioning example: http://plnkr.co/edit/Ad12QG1uKFEf38aOtpaL?p=preview
I took the same approach I outlined in my question, but I had to wrap the defaultState in a $timeout() because the state isn't immediately available within onRegisterApi without that timeout. I just made sure to make it occur before the restoreState timeout.
onRegisterApi: function(gridApi) {
$scope.gridApi = gridApi;
$timeout(function() {
$scope.defaultState = $scope.gridApi.saveState.save();
}, 50);
$timeout(function() {
$scope.restoreState();
}, 100);
},
I'm not that familiar with angular yet, so I'm sure someone could improve this.
Upvotes: 1