angularjs - Angular UI Grid:如何创建用于列过滤的预填充下拉菜单

标签 angularjs drop-down-menu angular-ui-grid

我正在寻找有关 Angular UI Grid 功能的帮助。具体来说,我正在探索 filtering ,虽然我能够像在 example on their site 中一样在应用程序中使用自由格式文本字段成功实现排序,但我希望能找到一些帮助,以找到一种方法来代替使用pre某些列的填充下拉菜单。

进行说明:通过预填充,我的意思是我希望通过我的代码填充下拉列表。我很好,如果解决方案包含硬编码数据,但我的最终目标是让预填充包含要排序的列的唯一数据值集:)

我已经在(例如)Kendo UI(kendodropdownlist)中看到了此功能,但是我对该解决方案附带的价格标签不感兴趣。我想坚持上面提到(和链接)的Angular UI网格。在StackOverflow上,我发现了一个 similar question ,但不幸的是,它似乎并没有受到太大的关注。我希望通过对要查找的内容进行更详细的说明,我将得到比在此找到的更完整的答案。

这是我的 Controller 中当前的内容:

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;
    });

} ]);

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

对于此特定示例,“客户”,“数量”和“当前日期”列可能会保留为自由格式下拉列表,但我真的很希望能够使用预先填充的交易下拉列表进行过滤(并将其放在我的工具箱中)当然用于 future 的项目!)。

谢谢!

最佳答案

您可以使用此处记录的内置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>

关于angularjs - Angular UI Grid:如何创建用于列过滤的预填充下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28097758/

相关文章:

angularjs - 在 AngularJS 中渲染嵌套对象时更改范围

javascript - 嵌套表单 rails 中的动态选择

javascript - 更改选择按钮的样式和/或条件样式

angularjs - 安全的 RESTful API - 如何下载文件

javascript - 空文本输入框的值是多少?

html - CSS 下拉菜单 <li> 堆叠在一起?

html - 将下拉菜单定位到中心时出现问题

javascript - 用户界面网格 : how to show and position a popup menu div onclick of a row element inside the grid?

grouping - UI网格分组自动扩展

jquery - 获取 div 内第一个跨度的背景颜色