Discussion:
[AngularJS] Angular JS custom filter not working
Yogesh N
2015-07-13 07:10:40 UTC
Permalink
Hi

I am filtering the customer list based on the query typed by the user in
the suggest box.

Here is the code to get filtered customer list.

$scope.getCustomers = function (query) {
customerList = ($filter('partyFilter')(customerList, query));
}


Here is the my custom filter.

angular.module('filterModule', [])
.filter("partyFilter", function() {

return function(partyList, query) {
var filteredList = [];
for (i = 0; i < partyList.length; i++) {
var fullName = partyList[i].fullName;
if (fullName.toLowerCase().includes(query.toLowerCase())) {
filteredList.push(partyList[i]);
}
}
return filteredList;
};
});

The customer list is empty when i execute this code. Can anyone please
suggest what is the issue in this code.
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Sander Elias
2015-07-13 07:52:06 UTC
Permalink
Hi Yogesh,

You are aware that String.include() is ES6, and is not available by default
in most browsers?
Also, a filter like this is also available as filter.filter, just pass an
object to it in stead of a string.

But if you want to make it yourself, you can try something like this:
angular
.module('filterModule', [])
.filter("partyFilter", function() {


return function(partyList, query) {
if (!angular.isArray(partList)) {
return partyList;
}
query = query.toLowerCase();
return partyList.filter(includes);


function includes(item,query) {
return item.fullName.toLowerCase().indexOf(query)>-1
}
};
});


Regards
Sander
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Yogesh N
2015-07-13 08:22:18 UTC
Permalink
Hi Sander,
Thank you for your help. I have looked at the docs of String.includes()
and gotto know it does support in most of the browsers.
I have used the code which u have replyd and my issue is solved.
Thanks & Regards
Yogesh
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Yogesh N
2015-07-13 08:26:45 UTC
Permalink
Hi Sander,
Thanks for the help. I have looked at the docs of String.includes() and got
to know it does not support in most of the browsers.
I have used the code which u have replyed and my issue is solved.


Thanks & Regards,
Yogesh
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Yogesh N
2015-07-13 11:56:56 UTC
Permalink
Hi Sander,
Can you please explain this :

partyList.filter(includes);

I did not understand because the includes() function will return boolean
value.

Now on the return value ie:

partyList.filter(true/false);

what will partyList get filtered by?
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Sander Elias
2015-07-13 12:41:48 UTC
Permalink
Hi Yogesh,

That is how .filter
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter>works.
For every function that returns true, the corresponding array element gets
added to the result.
see the link I put in for the details.

Regards
Sander
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Yogesh N
2015-07-13 12:56:57 UTC
Permalink
Hi Sander,

Thanks for the reply. I am looking into the Array.prototype.filter() docs.


Thanks & Regards
Yogesh
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Loading...