0

I have a JSON File with Tasks. They are structured as follows:

[
{
    "id":"1",
    "name":"task1",
    "tags":["tag1","tag2","tag3"]
},
{
    "id":"2",
    "name":"task2",
    "tags":["tag4","tag2","tag5"]
},
{
    "id":"3",
    "name":"task3",
    "tags":["tag3"]
}
]

Here's a Plunkr for what I want http://plnkr.co/edit/kMhiHDyybHYxGfV7W39z?p=preview

Basically, this is what I want : http://jsfiddle.net/TahmidTanzim/N9Vqk/

I've looked through various SO questions and they all suggest I am doing it right. However I don't understand where I am going wrong. I want only those tasks that contain the selectedTag to be displayed in the red list.

Thanks!

1 Answer 1

1

Just change filters to compare inner tags using arrays.

myapp.filter('tagFilter',function()
{
    return function(Data,selectedTags)
    {
        if(selectedTags.length===0) return Data;
        var tempData=[];
        for(var i  in Data) {
            for(var z in Data[i].tags) {
                for(var k in selectedTags) {
                    var value = selectedTags[k];
                    if(value == Data[i].tags[z]) {
                        tempData.push(Data[i]);
                        break;             
                    }
                }
            }
        }
        return tempData;
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

The Plunkr is working fine, but it seems that the filter in not able to get selectedTags array. I tried console.log(selectedTags) in the filter and it comes up empty in my actual app!
I have just edited the code, I have tested in Plunker. Could you try again ?
Did you try to change your angular version (using the same as plunker) ?
The plunker seems to work. But In my actual app, I am unable to pass selectedTags to the filter. While trying to print out selectedTags in the filter, I get an empty array. However, I can print out the selectedTags array from SetSetectedTags() function.
You are correct. I am using an older version of AngularJS!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.