【问题标题】:ui-grid field returning arrayui-grid 字段返回数组
【发布时间】:2017-09-13 14:09:12
【问题描述】:

Output

嗨, 我需要根据位置(“SB”或“CO”)过滤掉一些公司名称。所以我在ui-grid中使用了自定义过滤器。它仅在完成映射时才返回每个键的整个值。我已经附上了输出。 Plunker 代码演示:link 使用:Angular - v1.4.2 ui-grid - v3.0.7

    application.controller('ApplicationController', ['$scope', 'uiGridConstants', 'ApplicationService',
        function ($scope, uiGridConstants, ApplicationService) {
            $scope.message = "Welcome";
            $scope.gridOptions = {
                showGridFooter: true,
                showColumnFooter: true,
                enableFiltering: true
            };
            $scope.gridOptions.columnDefs = [
                {
                    name: 'company',
                    field: 'company',
                    filterHeaderTemplate: '<div class="ui-grid-filter-container" ng-repeat="colFilter in col.filters"><div my-custom-dropdown></div></div>',
                    filter: {
                        term: '',
                        options: [{ id: 'CO', value: 'Navy' }, { id: 'CO', value: 'Army' }, { id: 'SB', value: 'Hp' }]
                    },
                    cellFilter: 'mapCompany'
                }
            ];
            ApplicationService.getData(function (response) {
                if (response) {
                    $scope.gridOptions.data = response.data;
                    response.data.forEach(function addDates(row, index) {
                        var companyName = row.company;
                        if (companyName === 'Navy')
                            row.company = 'CO';
                        else if (companyName === 'Army')
                            row.company = 'CO';
                        else if (companyName === 'Hp')
                            row.company = 'SB';
                    });
                }
                else {
                    $scope.gridOptions.data = null;
                }
            });
        }
    ])
    .filter('mapCompany', function () {
            var genderHash = {
                'CO': ['Navy'],
                'CO': ['Army'],
                'SB': ['Hp']
            };
            return function (input) {
                if (!input) {

                    return '';
                }
                else {
                    return genderHash[input];
                }
            };
        })
     .directive('myCustomDropdown', function () {
            return {
                template: '<input ng-model="colFilter.term" >'
            };
        })

【问题讨论】:

    标签: angularjs filter angular-ui-grid ui-grid


    【解决方案1】:

    你写道:

    var genderHash = {
                'CO': ['Navy'],
                'CO': ['Army'],
                'SB': ['Hp']
            }; 
    

    改为:

    var genderHash = {
                'CO': 'Navy',
                'CO': 'Army',
                'SB': 'Hp'
            };
    

    还请记住,您创建的地图使用相同的键 "CO"

    【讨论】:

    • 您的回答部分解决了我的问题。但是在前端仍然看不到单独的“军队”。可能是因为循环(foreach)中的赋值row.company = 'CO'。
    • @NirmalBalajiPackirisamy 你能在 plunker 中发布演示吗?
    • 我需要一些时间才能在 Plunker 中发布演示。
    • 我对两个不同的值使用了相同的键,因为在我的应用程序中,当用户搜索“CO”时,过滤后的输出应该同时包含“海军”和“陆军”。 (多对一映射)。
    • 这是您要求的 plunker 代码演示:plunker
    【解决方案2】:
            filter: {
                    condition: function(searchTerm,cellValue) {
                      if( !searchTerm || searchTerm === '') {
                        return true;
                      }
    
                      if(searchTerm === 'CO') {
                        if(cellValue === 'Navy' || cellValue === 'Army') {
                         return true; 
                        }
                      }
                      if(searchTerm === 'SB') {
                        if(cellValue === 'Hp') {
                         return true; 
                        }
                      }
                      return false;
                    }
                  }
    

    我应该在 gridOptions 中使用 filter 参数而不是带有 Hashmap 的 cellFilter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2023-03-24
      • 2017-03-28
      • 2022-11-08
      • 2018-12-02
      相关资源
      最近更新 更多