1

The title of this question may be misleading, but I could not think of a better way to word it.

Let me get to the point. I am using buttons to filter a table of statuses. If I have button A and button B active, I only want to display A and B, the rest are hidden. Currently I can only have it display either A or B, not both.

var aSelected = [];
$(".admin_view #filterTable tr td button").on("click", function() {
    $(this).toggleClass("selected"); //class to show user button is active
    //Add to array if not in already
    if ($.inArray(this.id,aSelected) == -1) {
        aSelected.push(this.id);
    //Remove from array if button is not active
    } else if ($.inArray(this.id,aSelected) >= 0) {
        aSelected.splice(aSelected.indexOf(this.id), 1);
    }
    //Show all if array is empty
    if (!aSelected.length) {
        $(".admin_view #applicantTable tbody tr").each(function() {
            $(".admin_view #applicantTable tbody tr").removeClass("hidden");
        });
    }
    //This is the section I need help with
    $.map(aSelected, function(a) {
        $(".admin_view #applicantTable tbody tr").not("."+a).addClass("hidden");
        $(".admin_view #applicantTable tbody tr."+a).removeClass("hidden");
    });
});

I need to find a way to compare against the entire array, not against one piece.

1 Answer 1

1

Currently, you hide all other elements for every item in aSelected, instead of only at the beginning. If you instead first hide everything and then show the elements you want, it should work:

//This is the section I need help with
$(".admin_view #applicantTable tbody tr:not(.hidden)").addClass("hidden");
$.map(aSelected, function(a) {
    $(".admin_view #applicantTable tbody tr.hidden."+a).removeClass("hidden");
});
Sign up to request clarification or add additional context in comments.

1 Comment

I had a feeling it was simply an issue with my logic. This answer works perfectly, thank you.

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.