0

I have a group of checkboxes that when clicked add an attribute to an array. The problem is i want to filter through links that have a specific attribute that i just added to that array.

So i have an array that could have ["filter-F_0", "filter-F_1"] etc... Now the code below only works if only one checkbox is clicked so my array would be ["filter-F_0], but once there are more i'm lost. Should i be doing this in a loop instead?

var arr = filterArray.join(",")

$(".appLink").filter("[" + arr + "]").css({
                        borderColor : 'red',
                        borderWidth : '1px',
                        borderStyle : 'solid'
                    });

I set up a simple example below to hopefully help explain what i am trying to do

http://jsfiddle.net/QCeXV/21/

2
  • because the selector [filter-F_0,filter-F_0] is invalid Commented Dec 31, 2013 at 2:16
  • You could use classes too, like in this example. Commented Dec 31, 2013 at 2:33

2 Answers 2

1

looks like you are looking for a or condition based filter. Now you are creating a filter condition like [filter-F_0,filter-F_0] which is invalid instead you might need [filter-F_0], [filter-F_0]. so try

var arr = $.map(filterArray, function (val) {
    return '[' + val + ']'
}).join(",")
Sign up to request clarification or add additional context in comments.

2 Comments

is there a ":not" version to the filter? '$(".appLink").filter(":not"+arr).css()'
@Bardsworth you can use .not() instead of .filter() like $(".appLink").not(arr)
1

I'd suggest using this instead: http://blog.stevensanderson.com/2013/12/03/knockout-projections-a-plugin-for-efficient-observable-array-transformations/

It lets you filter an observable and as you change your selection it will update the view. It might be a better approach than handling it manually yourself.

But just to clarify, you use filter like this:

return this.products().filter(function(product) {
    return product.isSelected();
});

Where this.products() is an array of objects and the function you pass in takes an item (product) from the array and returns true/false whether it is included in the output of the filter function. What you're doing is looping inside that function to see if the current row's filters array has the value.

$.map() is similar in function to filter() as mentioned in another answer...

1 Comment

I like where this is going, and in my next endeavors i will probably proceed down this path. Thanks for the info!

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.