0

I want to load the last state of a ui-grid when the controller is created. I've set up a plunkr. http://plnkr.co/edit/cOhLUUABVStfoTH6QVZd?p=preview In my controller I use a restoreOnLoad function which is called immediately at the end of the controller. I get a TypeError: Cannot read property 'saveState' of undefined because the gridApi is undefined at the time of controller creation.

restoreOnLoad = function(){
  //retrieve stateinfo. For simplicity: inline object...
  var stateInfo = {
    "columns": [
        {
            "name": "",
            "visible": true,
            "width": 50,
            "sort": {
                "direction": "asc",
                "priority": 0
            },
            "filters": [
            ]
        },
        {
            "name": "M",
            "visible": true,
            "width": 50,
            "sort": {
            },
            "filters": [
            ]
        },
        {
            "name": "Company",
            "visible": true,
            "width": 200,
            "sort": {
            },
            "filters": [
            ]
        }
    ],
    "scrollFocus": {
    },
    "selection": {
    }
  }
  $scope.gridApi.saveState.restore($scope, stateInfo);
}

restoreOnLoad();

Any ideas how to avoid this error?

1 Answer 1

0

Your restore call is running before the grid's API is registered, so you just need to wait for that to happen.

You can move your restoreOnLoad() call to your onRegisterApi handler:

vm.gridOptions = {
  onRegisterApi: function (gridApi) {
    $scope.gridApi = gridApi;
    restoreOnLoad();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

if you call restoreOnLoad in the onRegisterApi callback, won't it run after the grid rendered. In other words, that callback doesn't get called until the grid is made visible right....so won't that lead to a double draw? Draw the grid in the "default" state, then again after restore?

Your Answer

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