滤波angularJs多操作 [英] Filtering Multiple Operation in angularJs
问题描述
我要筛选基于两个值。我试图用下列功能
I have to filter based on two values. I am trying with following functionality
JSON数据是,
{"homeAddress":"26, New Street, Bangalore",
"officeAddress":"31, Old Office Street, Bangalore"
},{
"homeAddress":"27, Neww Street, Bangalore",
"officeAddress":"30, Old Office Street, Bangalore"
}
var locationFilter = {officeAddress: location};
var locationFilter1 = {homeAddress: location};
filterData = $filter('filter')(data, locationFilter);
filterData = $filter('filter')(data, locationFilter1);
return filterData;
通过使用上述功能。它已筛选第二个选项只。
By using above functionality. It have filtered second option only.
因此,如何与这两个条件与上述造型过滤和如何使用OR,AND Opeartion在于此。
So how to filter with the two condition with the above styling and how can i use OR, AND Opeartion in this.
当我过滤功能例如:26安培; 31是过滤器正常。但这里27没有过滤
When i filtering For ex: 26 & 31 is filter correctly. But here 27 is not filtering
推荐答案
有关和
的功能(例如,当你想只显示有两个<$ c中的元素$ C> officeAddress 和是homeAddress
等于位置
),只需要创建一个合并对象,它传递到过滤器
:
For AND
functionality (i.e., when you want to show only the elements that have both officeAddress
and homeAddress
equal to location
), just create a combined object and pass it into filter
:
var bothLocationsCheck = {
officeAddress: location,
homeAddress: location
};
filterData = $filter('filter')(data, bothLocationsCheck);
这会明显地给你'和';为OR,只是CONCAT两个结果:
That'll obviously give you 'AND'; for 'OR', just concat two results:
filteredData = $filter('filter')(data, locationFilter).concat(
$filter('filter')(data, locationFilter1));
...但在这种情况下,你必须从结果中删除重复。
... but in this case you'll have to remove the duplicates from the result.
不过,我想preFER另一种方法:实现这种过滤功能本身的操作。例如:
Still, I'd prefer another approach: implementing this kind of operations within filtering function itself. For example:
filteredData = $filter('filter')(data, function(el) {
return el.homeAddress.indexOf(location) !== -1
|| el.officeAddress.indexOf(location) !== -1
});
这是相当琐碎,这个功能改变从或
到和
功能。
这篇关于滤波angularJs多操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!