javascript - ag-grid 是否有一个 api 来过滤多个复选框值?

标签 javascript angularjs checkbox filtering ag-grid

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

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

示例:
[复选框]苹果
[复选框]蜜蜂
[复选框]麦片

同时选中 Apple 和 Cheerios 复选框应返回一个经过过滤的网格,仅显示包含“Apple”或“Cheerios”一词的行。

最佳答案

根据 ag-grid 支持,这是不可能开箱即用的 - 这与 ag-grid 文档页面上找到的一些示例(尽管不起作用)相矛盾(请参阅 https://www.ag-grid.com/javascript-grid-filtering/# 。)他们确实建议编写一个自定义函数来完成此任务。不过,我可以通过另一种方法让它发挥作用。

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

代码如下:

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

关于javascript - ag-grid 是否有一个 api 来过滤多个复选框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46452147/

相关文章:

javascript - 使用 JS 查找素数序列

android - 滚动自定义 ListView 后复选框未选中...!

javascript - 日期格式在 angularjs 中不起作用

javascript - AngularJs 重新加载页面而不是仅加载 View

javascript - 在 angularjs 中设置选择选项值/id 以与 ajax/php 插入一起使用

javascript - 取消选中复选框时不隐藏文本区域

javascript - jQuery - 复选框未被选中

javascript - p5.j​​s:为上传的图片添加过滤器

javascript - Javascript 与 Haskell 中的 Eratosthenes 筛选

javascript - 使用实例模式时遇到问题