阿里云新用户优惠

十二、UI-Grid Adding and removing columns 添加和删除列

原文:113 Adding and removing columns

表格会监控column defs 和 数据的更新,你可以动态地添加和删除列。列默认的显示顺序是按照columnDefs的顺序,用户可以通过移动表格的列来改变默认的顺序。

动态更改列上的显示名称(以及其他一些列属性),可以通过notifyDataChange API来强制更新。

  • index.html
<!doctype html>
<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.css"></script>
    <script src="app.js"></script>
  </head>
  <body>
    <div ng-controller="MainCtrl">
      Try clicking the Add button to add the company column.
      Try clicking the Remove button to remove the last column.
      Try clicking the Splice button to insert a column in the middle.
      <br>
      <br>
      <button id="button_add" class="btn" ng-click="add()">Add</button>
      <button id="button_remove" class="btn" ng-click="remove()">Remove Last</button>
      <button id="button_splice" class="btn" ng-click="splice()">Splice</button>
      <button id="button_unsplice" class="btn" ng-click="unsplice()">Remove Middle</button>
      <button id="button_toggle_visible" class="btn" ng-click="toggleVisible()">Toggle Visible</button>
      <button id="button_toggle_display_name" class="btn" ng-click="toggleDisplayName()">Toggle Display Name</button>
      <div id="grid1" ui-grid="gridOptions" class="grid"></div>
    </div>
  </body>
</html>
  • main.css
.grid {
  width: 500px;
  height: 250px;
}
  • app.js
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$scope', '$http', 'uiGridConstants', function ($scope, $http, uiGridConstants) {
  $scope.columns = [{ field: 'name' }, { field: 'gender' }];
  $scope.gridOptions = {
    enableSorting: true,
    columnDefs: $scope.columns,
    onRegisterApi: function(gridApi) {
      $scope.gridApi = gridApi;
    }
  };

  $scope.remove = function() {
    $scope.columns.splice($scope.columns.length-1, 1);
  }

  $scope.add = function() {
    $scope.columns.push({ field: 'company', enableSorting: false });
  }

  $scope.splice = function() {
    $scope.columns.splice(1, 0, { field: 'company', enableSorting: false });
  }

  $scope.unsplice = function() {
    $scope.columns.splice(1, 1);
  }

  $scope.toggleDisplayName = function() {
    if( $scope.columns[1].displayName === 'GENDER' ){
      $scope.columns[1].displayName = 'Gender';
    } else {
      $scope.columns[1].displayName = 'GENDER';
    }
    $scope.gridApi.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
  }

  $scope.toggleVisible = function() {
    $scope.columns[0].visible = !($scope.columns[0].visible || $scope.columns[0].visible === undefined);
    $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
  }

  $http.get('http://ui-grid.info/data/100.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
    });
}]);

关注微信公众号,与我交流吧~

分享