ag-grid是否有api来过滤多个复选框值? [英] Does ag-grid have an api to filter multiple checkbox values?

查看:192
本文介绍了ag-grid是否有api来过滤多个复选框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularjs并且有一个复选框列表,我需要能够过滤我的ag-grid。

I'm using angularjs and have a list of checkboxes that I need to able to filter my ag-grid on.

使用单选按钮并使用单个值调用api.setQuickFilter可以正常工作。但是,我没有看到允许多个过滤器(即存储在数组中的复选框值)与setQuickFilter一起使用的方法。我应该用另一种方法来实现这个目标吗?

This works fine using radio buttons and calling api.setQuickFilter with the individual value. However, I'm not seeing a way to allow for multiple 'filters' (i.e. checkbox values stored in an array) to function with setQuickFilter. Is there another method I should be using to accomplish this?

示例:

[复选框] Apple

[复选框]蜜蜂

[复选框] Cheerios

Example:
[checkbox] Apple
[checkbox] Bee
[checkbox] Cheerios

复选框Apple和Cheerios应同时返回一个过滤的网格,只显示包含单词Apple或Cheerios的行。

Checking box Apple and Cheerios at the same time should return a grid filtered to only show rows that contain the word "Apple" OR "Cheerios".

推荐答案

根据ag-grid支持,这不可能开箱即用 - 这与一些例子相矛盾(虽然,非-ag)在ag-grid的文档页面上找到(参见 https:// www。 ag-grid.com/javascript-grid-filtering/# 。)他们确实建议编写一个自定义函数来完成此任务。但是,我能够通过另一种方法使其工作。

Per ag-grid support, this is not possible out of the box - which contradicts some of the examples (albeit, non-working) found on ag-grid's documentation page (see https://www.ag-grid.com/javascript-grid-filtering/#.) They did suggest writing a custom function to accomplish this. However, I was able to get it to work through another method.

解决方案:我能够通过在控制器中编写一个函数来过滤掉选择(搜索网格数组并比较元素)重新创建网格数据。然后使用新过滤的数组调用setRowData。我让复选框调用此函数来添加/删除显示的行。

Solution: I was able to filter out the selections by writing a function within the controller (that searches through the grid array and compares elements) to recreate the grid data. Then calls setRowData with the new filtered array. I made the checkboxes call this function to add/remove the displayed rows.

这是代码的样子:

HTML

<div class="checkboxFilter" ng-repeat="filterItemName in filterItem">
  <input type="checkbox" name="selectedCap[]" value="{{filterItemName}}" ng-checked="selection.indexOf(filterItemName) > -1" ng-click="$ctrl.chartData(filterItemName)" /> {{filterItemName}}
</div>

js

//defined elsewhere (i.e. $onLoad):
this.$scope.filterItem = gridFilterArray; //(checkbox items)
this.$scope.selection = [];


chartData(value){

//****this portion from https://stackoverflow.com/questions/14514461/how-do-i-bind-to-list-of-checkbox-values-with-angularjs?rq=1 *****
   let idx = this.$scope.selection.indexOf(value);

   // Is currently selected
   if (idx > -1) {
     this.$scope.selection.splice(idx, 1);
   }

   // Is newly selected
   else {
     this.$scope.selection.push(value);
   }
//******end

   let filteredArray = [];

   //create new array of data to display based on filtered checkboxes
   for(let x = 0; x < this.arrayData.length; x++) {
     for(let y = 0; y < this.$scope.selection.length; y++) {
       if (this.$scope.selection[y] === this.arrayData[x].fieldToCheck)
         filteredArray.push(this.arrayData[x]);
     }
   }

   if(this.$scope.selection.length === 0)
     this.$scope.gridOptions.api.setRowData(this.arrayData);
   else
     this.$scope.gridOptions.api.setRowData(filteredArray);
}

这篇关于ag-grid是否有api来过滤多个复选框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆