build a dynamic multi parameter filter list with AngularJS

83 views
Skip to first unread message

ste...@gmail.com

unread,
Jun 8, 2014, 11:40:23 AM6/8/14
to ang...@googlegroups.com

I have a json wine list, and my wish is to build a code with AngularJS to filter the results

the json file

[
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" },
{ "name": "Wine a", "type": "white", "country": "italie", "style": "medium" },
{ "name": "Wine a", "type": "white", "country": "france", "style": "light" },
{ "name": "Wine a", "type": "rose", "country": "usa", "style": "strong" },
{ "name": "Wine a", "type": "champagne", "country": "chili", "style": "medium" },
{ "name": "Wine a", "type": "red", "country": "brazil", "style": "strong" },
{ "name": "Wine a", "type": "red", "country": "france", "style": "strong" }

]

the check-boxes

<div ng-controller="MainCtrl">
    <input ng-model="red" type="checkbox" >red</input>
    <input ng-model="white" type="checkbox" >white</input>
    <input ng-model="rose" type="checkbox" >rose</input>
    <input ng-model="champagne" type="checkbox" >champagne</input>

    <input ng-model="france" type="checkbox" >france/input>
    <input ng-model="italie" type="checkbox" >italie</input>
    <input ng-model="brazil" type="checkbox" >brazil</input>
    <input ng-model="chili" type="checkbox" >chili</input>

    <input ng-model="strong" type="checkbox" >strong</input>
    <input ng-model="medium" type="checkbox" >medium</input>
    <input ng-model="light" type="checkbox" >light</input>

</div>

my wish

My wish is to build a dynamic multi parameter filter list,the result of the list would match with the values of the check-boxes.

  • if i check red and strong, would appears in the list, only the wines from the json list with these parameter (a list of red and strong wines).

  • if i check red, white and france, would appears in the list, only the wines from the json list with these parameter ( a list of red and white wines from france).

i tried many solution, with no results. It seems to be hard to code!!

kristijan budimir

unread,
Jun 9, 2014, 3:02:12 AM6/9/14
to ang...@googlegroups.com
pure javascript:
var wines = [];
function filterrData(wines, name, type, country, style) {
    var filteredData = wines.filter(function(entity){
        // just nest all of the results here
        var wineName = name || entity.name;
        if(entity.name === wineName) return true;
        else return false
    });

Stéphane Cabaret

unread,
Jun 9, 2014, 9:54:38 AM6/9/14
to ang...@googlegroups.com
Kristijan,

Thanks for your javascript code, it helps me to build the filter function and to understand better the filter system.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/T_S3RjBohPs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages