2016-07-27 73 views
1

我正在尝试使用rowEntity值筛选下拉选项。Angularjs - Angular UI Grid:可以通过rowentity值动态地过滤下拉选项吗?

$scope.data = [{ 
    'id_a': 1, 
    'code': 1, 
    'line': 'Line 1 ' 
}, { 
    'id_a': 2, 
    'code': 2, 
    'line': 'Line 2' 
}, { 
    'id_a': 3, 
    'code': 1, 
    'line': 'Line 3' 
}, { 
    'id_a': 4, 
    'code': 3, 
    'line': 'Line 4' 
}]; 


$scope.opts = [{ 
    id: 1, 
    value: 'A', 
    code: 1 
}, { 
    id: 2, 
    value: 'B', 
    code: 2 
}, { 
    id: 3, 
    value: 'C', 
    code: 1 
}, { 
    id: 4, 
    value: 'D', 
    code: 1 
}, { 
    id: 5, 
    value: 'E', 
    code: 3 
}, { 
    id: 6, 
    value: 'F', 
    code: 2 
}]; 

    $scope.columns = [{ 
    field: 'line', 
    enableCellEdit: false, 
    enableFiltering: false 
}, { 
    field: 'code', 
    enableCellEdit: false, 
    enableFiltering: false 
}, { 
    field: 'value', 
    enableFiltering: false, 
    width: 500, 
    editableCellTemplate: 'ui-grid/dropdownEditor', 
    editDropdownIdLabel: 'id', 
    editDropdownValueLabel: 'value', 
    editDropdownOptionsArray: $scope.opts 
}]; 

$scope.gridOptions = { 
    enableCellEditOnFocus: true, 
    enableFiltering: true, 
    onRegisterApi: function(gridApi) { 
     $scope.gridApi = gridApi; 
    }, 
    columnDefs: $scope.columns 
}; 
$scope.gridOptions.data = $scope.data; 

我想要做的是在editDropdownOptionsArray加载一个数组,然后使用动态值过滤此阵。

如果我在'第1行',那么只能显示具有相同“代码”值的选项。

在这种情况下,“1号线”有码“1”,则下拉菜单选项是“A”,“C”,“d”

我怎样才能做到这一点?使用cellFilter

Here is a plunker与基本代码。

回答

1

希望这个作品出来给你,并随时接受它:

Working Plnkr

  1. 设置你的模板:

    editableCellTemplate: 'dropdown.html' 
    
  2. 添加dropdown.html到您的项目:

<select ng-model="MODEL_COL_FIELD" ng-options="item as item.value for 
item in col.colDef.editDropdownOptionsArray | filter : { code: 
row.entity.code}"></select> 
  • 测试:
  • enter image description here

    +0

    这是一个很好的解决方案!谢谢! –

    1

    从KreepN溶液我已经在代码由一些升级,因为我没”后当我选择一个选项时,获取该行的值。

    我在select上添加了ui-grid-edit-dropdown,这就解决了这个问题。

    Updated Plnkr

    再次感谢您KreepN您的解决方案。

    +0

    很好,你有额外的一英里那里伙计。我也学到了一些东西:) +1 – KreepN