簡體   English   中英

我可以在ui-grid中使用下拉列表來填充數據庫中的數據嗎?

[英]Can i use a dropdown in ui-grid filling it with data from a database?

當我嘗試使用ui-grid內的下拉列表從數據庫傳遞到它的字段時,我在這里面臨一些問題。 文檔之后 ,在gridOptions.columnDefs中,我創建了一個id和值的數組,如:

{ name: 'gender', displayName: 'Gender', editableCellTemplate: 'ui-grid/dropdownEditor', width: '20%',
  cellFilter: 'mapGender', editDropdownValueLabel: 'gender', editDropdownOptionsArray: [
  { id: 1, gender: 'male' },
  { id: 2, gender: 'female' }
] },

但是,在我的情況下,“id”和“value”必須是數據庫中的字段,如下所示:

 { id: data.Id, gender: data.Nome }

它只是不起作用。 關於如何解決這個問題的任何想法? 謝謝!!

您可以考慮使用Angular Grid - 它允許您擁有自定義單元格渲染器,您可以在其中使用交互式單元格渲染器。 我有它在我的工作中工作(不能發布代碼示例,因為它屬於我工作的公司)但是如果這是一個選項我可以模擬一些東西並發布它。

嘗試這樣的事情

editableCellTemplate: 'ui-grid/dropdownEditor', 
editDropdownOptionsArray: YourDataArray, 
editDropdownIdLabel: 'Id', 
editDropdownValueLabel: 'Nome'

YourDataArray可以是一個服務調用 - 例如,對我來說,我有一個MyServiceName.Get()的調用 - 返回的對象可能在你的問題中有像'Id'和'Nome'這樣的屬性。

這是我的方法。 它基於這個主題:

https://github.com/angular-ui/ng-grid/issues/2808

1)我將uiGridFactory.js定義為:

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)然后我在需要下拉邏輯的控制器的末尾添加了過濾器mapDropdown

.filter('mapDropdown', function(uiGridFactory) { 
    return uiGridFactory.getMapDrowdownFilter()
});

3)我將此cellFilter添加到包含下拉列表的列定義中:

 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' }
 ]

mapDropdown()參數是:

a)目錄圖(row.grid.appScope.languageCatalog)

b)ID標簽

c)VALUE標簽

注意:在我的示例中,我使用了帶有工廠從Database加載的$ scope.languageCatalog變量。 你必須實現自己的。

factory.getLanguages = function (callback) {

    $http.get(RESOURCES.REST_ADDRESS + 'getLanguages').
        success(function (data, status, headers, config) {

            callback(data);
        }).
        error(function (data, status, headers, config) {

        });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM