Reputation: 35
I'm facing some issue here when i try to use a dropdown inside a ui-grid passing to it fields from a database. Following the docs, in the gridOptions.columnDefs, i have create an array of id and a value, like:
{ name: 'gender', displayName: 'Gender', editableCellTemplate: 'ui-grid/dropdownEditor', width: '20%',
cellFilter: 'mapGender', editDropdownValueLabel: 'gender', editDropdownOptionsArray: [
{ id: 1, gender: 'male' },
{ id: 2, gender: 'female' }
] },
but, in my case the "id" and the "value", must be fields from the database, as follows:
{ id: data.Id, gender: data.Nome }
It just don't work. Any ideas about how to solve this? Thanks!!
Upvotes: 3
Views: 8080
Reputation: 958
Try something like this
editableCellTemplate: 'ui-grid/dropdownEditor',
editDropdownOptionsArray: YourDataArray,
editDropdownIdLabel: 'Id',
editDropdownValueLabel: 'Nome'
YourDataArray could be a service call - for example, for me I have a call to MyServiceName.Get() - the objects returned might have properties like 'Id' and 'Nome' as in your question.
Upvotes: 1
Reputation: 867
Here is my approach. It was based on this thread:
https://github.com/angular-ui/ng-grid/issues/2808
1) I defined an uiGridFactory.js as this one:
angularFactories.factory('uiGridFactory', function ($http, $rootScope) {
var factory = {};
/* It returns a dropdown filter to help you show editDropdownValueLabel
*
* Parameters:
*
* - input: selected input value, it always comes when you select a dropdown value
* - map: Dictionary containing the catalog info. For example:
* $scope.languageCatalog = [ {'id': 'EN', 'description': 'English'}, {'id': 'ES', 'description': 'Español'} ]
* - idLabel: ID label. For this example: 'id'.
* - valueLabel: Value label. For this example: 'description'.
*
* 1) Configure cellFilter this way at the ui-grid colDef:
*
* { field: 'languageId', name: 'Language'), editableCellTemplate: 'ui-grid/dropdownEditor',
* editDropdownIdLabel: 'id', editDropdownValueLabel: 'description',
* editDropdownOptionsArray: $scope.languageCatalog,
* cellFilter: 'mapDropdown:row:row.grid.appScope.languageCatalog:"id":"description":languageCatalog' },
*
* 2) Append this snippet to the controller:
*
* .filter('mapDropdown', function(uiGridFactory) {
* return uiGridFactory.getMapDrowdownFilter()
* });
*
*/
factory.getMapDrowdownFilter = function() {
return function(input, map, idLabel, valueLabel) {
if (map != null)
{
for (var i = 0; i < map.length; i ++) {
if (map[i][idLabel] == input)
{
return map[i][valueLabel];
}
}
}
return "";
}
}
return factory;
});
2) Then I added the filter mapDropdown at the end of the controller that requires dropdown logic
.filter('mapDropdown', function(uiGridFactory) {
return uiGridFactory.getMapDrowdownFilter()
});
3) I added this cellFilter to the column definition that contains the dropdown:
columnDefs: [
{ field: 'id', 'ID')},
{ field: 'languageId', name: 'Language',
editableCellTemplate: 'ui-grid/dropdownEditor',
editDropdownIdLabel: 'id', editDropdownValueLabel: 'description',
editDropdownOptionsArray: $scope.languageCatalog,
cellFilter: 'mapDropdown:row.grid.appScope.languageCatalog:"id":"description"' },
{ field: 'comments', 'Comments' }
]
where the mapDropdown() parameters are:
a) the catalog map (row.grid.appScope.languageCatalog)
b) the ID label
c) the VALUE label
Note: In my example I used $scope.languageCatalog variable that was loaded from Database with a factory. You have to implement your own.
factory.getLanguages = function (callback) {
$http.get(RESOURCES.REST_ADDRESS + 'getLanguages').
success(function (data, status, headers, config) {
callback(data);
}).
error(function (data, status, headers, config) {
});
}
Upvotes: 1
Reputation: 2411
You could consider using Angular Grid - it allows you to have custom cell renderers, where you can have a cell renderer that's interactive. I have it working in my job (can't post the code example as it belongs to the company I work for) however if this is an option I can mock something up and post it.
Upvotes: 2