【问题标题】:Angularjs - Angular UI Grid: It's possible to filter dynamically dropdown options by rowentity values?Angularjs - Angular UI Grid:可以通过 rowentity 值动态过滤下拉选项吗?
【发布时间】:2016-07-28 21:19:43
【问题描述】:

我正在尝试使用 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 带有基本代码。

【问题讨论】:

    标签: javascript html angularjs angular-ui-grid


    【解决方案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>
    
    1. 测试:

    【讨论】:

    • 这是一个很好的解决方案!谢谢!
    【解决方案2】:

    在 KreepN 的解决方案之后,我对代码进行了一些升级,因为当我选择一个选项时,我没有得到该行的值。

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

    Updated Plnkr

    再次感谢 KreepN 的解决方案。

    【讨论】:

    • 感谢你多跑一英里,伙计。我也可以学到一些东西:) +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多