ui-grid,无法理解我是否可以从 $http.get 设置 editDropdownOptionsArray

ui-grid, could not understand if I can set the editDropdownOptionsArray from a $http.get

我在看 Ui 网格教程,看到他们使用过滤器在网格的每一行内填充下拉菜单。

我正在尝试理解这个概念,看看我是否可以使用它来使用 $http.get 方法或使用服务来获取选项。

有人可以指点我更好的教程或指导我吗?

我写了类似

    $scope.ContractDetails.columnDefs = [
        { name: 'Contract_Detail_Id', enableCellEdit: false, visible: false },
        { name: 'Contract_Id', enableCellEdit: false, visible: false },
        { name: 'Contract_Detail_Sl', displayName: 'Sl', enableCellEdit: false },
        { name: 'Contract_Cust_Location', displayName: 'Customer Location' },
        {
            name: 'Contract_Cust_Location', displayName: 'Customer Location',
            editableCellTemplate: 'ui-grid/dropdownEditor',
            editDropdownIdLabel: 'Loc_Id', editDropdownValueLabel: 'Loc_Name',
            editDropdownOptionsArray: $scope.clientLocs,
            cellFilter: 'mapClientLocs:row.grid.appScope.clientLocs:"Loc_Id":"Loc_Name"'
        },
        {
            name: 'Fixed_or_Percentage', displayName: 'Fixed %', editableCellTemplate: 'ui-grid/dropdownEditor',
            cellFilter: 'mapFixed', editDropdownValueLabel: 'Fixed_or_Percentage', editDropdownOptionsArray: [
            { id: false, Fixed_or_Percentage: 'Fixed' },
            { id: true, Fixed_or_Percentage: 'Percentage' }
            ]
        },
]

并添加了两个过滤器:

.filter('mapFixed', function () {
    var fixedHash = {
        false: 'Fixed',
        true: 'Percentage'
    };

    return function (input) {
        if (!input) {
            return '';
        } else {
            return fixedHash[input];
        }
    };
})
.filter('mapClientLocs', function (ClientDataService) {
    var clientLocs = {};
    ClientDataService.GetClientLocs()
    .then(function (response) {
        clientLocs = response;
        console.log("response", response);
        return clientLocs;
    });
    return clientLocs;
});

第一个过滤器工作正常。但是我试图从服务中获取数据的那个不起作用。我收到类似 angular.js:11655 TypeError: fn.apply is not a function

的错误

您的 mapClientLocs 过滤器没有达到您的预期。它立即返回一个空对象,然后在异步请求完成后基本上丢弃结果。如果您不明白为什么,您应该仔细阅读 promises。

基本上,过滤器不是编写异步调用的好地方。在将数据传递给 ui-grid.

之前,您需要在控制器中执行这些操作

正如 Dana 所提到的,过滤器不是执行此操作的合适位置,它应该在控制器中完成。

如果我理解正确,遵循这样的逻辑应该可行:

ClientDataService.GetClientLocs()
        .then(function (response) {
            $scope.ContractDetails.columnDefs[4].editDropdownOptionsArray = response;
        });