gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:00:34 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com