Angular UI 网格:如何为列筛选创建预填充的下拉菜单

Angular UI Grid: How to create a pre-populated dropdown menu for column filtering

我正在寻找有关 Angular UI Grid. Specifically I am exploring filtering and while I was able to successfully implement sorting using free form text field(s) in my application as they do in the example on their site 的功能的一些帮助 我想要一些帮助找到一种方法来代替使用 pre - 某些列的填充下拉菜单

澄清一下: 预填充是指我希望通过我的代码填充下拉菜单。如果解决方案包含硬编码数据我没问题,但我的最终目标是让预填充由正在排序的列的 unique 数据值集组成:)

我在(例如)Kendo UI (kendodropdownlist) 中看到了此功能,但我对该解决方案附带的价格标签不感兴趣。我想坚持使用上面提到的(和链接的)Angular UI-网格。在 Whosebug 上,我发现了一个 similar question,但不幸的是,它似乎没有得到太多关注。我希望通过对我正在寻找的内容进行更详细的解释,我会得到比我在那里找到的更完整的答案。

这是我控制器中当前的内容:

var simpleMessagingApp = angular.module('MainAppCtrl', [ 'ngAnimate',
                                                         'ngTouch', 'ui.grid' ]);

simpleMessagingApp.controller('CacheTableCtrl', [ '$scope', '$http',
                                                  'uiGridConstants', function($scope, $http, uiGridConstants) {
    $scope.columns = [ {
        field : 'trans_detail',
        displayName : 'Transaction'
    }, {
        field : 'cust_name',
        displayName : 'Customer'
    }, {
        field : 'quantity',
        displayName : 'Quantity',
        filters : [ {
            condition : uiGridConstants.filter.GREATER_THAN,
            placeholder : 'greater than'
        }, {
            condition : uiGridConstants.filter.LESS_THAN,
            placeholder : 'less than'
        }
        ]
    }, {
        field : 'today_date',
        displayName : 'Current Date'
    } ];
    $scope.gridOptions1 = {
            enableSorting : true,
            enableFiltering : true,
            columnDefs : $scope.columns,
            onRegisterApi : function(gridApi) {
                $scope.grid1Api = gridApi;
            }
    };

    $http.get("../services/Coherence/Cache").success(function(data) {
        $scope.gridOptions1.data = data;
    });

} ]);

下面是输出 - 带有自由格式的文本字段

对于这个特定的示例列客户、数量和当前日期,我可能会保留自由形式的下拉列表,但我真的希望能够使用预先填充的交易下拉列表进行过滤(并且把它放在我的工具箱里,当然是为了未来的项目!)。

谢谢!

您可以通过 columnDefs

中的 headerCellTemplate 在 header 中放置一个下拉菜单
  columnDefs: [
     {field:'myField',headerCellTempalte: 'mypulldowntemplate.html"...}
  ]

mypulldowntemplate.html

  <div ng-class="{ 'sortable': sortable }">
  <div class="ui-grid-vertical-bar">&nbsp;</div>
<div class="ui-grid-cell-contents {{col.headerClass}}" col-index="renderIndex">
    {{ col.displayName CUSTOM_FILTERS }}
    <br>
    <select ng-model="getExternalScopes().value[col.field]" ng-change="$event.stopPropagation();getExternalScopes().yourFilterFunction(col.field)">
    </select>
      
  ....

yourFilterFunction() 可以做任何你想过滤的事情。也许只是通过设置一些您在分配给网格的自定义过滤器中使用的变量。您可以在此处 http://ui-grid.info/docs/#/tutorial/103_filtering

的 ui 网格教程中找到在自定义过滤器中设置条件的示例

我有同样的要求uirement recently.I 已经弄明白了 myself.Here 是我的步骤 followed.If 你想在 ui-grid you can use two approaches either use existing ui-grid custom_filters or created a header template and bind it into grid.There is a nice article that how can add drop downs in ui-grid.基于这些代码,我自定义了我的代码 snippets.What 我所做的是我在 index.html.

中创建了自定义模板
<script type="text/ng-template" id="uiSelect">

      <ui-select-wrap>
        <label> Gender</label>
        <ui-select ng-model="MODEL_COL_FIELD" theme="selectize" ng-disabled="disabled" append-to-body="true" on-select="grid.appScope.filterTableBasedonDropDownSelect($item)">
          <ui-select-match placeholder="Select...">{{$select.selected}}</ui-select-match>
          <ui-select-choices repeat="item in col.colDef.editDropdownOptionsArray | filter: $select.search">
            <span>{{ item }}</span>
          </ui-select-choices>
        </ui-select>
      </ui-select-wrap>
    </script>

我创建了名为 filterTableBasedonDropDownSelect($item) 的函数,它将处理过滤 logic.Note,当您在 ui 中调用函数时,网格中的正常函数声明不起作用。因为 ui-grid 有自己的 parent scope。所以你应该这样调用你的函数。

on-select="grid.appScope.filterTableBasedonDropDownSelect($item)"

然后你就可以在controller下声明你的函数逻辑了。

$scope.filterTableBasedonDropDownSelect= function(item){
     $scope.gridOptions.data  = $filter('filter')($scope.jsonData,item, undefined);};

这是我的作品example

希望这对某人有所帮助。

您可以使用此处记录的内置 selectOptions 过滤器功能:http://ui-grid.info/docs/#/tutorial/103_filtering

示例:

angular.module('exampleApp', ['ui.grid'])
  .controller('exampleCtrl', ['$scope', 'uiGridConstants', function($scope, uiGridConstants) {
    var animals = [
      { id: 1, type: 'Mammal', name: 'Elephant' },
      { id: 2, type: 'Reptile', name: 'Turtle' },
      { id: 3, type: 'Mammal', name: 'Human' }
    ];
                                                          
    var animalTypes = [
      { value: 'Mammal', label: 'Mammal' },
      { value: 'Reptile', label: 'Reptile'}
    ];
  
    $scope.animalGrid = {
      enableFiltering: true,
      columnDefs: [
        {
          name: 'type', 
          field: 'type', 
          filter: { selectOptions: animalTypes, type: uiGridConstants.filter.SELECT }
        },
        { name: 'name', name: 'name'}
      ],
      data: animals
    };
      
  }]);
<link href="http://ui-grid.info/release/ui-grid.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://ui-grid.info/release/ui-grid.js"></script>

<div ng-app="exampleApp">
  <div ng-controller="exampleCtrl">
    <h1>UI Grid Dropdown Filter Example</h1>
    <div ui-grid="animalGrid" class="grid"></div>
  </div>
</div>

已接受答案的扩展是我通过反复试验发现的。您可以在 selectOptions:

中使用正则表达式
           columnDefs: [
            {
                name: 'starRating',
                headerCellClass: 'blue',
                headerTooltip: 'Star Rating',
                maxWidth: 100, 
                filter:
                {
                    type: uiGridConstants.filter.SELECT,
                    selectOptions: [
                        { value: /(THREE|FOUR|FIVE)/, label: 'Positive' },  // Here I wanted the user to be able to choose one option that would filter by several of the possible values in the data set
                        { value: /(ONE|TWO)/, label: 'Negative' },  // ...and Here
                        { value: 'ONE', label: '1' },
                        { value: 'TWO', label: '2' },
                        { value: 'THREE', label: '3' },
                        { value: 'FOUR', label: '4' },
                        { value: 'FIVE', label: '5' }
                    ]
                }
            },