在ui-grid editableCellTemplate [ng-grid 3.x]中使用ng-option下拉列表

jul*_*win 8 javascript grid angularjs ng-grid angular-ui-grid

我正在使用ng-grid的新3.0版ui- grid在我的应用程序中创建一个网格.我正在尝试做的是在我的表格中创建一个可编辑单元格的ng-options下拉列表,其中填充了使用角度工厂检索的数据.

我试图通过使用ui-grid 的editableCellTemplate功能来实现这一点.

这是一些示例代码:

HTML:

<div ui-grid="gridOptions" ui-grid-edit class="grid"></div>
Run Code Online (Sandbox Code Playgroud)

控制器:

$scope.gridOptions = {
    enableSorting: true,
    enableFiltering: true,
    enableCellEditOnFocus: true,
    columnDefs: [
      { field: 'name',
        sort: {
          direction: 'desc',
          priority: 1
        }
      },
      { field: 'gender', editType: 'dropdown', enableCellEdit: true,
          editableCellTemplate: 'temp.html' },
      { field: 'company', enableSorting: false }
]};
Run Code Online (Sandbox Code Playgroud)

temp.html:

<div>
    <select ng-model="row.entity.gender" data-ng-options="d as d.type for d in genderType">
        <option value="" selected disabled>Choose Gender</option>
    </select>
</div>
Run Code Online (Sandbox Code Playgroud)

这是一个带代码的plunker.[ 注意:这只是示例代码.用于ng-options的数组正在实际代码中从角度工厂中提取,而不是在范围中声明.editDropdownOptionsArray可能无法正常工作,因为数据是动态的.]

是否可以使用ui-grid执行此操作?我想也许这是一个范围问题,因为如果我将ng-option代码放在​​我的HTML页面中它按预期工作,但我可以从ui-grid文档收集的是temp.html文件应该在范围内.我知道这些东西仍处于不稳定状态,但对此事的任何帮助都将不胜感激!


2015年3月31日更新:

嗨,大家好,如果您尝试这个解决方案并且不起作用,请注意.1月份,外部范围的代码被重构getExternalScopes()grid.addScope.source.https://github.com/angular-ui/ng-grid/issues/1379

这是更新的plunkr与新代码:点击我!

mai*_*guy 6

Not sure if this may help you, because I'm also just starting to play around with the new ng-grid.

It seems like a lot of options have changed. From what I learned I can tell you that there is no more need for a cellTemplate if you want to have a dropdown. It's already built in.

Activate it like this:

app.controller('MainCtrl', ['$scope', '$http',
  function($scope, $http) {

    $scope.genderTypes = [{
      ID: 1,
      type: 'female'
    }, {
      ID: 2,
      type: 'female'
    }, {
      ID: 3,
      type: 'both'
    }, {
      ID: 4,
      type: 'none'
    }, ];


    $scope.gridOptions = {
      enableSorting: true,
      enableFiltering: true,
      enableCellEditOnFocus: true,
      columnDefs: [{
        field: 'name',
        sort: {
          direction: 'desc',
          priority: 1
        }
      }, {
        field: 'gender',
        editType: 'dropdown',
        enableCellEdit: true,
        editableCellTemplate: 'ui-grid/dropdownEditor',
        editDropdownOptionsArray: $scope.genderTypes,
        editDropdownIdLabel: 'type',
        editDropdownValueLabel: 'type'
      }, {
        field: 'company',
        enableSorting: false
      }],
      onRegisterApi: function(gridApi) {
        grid = gridApi.grid;
      }
    };

    $http.get('https://rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json')
      .success(function(data) {
        $scope.gridOptions.data = data;
      });

  }
]);
Run Code Online (Sandbox Code Playgroud)

I'm not quiet sure if I like this approach, but time and usage will tell ...

Oh, and I haven't found out how to detect changes. Still searching the (lousy) documentation for an event or if I have to watch grid-data for changes.

Tell me if you have found something about this.

So far have fun with this Plunker

Update:

I found out how to react to a change. Add/Change these lines:

  onRegisterApi: function(gridApi) {
    grid = gridApi.grid;
    gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef) {
      alert(rowEntity.name + ' ' + rowEntity.gender);
    });
  }
Run Code Online (Sandbox Code Playgroud)

Alert pops up when you leave edit mode. e.g Click outside the cell.

Hope this helps.


小智 6

您需要在3.i版本的ui-grid中使用外部范围功能.您无法在选择下拉列表中获取任何选项的原因是因为ui-grid现在使用隔离范围,这将不允许您在单元格中直接访问应用程序范围变量.

我能让你的plunkr使用以下步骤 - 请参阅更新的plunkr

脚步:

1) 在index.html中,在grid div中指定external-scopes属性,即modify

<div ui-grid="gridOptions" ui-grid-edit class="grid"></div>
Run Code Online (Sandbox Code Playgroud)

<div ui-grid="gridOptions" ui-grid-edit class="grid" external-scopes="myExternalScope"></div> 
Run Code Online (Sandbox Code Playgroud)

2)在app.js中,将范围分配给我们的外部范围属性,即添加以下行:

$scope.myExternalScope = $scope;
Run Code Online (Sandbox Code Playgroud)

3)在temp.html中,使用getExternalScopes()访问genderTypes数组,即修改editableCellTemplate

<select ng-model="row.entity.Gender" data-ng-options="d as d.type for d in genderType">
Run Code Online (Sandbox Code Playgroud)

<select ng-model="row.entity.Gender" data-ng-options="d as d.type for d in getExternalScopes().genderTypes">
Run Code Online (Sandbox Code Playgroud)

额外的想法:

1)我没有找到适合我需要的ui-grid/dropdownEditor - 因此,还没有尝试过.

2)您还可以添加cellTemplate以及editableCellTemplate.您可以指定两个相同的值.

参考文献:

  1. http://ui-grid.info/docs/#/tutorial/305_externalScopes