简体   繁体   English

如何正确链接jquery选择器?

[英]How do I chain jquery selectors properly?

I'm trying to implement a quick search/filter function to my table using jquery. 我正在尝试使用jquery在我的表中实现快速搜索/过滤功能。 In essence I want to hide all the rows that don't have the string I'm looking for from a searchbox to be hidden. 本质上,我想隐藏所有没有我正在寻找的字符串的行,从搜索框中隐藏。

I have a dynamically created table and a text field used as the filter for the list. 我有一个动态创建的表和一个文本字段用作列表的过滤器。

Table: 表:

<table id="report-table" class="table">
<thead>
    <tr>
        <th class="">client</th>
        <th class="">coach</th>
        <th class="">groups</th>

    </tr>
</thead>

<tbody>
    <tr>
        <td class="name">John</td>
        <td class="coach">Peter </td>
        <td class="groups"> Skiers </td>
    </tr>
</tbody>

Ihave a function tied to the change event of the search text box. 我有一个与搜索文本框的更改事件关联的函数。 In this function I essentially want to choose all tr that do not contain the text string in name or coach column and add a class to them. 在这个函数中,我基本上想要选择在name或coach列中不包含文本字符串的所有tr,并为它们添加一个类。 I have tried many things but have not gotten the syntax right, how should it be written? 我尝试了很多东西,但没有正确的语法,应该如何编写?

hideSearch: function(e){

        console.log("hideSearch called");
        var searchValue = this.$el.find('.search-text').val();

        if(!searchValue ){
            console.log("hideSearch: empty search param");
            this.$el.find('tr').removeClass('hidden');
        }
        else{
            console.log("hideSearch: searched for: " + searchValue);
            //$('(#name, #groups):contains:not("'+searchValue+'")').parent().addClass('hidden');
            var selection =$('#name, #groups').('*:contains("'+searchValue+'")');

            console.log(selection);
            //console.log($('#name, #groups').('*:contains("'+searchValue+'")'));
            //$('(#name, #groups):contains("'+searchValue+'")').parent().addClass('hidden');
            //$('#name, #groups').('*:contains:not("'+searchValue+'")').parent().addClass('hidden');

        }

$('#name, #groups').('*:contains("'+searchValue+'")'); would basically try to access the property *:contains("foo") (assuming searchValue is "foo" ) of the object returned by $('#name, #groups') . 基本上会尝试访问$('#name, #groups')返回的对象*:contains("foo") (假设searchValue"foo" $('#name, #groups') I believe I don't have to say that jQuery objects don't have properties with such strange names. 我相信我不必说jQuery对象没有这些奇怪名字的属性。

First of all you have to give all the cells a common class instead of an ID. 首先,您必须为所有单元格提供一个公共而不是ID。 Then you should select all rows and see if either .name or .coach contain the search value. 然后,您应该选择所有行,并查看.name.coach包含搜索值。 Use .filter to get those for which neither cell matches: 使用.filter来获取两个都不匹配的那些:

$('#report-table > tbody > tr').filter(function() {
    return $(this).children('.name').text().indexOf(searchValue) === -1 &&
           $(this).children('.coach').text().indexOf(searchValue) === -1;
}).addClass('hidden');

The filter callback returns true if neither the .name cell nor the .coach cell contain the search value. 如果.name单元格和.coach单元格都不包含搜索值,则过滤器回调返回true Those rows for which the callback returns true are kept in the selection and are getting the class hidden added to them. 回调返回true那些行保留在选择中,并将hidden的类添加到它们中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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