简体   繁体   English

jQuery - 使用类名数组过滤表行

[英]jQuery - Filtering table rows using an array of class names

I have a table with some values and a filter option where the user can select multiple values and filter the table.我有一个包含一些值的表和一个过滤器选项,用户可以在其中选择多个值并过滤表。 What I am trying to achieve is to have a filter with numbers from 1 to 10, and table tr with class names filter_1 , filter_2 , filter_3 etc. when I choose number 1 from filter and click on it, it will show only tr with class filter_1 .我想要实现的是拥有一个数字从 1 到 10 的过滤器,以及带有类名filter_1filter_2filter_3等的表tr 。当我从过滤器中选择数字 1 并单击它时,它只会显示带有类的 tr filter_1 My code is below.我的代码如下。

HTML: HTML:

<select multiple id="filterNumber">
<option value="1">1</option><option value="1">2</option><option value="1">3</option>
</select>
<button class="filterBtn">Filter</button>

Table:桌子:

<table>
<thead><tr><th>Name</th></tr></thead>
<tbody>
<tr class="filter_1"><td>A</td></tr>
<tr class="filter_5"><td>B</td></tr>
<tr class="filter_1"><td>C</td></tr>
</thead>
</table>

jQuery: jQuery:

$(document).on('click','.filterBtn',function(){
let filterNumber = $('#filterNumber).val();

//loop through this numbers and hide tr without this class name 
});

I know how to pass these values through AJAX into DB and display the result, but I am trying to learn more like doing from the front-end only that makes my app more faster.我知道如何通过 AJAX 将这些值传递到 DB 并显示结果,但我正在尝试学习更多的东西,比如从前端做的事情,只是为了让我的应用程序更快。 But I don't know how to filter this using JavaScript or jQuery.但我不知道如何使用 JavaScript 或 jQuery 过滤它。

Add hideable or similar so you don't select the wrong thing.添加hideable或类似的,这样你就不会选择错误的东西。

<table>
<thead><tr><th>Name</th></tr></thead>
<tbody>
<tr class="filter_1 hideable"><td>A</td></tr>
<tr class="filter_5 hideable"><td>B</td></tr>
<tr class="filter_1 hideable"><td>C</td></tr>
</thead>
</table>

Hide them all, then show the visible one(s):全部隐藏,然后显示可见的:

$(document).on('click','.filterBtn',function(){
  const filterNumbers = $('#filterNumber').val().toArray().map(item => item.value);

   $('.hideable').addClass( "hidden" ); 

  filterNumbers.forEach(num => 
    $(`.filter_${number}`).each(function() {
      $(this).removeClass("hidden"); 
    })
  )
}

You need to add a class to your CSS with display:none for .hidden您需要使用display:none.hidden添加一个类到您的 CSS

Multi-select values: https://stackoverflow.com/a/55929605/1758461多选值: https : //stackoverflow.com/a/55929605/1758461

jQuery each() : https://api.jquery.com/each/#each-function jQuery each() : https://api.jquery.com/each/#each-function

On the click of your button, you take the desired value, as you did.单击按钮后,您就可以像以前一样获取所需的值。 After that, you can use jQuery to add hide class to each tr that does not have the filtered class, with tr:not(.className) , as shown in the example below:之后,您可以使用 jQuery 为每个没有过滤类的tr添加hide类,使用tr:not(.className) ,如下例所示:

$(document).on('click', '.filterBtn', function(){
 let filterNumber = $('#filterNumber').val();
 let className = 'filter_' + filterNumber;
 $('table tbody tr:not(.'+className+')').addClass('hide');
});

Note that if you filter more than once, you will also need to include a way to remove the hide class if you've previously filtered something else.请注意,如果您进行了多次过滤,并且您之前已经过滤了其他内容,则还需要包含一种删除隐藏类的方法。 So you would just add所以你只需添加

$('table tr.'+className+'').removeClass('hide');

to remove hide class if the filter element has been previously filtered out.如果过滤器元素先前已被过滤掉,则删除隐藏类。

Edit:编辑:

Since this is a multiple select, I would first apply hide class to each element, then loop through selected values and remove hide from the ones whose class matches the selected number.由于这是一个多选,我首先将hide类应用于每个元素,然后循环选择的值并从类与所选数字匹配的值中删除hide

$(document).on('click', '.filterBtn', function(){
   $('table tbody').find('tr').addClass('hide');
   let filterNumbersArray = $('#filterNumber').val();
   for(i=0; i<filterNumbersArray.length; i++){
      let className = 'filter_' + filterNumbersArray[i];
      $('table tbody tr.'+className+'').removeClass('hide');
   }
});

Select all the thdboy rows and hide them.选择所有 thdboy 行并隐藏它们。 Select all the rows with the class and show them选择该类的所有行并显示它们

 $(document).on("click", '.filterBtn', function() { const filterNumber = $('#filterNumber').val(); // create a comma seperated list of the class names to show var filters = filterNumber.map(function (num) { return '.filter_' + num }).join() var trs = $('table tbody tr') // select all the rows // if we have filters run the code if (filters.length) { trs .hide() // hide them .filter(filters) // find the rows with the class[es] .show() // show them } else { // no filters, just show everything trs.show() } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select multiple id="filterNumber"> <option value="1">Option 1</option> <option value="3">Option 3</option> <option value="5">Option 5</option> </select> <button type="button" class="filterBtn">Filter</button> <table> <thead> <tr> <th>Name</th> </tr> </thead> <tbody> <tr class="filter_1"> <td>A1</td> </tr> <tr class="filter_1"> <td>A2</td> </tr> <tr class="filter_5"> <td>B</td> </tr> <tr class="filter_3"> <td>C</td> </tr> </tbody> </table>

If your rows have classes associated with the selection values, then you can use classes to select which rows to hide and show.如果您的行具有与选择值关联的类,那么您可以使用类来选择要隐藏和显示的行。 Notice how I select all the rows first to remove the class hide .请注意我如何首先选择所有行以删除类hide Then, I add hide to all rows that do not share the glass selected.然后,我将隐藏添加到所有不共享所选玻璃的行。

     jQuery("#c_selector").change(function(){
        jQuery('table tbody tr').removeClass('hide');
        var cat_id = jQuery("#c_selector option:selected").val();
        console.log(cat_id)
        jQuery('table tbody tr:not(.itemtext.c_id'+cat_id+')').addClass('hide');
    })

Maybe this is helpful to someone!也许这对某人有帮助!

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

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