Skip to content

Commit 480927f

Browse files
committed
feat(edit): add a function to retrieve dropdown options
Add a new member to ColumnDef: editDropdownOptionsFunction: a function returning an array of values in the format [ {id: xxx, value: xxx} ], which will be used to populate the edit dropdown. This can be used when the dropdown values are dependent on the backing row entity with some kind of algorithm. If this property is set then both editDropdownOptionsArray and editDropdownRowEntityOptionsArrayPath will be ignored.
1 parent beefb67 commit 480927f

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

misc/tutorial/201_editable.ngdoc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ __ColumnDef Options__:
5555
- `editDropdownValueLabel` (default: `'value'`) Controls the value label in the options array - if your array happens to
5656
contain `'name'` instead you can use it without having to reprocess the array
5757
- `editDropdownRowEntityOptionsArrayPath` can be used as an alternative to editDropdownOptionsArray when the contents of the dropdown depend on the entity backing the row.
58+
- `editDropdownOptionsFunction` can be used as yet another alternative to editDropdownOptionsArray when the contents can be retrieved with a function whose parameters are row entity and column definition.
5859
- `editDropdownFilter` (default: `''`) Allows you to apply a filter to the values in the edit dropdown options, for example
5960
if you were using angular-translate you would set this to `'translate'`
6061

@@ -93,7 +94,7 @@ $scope.gridOptions.columnDefs = [
9394
};
9495
});
9596

96-
app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
97+
app.controller('MainCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
9798
$scope.gridOptions = { };
9899

99100
$scope.storeFile = function( gridRow, gridCol, files ) {
@@ -133,6 +134,22 @@ $scope.gridOptions.columnDefs = [
133134
{ name: 'pet', displayName: 'Pet', width: '20%', editableCellTemplate: 'ui-grid/dropdownEditor',
134135
editDropdownRowEntityOptionsArrayPath: 'foo.bar[0].options', editDropdownIdLabel: 'value'
135136
},
137+
{ name: 'status', displayName: 'Status', width: '20%', editableCellTemplate: 'ui-grid/dropdownEditor',
138+
cellFilter: 'mapStatus',
139+
editDropdownOptionsFunction: function(rowEntity, colDef) {
140+
var single;
141+
var married = {id: 3, value: 'Married'};
142+
if (rowEntity.gender === 1) {
143+
single = {id: 1, value: 'Bachelor'};
144+
return [single, married];
145+
} else {
146+
single = {id: 2, value: 'Nubile'};
147+
return $timeout(function() {
148+
return [single, married];
149+
}, 100);
150+
}
151+
}
152+
},
136153
{ name: 'filename', displayName: 'File', width: '20%', editableCellTemplate: 'ui-grid/fileChooserEditor',
137154
editFileChooserCallback: $scope.storeFile }
138155
];
@@ -180,6 +197,22 @@ $scope.gridOptions.columnDefs = [
180197
}
181198
};
182199
})
200+
201+
.filter('mapStatus', function() {
202+
var genderHash = {
203+
1: 'Bachelor',
204+
2: 'Nubile',
205+
3: 'Married'
206+
};
207+
208+
return function(input) {
209+
if (!input){
210+
return '';
211+
} else {
212+
return genderHash[input];
213+
}
214+
};
215+
})
183216
});
184217
</file>
185218
<file name="index.html">

src/features/edit/js/gridEdit.js

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@
457457
*/
458458

459459
module.directive('uiGridCell',
460-
['$compile', '$injector', '$timeout', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', '$rootScope',
461-
function ($compile, $injector, $timeout, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService, $rootScope) {
460+
['$compile', '$injector', '$timeout', 'uiGridConstants', 'uiGridEditConstants', 'gridUtil', '$parse', 'uiGridEditService', '$rootScope', '$q',
461+
function ($compile, $injector, $timeout, uiGridConstants, uiGridEditConstants, gridUtil, $parse, uiGridEditService, $rootScope, $q) {
462462
var touchstartTimeout = 500;
463463
if ($injector.has('uiGridCellNavService')) {
464464
var uiGridCellNavService = $injector.get('uiGridCellNavService');
@@ -652,6 +652,40 @@
652652
* </pre>
653653
*
654654
*/
655+
/**
656+
* @ngdoc service
657+
* @name editDropdownOptionsFunction
658+
* @methodOf ui.grid.edit.api:ColumnDef
659+
* @description a function returning an array of values in the format
660+
* [ {id: xxx, value: xxx} ], which will be used to populate
661+
* the edit dropdown. This can be used when the dropdown values are dependent on
662+
* the backing row entity with some kind of algorithm.
663+
* If this property is set then both editDropdownOptionsArray and
664+
* editDropdownRowEntityOptionsArrayPath will be ignored.
665+
* @param {object} rowEntity the options.data element that the returned array refers to
666+
* @param {object} colDef the column that implements this dropdown
667+
* @returns {object} an array of values in the format
668+
* [ {id: xxx, value: xxx} ] used to populate the edit dropdown
669+
* @example
670+
* <pre>
671+
* $scope.gridOptions = {
672+
* columnDefs: [
673+
* {name: 'status', editableCellTemplate: 'ui-grid/dropdownEditor',
674+
* editDropdownOptionsFunction: function(rowEntity, colDef) {
675+
* if (rowEntity.foo === 'bar') {
676+
* return [{id: 'bar1', value: 'BAR 1'},
677+
* {id: 'bar2', value: 'BAR 2'},
678+
* {id: 'bar3', value: 'BAR 3'}];
679+
* } else {
680+
* return [{id: 'foo1', value: 'FOO 1'},
681+
* {id: 'foo2', value: 'FOO 2'}];
682+
* }
683+
* },
684+
* editDropdownIdLabel: 'code', editDropdownValueLabel: 'status' }
685+
* ],
686+
* </pre>
687+
*
688+
*/
655689
/**
656690
* @ngdoc property
657691
* @name editDropdownValueLabel
@@ -731,12 +765,24 @@
731765
}
732766
html = html.replace('INPUT_TYPE', inputType);
733767

734-
var editDropdownRowEntityOptionsArrayPath = $scope.col.colDef.editDropdownRowEntityOptionsArrayPath;
735-
if (editDropdownRowEntityOptionsArrayPath) {
736-
$scope.editDropdownOptionsArray = resolveObjectFromPath($scope.row.entity, editDropdownRowEntityOptionsArrayPath);
737-
}
738-
else {
739-
$scope.editDropdownOptionsArray = $scope.col.colDef.editDropdownOptionsArray;
768+
// In order to fill dropdown options we use:
769+
// - A function/promise or
770+
// - An array inside of row entity if no function exists or
771+
// - A single array for the whole column if none of the previous exists.
772+
var editDropdownOptionsFunction = $scope.col.colDef.editDropdownOptionsFunction;
773+
if (editDropdownOptionsFunction) {
774+
$q.when(editDropdownOptionsFunction($scope.row.entity, $scope.col.colDef))
775+
.then(function(result) {
776+
$scope.editDropdownOptionsArray = result;
777+
});
778+
} else {
779+
var editDropdownRowEntityOptionsArrayPath = $scope.col.colDef.editDropdownRowEntityOptionsArrayPath;
780+
if (editDropdownRowEntityOptionsArrayPath) {
781+
$scope.editDropdownOptionsArray = resolveObjectFromPath($scope.row.entity, editDropdownRowEntityOptionsArrayPath);
782+
}
783+
else {
784+
$scope.editDropdownOptionsArray = $scope.col.colDef.editDropdownOptionsArray;
785+
}
740786
}
741787
$scope.editDropdownIdLabel = $scope.col.colDef.editDropdownIdLabel ? $scope.col.colDef.editDropdownIdLabel : 'id';
742788
$scope.editDropdownValueLabel = $scope.col.colDef.editDropdownValueLabel ? $scope.col.colDef.editDropdownValueLabel : 'value';

0 commit comments

Comments
 (0)