为什么ui-grid重置为默认状态,设置过滤后,恢复后状态?

时间:2016-07-18 10:00:58

标签: javascript angular-ui-grid ui-grid

我创建了ui-grid,并通过localStorageService创建了将网格状态保存到cookie的功能。然后我改变列宽并保存这个状态。通过< gridApi.saveState.restore'恢复状态后这样做很完美,但是当我设置过滤器时,网格重置列为默认宽度。 如果我做错了,请告诉我。

    $scope.enableFiltering = function () {
      var isEnabledFiltering = localStorageService.get('enableFiltering');
      if (!_.isNull(isEnabledFiltering) && !_.isUndefined(isEnabledFiltering)) {
        return JSON.parse(isEnabledFiltering);
      }
      return false;
    };

    $scope.colDefs = [
      {
        displayName: locMsg['id'],
        field: 'id',
        enableHiding: true,
        visible: false,
        headerCellClass: 'header-filtered',
        filters: [{
          condition: uiGridConstants.filter.CONTAINS
        }]
      },
      {
        displayName: locMsg['name'],
        field: 'name',
        enableHiding: true,
        headerCellClass: 'header-filtered',
        filters: [{
          condition: uiGridConstants.filter.CONTAINS
        }],
      },];

    $scope.regularGridStateName = "grid";
    $scope.defaultGridStateName = "defaultGrid";
    $scope.gridOptions = {
      enableFiltering: $scope.enableFiltering(),
      enableGridMenu: true,
      columnDefs : $scope.colDefs
    };

    $scope.toggleFiltering = function () {
      $scope.gridOptions.enableFiltering = !$scope.gridOptions.enableFiltering;
      localStorageService.set('enableFiltering', $scope.gridOptions.enableFiltering);
      $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);

      var gridState = localStorageService.get($scope.regularGridStateName);
      if (gridState) {
        gridApi.saveState.restore($scope, gridState);
      }
    };

    $scope.gridOptions.onRegisterApi = function (gridApi) {
      $scope.gridApi = gridApi;
      $scope.gridOptions.gridMenuCustomItems.push({
        title: 'Reset table',
        action: function () {
          var defaultState = localStorageService.get($scope.defaultGridStateName);
          localStorageService.set($scope.regularGridStateName, defaultState);
          if (defaultState) {
            gridApi.saveState.restore($scope, defaultState);
          }
        },
        order: 220
      });
      $scope.gridOptions.gridMenuCustomItems.push({
        title: 'Save table state',
        action: function () {
          localStorageService.set($scope.regularGridStateName, $scope.gridApi.saveState.save());
        },
        order: 221
      });

      $timeout(function () {
        gridStateService.saveDefaultStateIfNotExist($scope.defaultGridStateName, $scope.gridApi.saveState.save());

        var defaultState = localStorageService.get($scope.defaultGridStateName);
        if (!defaultState) {
          localStorageService.set($scope.regularGridStateName, $scope.gridApi.saveState.save());
        }

        var gridState = localStorageService.get($scope.regularGridStateName);
        if (gridState) {
          gridApi.saveState.restore($scope, gridState);
        }
      }, 10);

2 个答案:

答案 0 :(得分:0)

此问题已解决,只需更新到新版本。

答案 1 :(得分:0)

我有类似的问题,这是由于列顺序的缓存。尝试以下

n