简体   繁体   中英

How can I add to my table filter to allow for multiple checkbox selections, as well as filtering from a dropdown?

I've got a table that can be filtered by multiple checkboxes, as well as by a "select" dropdown. Essentially, what I want to be able to do is click on multiple checkboxes in order to find each row containing that class, so class 1 and 3, or example, and then filter it by the location.

I've gotten really close at this point, where I can select both the location (which is also a class, two letters for state abbreviations), and a single class from the checkboxes, but I want people to be able to compare multiple classes from the checkboxes.

The problem is something I understand, but can't seem to figure out how to fix. The code adds each class to the end of the "tr". This means if you apply 2 filters from the checkboxes, it finds nothing, instead of finding everything from each filter. While this behavior is perfectly functional for 1 checkbox and the Dropdown, meaning it will only give me Filter 1 values from Tennessee, I want it to be able to give me Filter 2, Filter 3, and Filter 4 at the same time, if I so choose.

Any ideas on how to alter this to make it allow multiple checkbox selections?

By the way, I'm very limited in what JavaScript/jQuery plugins I can use, which is why I'm fighting so hard to use tableSorter with this solution.

My HTML:

 <form name="FilterForm" id="FilterForm" action="" method="">
    <input type="checkbox" name="filterStatus" value="1" /><label for="filter_1">Filter 1</label>
    <input type="checkbox" name="filterStatus" value="2" /><label for="filter_2">Filter 2</label>
    <input type="checkbox" name="filterStatus" value="3" /><label for="filter_3">Filter 3</label>
    <input type="checkbox" name="filterStatus" value="4" /><label for="filter_4">Filter 4</label>
    <select type="dropdown" name="filterStatus" id="chooseState" class="filter">
      <option value="ZZ">--Choose State--</option>
      <option value="TN">Tennessee</option>
      <option value="MO">Missouri</option>
      <option value="NV">Nevada</option>
      <option value="IA">Iowa</option>
    </select><label>State</label>
       </form>

<table id="StatusTable">
     <tbody>                        
       <tr class="1 TN">
       <td class="Name"></td>
       <td class="ID"></td>
       <td class="Type"></td>
       <td class="Location">City, Tennessee</td>
       <td class="Class"></td>
       <td class="Received"></td>
       <td class="Action"></td>
       <td class="Status">Active</td>
     </tr>
</tbody>
</table>

JavaScript:

    $("input[name='filterStatus'], select.filter").change(function () {
var classes = "";
var stateClass = ""

    $("input[name='filterStatus']").each(function() {
    if ($(this).is(":checked")) {
    classes += "." + $(this).val();
    }
});

$("select.filter").each(function() {
if ($(this).val() != 'ZZ') {
stateClass += "." + $(this).val();
}
});

if (classes == "" && stateClass == "") {
// if no filters selected, show all items
$("#StatusTable tbody tr").show();
} else {
// otherwise, hide everything...
$("#StatusTable tbody tr").hide();

    // then show only the matching items
rows = $("#StatusTable tr" + classes + stateClass);
if (rows.size() > 0) {
rows.show();
}
}

});

Since you know the problem, ill skip the explication and jump to the solution.

Use the .filter() method mixed with an array.

Instead of expending a string, push the class name into an array :

var classes = [];

$("input[name='filterStatus']").each(function() {
    if ($(this).is(":checked")) {
        classes.push('.'+$(this).val());
    }
});

Then when selecting the row, use .filter() just like that :

rows = $("#StatusTable tr" + stateClass).filter(classes.length ? classes.join(',') : '*')

Then everything work fine!

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM