通过过滤表进行Jquery实时搜索

问题描述 投票:0回答:1

我正试图通过一个带有过滤器的表格进行实时搜索。 当过滤器被应用时,我希望只搜索当前显示的行。 目前搜索和过滤器都是自己工作的。 我试过使用JQuery的visiblehidden选择器只在可见的行中迭代,但这不起作用,因为被隐藏的行在进一步搜索时不会变得可见。 如何让它们一起工作? JSfiddle。https:/jsfiddle.netL09u3z4p

JS

$(document).ready(function () {

    // Live search function through user input
    $("#search").keyup(function () {
        var value = $(this).val().toLowerCase().trim();

        // Tried searching through visible rows - does not work - ex $("#indexTable tbody tr").not(":hidden").each...
        $("#indexTable tbody tr").each(function (index) {

            var row = $(this);
            var name = row.find('td').eq(0).text().toLowerCase().trim();
            var location = row.find('td').eq(1).text().toLowerCase().trim();

            if (name.indexOf(value) == -1 && location.indexOf(value) == -1) {
                row.hide();
            }
            else {
                row.show();
            }
        });
    });

    // Map regions to cities
    const regions = {
        'central': ['Chicago', 'Madison', 'Dallas'],
        'east': ['New York', 'Boston'],
        'west': ['Seattle', 'Los Angeles'],
    }

    // Table filters through checkboxes
    $("input[type='checkbox']").change(function () {
        var locations = [];
        var hideNoAges = $('#hideAge').is(":checked")

        // Get ids of each region checkbox checked
        $(".location:input[type='checkbox']").each(function () {
            if ($(this).is(":checked")) {
                locations.push($(this).attr('id'));
            }
        })

        // Get list of all cities to be included in filter
        var cities = locations.map(function (i) {
            return regions[i].join();
        }).join().split(",");

        if (cities == "" && !hideNoAges) { // if no filters selected, show all items
            $("#indexTable tbody tr").show();
        } else { // otherwise, hide everything...
            $("#indexTable tbody tr").hide();

            $("#indexTable tbody tr").each(function () {
                var show = false;
                var row = $(this);

                if (hideNoAges) {
                    if (row.find('td').eq(2).text() == "Unknown") {
                        show = false;
                    } else {
                        show = true;
                        if (cities != "") {
                            cities.forEach(function (city) {
                                if (row.find('td').eq(1).text() != city) {
                                    show = false;
                                }
                            });
                        }
                    }
                }

                cities.forEach(function (city) {
                    if (row.find('td').eq(1).text() == city) {
                        show = true;
                        if (hideNoAges && row.find('td').eq(2).text() == "Unknown") {
                            show = false;
                        }
                    }
                })
                if (show) {
                    row.show();
                }
            })
        }
    })
})

超文本标记语言

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
    <table id="indexTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>Location</th>
                <th>Age</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Bob</td>
                <td>Chicago</td>
                <td>24</td>
            </tr>
            <tr>
                <td>Mike</td>
                <td>New York</td>
                <td>Unknown</td>
            </tr>
            <tr>
                <td>Sarah</td>
                <td>Seattle</td>
                <td>30</td>
            </tr>
            <tr>
                <td>Bill</td>
                <td>Los Angeles</td>
                <td>51</td>
            </tr>
            <tr>
                <td>Gary</td>
                <td>Boston</td>
                <td>37</td>
            </tr>
            <tr>
                <td>Melissa</td>
                <td>Madison</td>
                <td>Unknown</td>
            </tr>
            <tr>
                <td>Greg</td>
                <td>Dallas</td>
                <td>61</td>
            </tr>
        </tbody>
    </table>
    <h5>Search</h5>
    <div>
        <label for="search">Search</label>
        <input type="text" id="search">
    </div>
    <h5>Age Filter</h5>
    <label for="hideAge">Hide unknown ages</label>
    <input type="checkbox" id="hideAge">
    <h5>Region Filter</h5>
    <div>
        <label for="east">East</label>
        <input type="checkbox" id="east" class="location">
    </div>
    <div>
        <label for="central">Central</label>
        <input type="checkbox" id="central" class="location">
    </div>
    <div>
        <label for="west">West</label>
        <input type="checkbox" id="west" class="location">
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</body>

</html>
javascript jquery
1个回答
0
投票

你可以尝试为被过滤器应用的行添加一个类,然后用类选择器在这些行上进行搜索(比如说 .filtering). 反之亦然,你也可以用搜索来做(也许是 .searching),并对其应用过滤器。

© www.soinside.com 2019 - 2024. All rights reserved.