How To Include A "string Includes" Function Inside A Angular Filter
I am working on filtering with checkboxes, My geojson has a property of status. the status is value looks like this: status : 'Act 3Q99' or status : 'Fut 3Q99' I need
Solution 1:
Here's some refactoring of @Mate's answer, primarily to adhere to the "Don't Repeat Yourself" principle:
var active = typeFilter.active;
var future = typeFilter.future;
var status = subdivision.properties.status;
var match = (active && future) ||
(active && !future && status.indexOf('Act') >= 0) ||
(future && !active && status.indexOf('Fut') >= 0);
if (match) {
filter.push(subdivision);
}
Note that once you have the boolean match
answer you could just use Array.filter
instead to produce your result instead of accumulating the results with .push
- it's supported on MSIE 9+, so it'll work on any browser that AngularJS supports, reducing your code above to:
functionStatusFilter() {
returnfunction(territorySubdivisions, typeFilter) {
return territorySubdivisions.filter(function(subdivision) {
var active = typeFilter.active;
var future = typeFilter.future;
var status = subdivision.properties.status;
return (active && future) ||
(active && !future && status.indexOf('Act') >= 0) ||
(future && !active && status.indexOf('Fut') >= 0);
});
}
}
Solution 2:
Your code looks strange. Don't you just need this:
if (typeFilter.active === true && typeFilter.future === true) {
filtered.push(subdivision);
} elseif (typeFilter.active === true && typeFilter.future === false && subdivision.properties.status.indexOf('Act') >= 0) {
filtered.push(subdivision);
} elseif (typeFilter.future === true && typeFilter.active === false && subdivision.properties.status.indexOf('Fut') >= 0) {
filtered.push(subdivision);
}
?
Post a Comment for "How To Include A "string Includes" Function Inside A Angular Filter"