Checkboxes hide groups of rows from a table

人盡茶涼 提交于 2019-12-13 05:20:23

问题


I have a table that I would like to dynamically hide/reveal rows in, based on checkboxes at the top.

I would like to do this without jQuery, just vanilla Javascript.

I have seen numerous methods on this site and others, but always snippets of code,not complete working examples.

I don't want to give each row a separate div name, or whatever, I am assuming there is a way I can do this with a common class for the 3 types of rows I want to hide/reveal.

Can anyone please show me a working example which allows checkboxes to hide/show multiple rows from a table with vanilla Javascript?


回答1:


Given HTML like the following:

<table>
    <thead>
        <tr>
            <td>
                <label>
                    <input type="checkbox" class="show" value="north" checked />North</label>
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    <input type="checkbox" class="show" value="south" checked />South
                </label>
            </td>
        </tr>
        <tr>
            <td>
                <label>
                    <input type="checkbox" class="show" value="outOfArea" checked />Out of area
                </label>
            </td>
        </tr>
    </thead>
    <tbody>
        <tr class="north">
            <td>North One</td>
        </tr>
        <tr class="north">
            <td>North Two</td>
        </tr>
        <tr class="outOfArea">
            <td>Out-of-area One</td>
        </tr>
        <tr class="south">
            <td>South One</td>
        </tr>
        <tr class="south">
            <td>South Two</td>
        </tr>
        <tr class="north">
            <td>North Three</td>
        </tr>
        <tr class="north">
            <td>North Four</td>
        </tr>
        <tr class="south">
            <td>South Three</td>
        </tr>
        <tr class="outOfArea">
            <td>Out-of-area Two</td>
        </tr>
    </tbody>
</table>

The following jQuery seems to do as you seem to describe:

$('thead input[type=checkbox]').change(function(){
    var self = this;
    $(self).closest('table').find('tbody tr').filter('.' + self.value).toggle(self.checked);
});

JS Fiddle demo.

As it seems that you'd prefer a plain-JavaScript approach, I'd suggest the following (to work on the same HTML as posted above):

function toggle (e) {
    var self = e.target,
        toggleClass = '.' + self.value,
        toToggle = document.querySelectorAll(toggleClass);
    for (var i = 0, len = toToggle.length; i < len; i++) {
        toToggle[i].style.display = self.checked ? 'table-row' : 'none';
    }
}

var thead = document.querySelector('thead');
thead.addEventListener('change', toggle);

JS Fiddle demo.

References:

  • jQuery:
    • Attribute-equals ([attribute="value"]) selector.
    • change().
    • closest().
    • find().
    • filter().
    • toggle().
  • Plain JavaScript:
    • document.querySelector().
    • document.querySelectorAll().
    • EventTarget.addEventListener().



回答2:


Looks like you are describing something along the lines of this example



来源:https://stackoverflow.com/questions/18190233/checkboxes-hide-groups-of-rows-from-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!