简体   繁体   English

如何在 AG-Grid 中的多列上设置 OR 过滤器?

[英]How to set an OR filter on multiple columns in AG-Grid?

Let's say in the example below, instead of having a radio button for 'Between 30 and 50', I'd like to have a radio button for filtering two columns using 'OR' logic.假设在下面的示例中,我想要一个用于使用“OR”逻辑过滤两列的单选按钮,而不是“30 和 50 之间”的单选按钮。


(I know this is a completely unrealistic example but for the sake of learning concepts) I'd like to display the rows where: (我知道这是一个完全不切实际的例子,但为了学习概念)我想显示以下行:

  • Country = United States国家 = 美国

Or或者

  • Year = 2012年 = 2012

So for example, the following rows would be displayed:例如,将显示以下行:

  • Country = United States and Year = 2003国家 = 美国,年份 = 2003
  • Country = Canada and Year = 2012国家 = 加拿大,年份 = 2012
  • Country = United States and Year = 2012国家 = 美国,年份 = 2012

So in essence, any given row only needs to fulfill one of the two requirements above.所以本质上,任何给定的行只需要满足上述两个要求之一。


var gridOptions = {
    defaultColDef: {
        filter: true
    },
    columnDefs: columnDefs,
    rowData: null,
    animateRows: true,
    isExternalFilterPresent: isExternalFilterPresent,
    doesExternalFilterPass: doesExternalFilterPass
};

var ageType = 'everyone';

    function isExternalFilterPresent() {
    // if ageType is not everyone, then we are filtering
    console.log('test');
    return ageType != 'everyone';
}

function doesExternalFilterPass(node) {

    console.log(node.data);
    switch (ageType) {
        case 'below30': return node.data.age < 30;
        case 'between30and50': return node.data.age >= 30 && node.data.age <= 50;
        case 'above50': return node.data.age > 50;
        case 'dateAfter2008': return asDate(node.data.date) > new 
Date(2008,1,1);
        default: return true;
    }
}

The full example of the plunker is here: https://plnkr.co/edit/cJaktv86hhptsP3pYQf5?p=preview plunker 的完整示例在这里: https ://plnkr.co/edit/cJaktv86hhptsP3pYQf5?p = preview

Just change the condition checked in the doesExternalFilterPass method like so.只需像这样更改在 dosExternalFilterPass 方法中检查的条件。

function doesExternalFilterPass(node) {
  console.log(node);
    switch (ageType) {
        case 'below30': return node.data.age < 30;
        case 'between30and50': return node.data.year === 2012 || node.data.country === 'United States';
        case 'above50': return node.data.age > 50;
        case 'dateAfter2008': return asDate(node.data.date) > new Date(2008,1,1);
        default: return true;
    }
}

Here's a plunker or the working demo: https://plnkr.co/edit/QfJq59MJA7FmiAg4C4Zb?p=preview这是一个plunker或工作演示: https ://plnkr.co/edit/QfJq59MJA7FmiAg4C4Zb?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM