2016-07-27 63 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