Angular UI Grid - 添加或删除列并更新数据

时间:2016-04-20 07:31:24

标签: javascript angularjs angular-ui-grid

我正在尝试从网格中添加/删除列并更新数据。这是Plunker

HTML

<div ng-controller="MainCtrl">
    <div id="grid1" ui-grid="settings" class="grid"></div>

    <button ng-click="add();">Add</button>
  </div>

JS

app.controller('MainCtrl', ['$scope', function($scope) {
  $scope.result = [{
    "firstName": "Cox",
    "lastName": "Carney",
    "company": "Enormo",
    "employed": true
  }, {
    "firstName": "Lorraine",
    "lastName": "Wise",
    "company": "Comveyer",
    "employed": false
  }, {
    "firstName": "Nancy",
    "lastName": "Waters",
    "company": "Fuelton",
    "employed": false
  }];

  $scope.column_defs = [{
    name: 'First Name',
    field: 'firstName'
  }, {
    name: 'Last Name',
    field: 'lastName'
  }, {
    name: 'Company',
    field: 'company'
  }];

  $scope.settings = {
    data: $scope.result,
    columnDefs: $scope.column_defs
  };

  $scope.add = function() {
    $scope.column_defs = [{
      name: 'First Name',
      field: 'firstName'
    }, {
      name: 'Last Name',
      field: 'lastName'
    }, {
      name: 'Company',
      field: 'company'
    }, {
      name: 'Employed',
      field: 'employed'
    }];

    $scope.result = [{
      "firstName": "Cox",
      "lastName": "Carney",
      "company": "Enormo",
      "employed": true
    }, {
      "firstName": "Lorraine",
      "lastName": "Wise",
      "company": "Comveyer",
      "employed": false
    }];
  };
}]);

当我们点击“添加&#39;按钮,没有任何反应,而我希望更新columnDefsdata

提前致谢。

2 个答案:

答案 0 :(得分:3)

UI网格绑定到$scope.settings。您需要刷新$scope.settings变量以刷新网格。在$scope.add中添加:

$scope.settings = {
    data: $scope.result,
    columnDefs: $scope.column_defs
  };

答案 1 :(得分:0)

这是我的傻瓜:http://plnkr.co/edit/avgavSB3hufCXHfe4nyX?p=preview 它表明您可以在grid.columnDefs对象上使用push / pop / splice。

<强> HTML

  <div ng-controller="MainCtrl">
    <div id="grid1" ui-grid="settings" class="grid"></div>
    <button ng-hide="employedVisible" ng-click="add();">Add</button>
    <button ng-show="employedVisible" ng-click="remove();">Remove</button>
  </div>

<强> JS

var app = angular.module('app', ['ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$scope', function($scope) {
  $scope.settings = {
    data: [{
      "firstName": "Cox",
      "lastName": "Carney",
      "company": "Enormo",
      "employed": true
    }, {
      "firstName": "Lorraine",
      "lastName": "Wise",
      "company": "Comveyer",
      "employed": false
    }, {
      "firstName": "Nancy",
      "lastName": "Waters",
      "company": "Fuelton",
      "employed": false
    }],
    columnDefs: [{
      displayName: 'First Name',
      name: 'firstName'
    }, {
      displayName: 'Last Name',
      name: 'lastName'
    }, {
      displayName: 'Company',
      name: 'company'
    }]
  };

  $scope.add = function() {
    for(var i = $scope.settings.columnDefs.length; i--;){
      if ($scope.settings.columnDefs[i].name === 'employed') {
        // Only add it once
        return;
      }
    }
    $scope.settings.columnDefs.push({
      displayName: 'Employed',
      name: 'employed'
    });
    $scope.employedVisible = true;
  };

  $scope.remove = function() {
    for(var i = $scope.settings.columnDefs.length; i--;){
      if ($scope.settings.columnDefs[i].name === 'employed') {
        $scope.settings.columnDefs.splice(i, 1);
        $scope.employedVisible = false;
        return;
      }
    }
  };
}]);