【问题标题】:Jquery Filter table rows by multiple checkboxesJquery通过多个复选框过滤表行
【发布时间】:2016-03-05 02:25:17
【问题描述】:

我正在尝试使过滤器适用于表格。我想要的是复选框来过滤(隐藏)行,基于它们是否被选中以及值是否存在于表格单元格中。我让它有点工作,但它只会对其中一个复选框进行排序。此外,如果要过滤的表格单元格中有多个值,则该行将被隐藏。想法?预先感谢您的任何帮助。出于某种原因,我真的在为此苦苦挣扎!

这是我一直在使用的代码:

$("input:checkbox").click(function () {
var showAll = true;
$('tr').not('.first').hide();
$('input[type=checkbox]').each(function () {
    if ($(this)[0].checked) {
        showAll = false;
        var status = $(this).attr('rel');
        var value = $(this).val();            
        $('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show();

    }
});
    if(showAll){
    $('tr').show();
}
});

标记如下(请原谅一些混乱,它的 Wordpress)

<div class="grants-filter">
<h3 class="filter-title">Filter Scholarships</h3><br/>
<h3>Year of Study:</h3>
<input type="checkbox" name="chx" rel="status" value="Freshman">Freshman
<input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore
<input type="checkbox" name="chx" rel="status" value="Junior">Junior
<input type="checkbox" name="chx" rel="status" value="Senior">Senior
<br/><br/><h3>Duration:</h3>
<input type="checkbox" rel="duration" value="Summer">Summer
<input type="checkbox" rel="duration" value="Semester">Semester
<input type="checkbox" rel="duration" value="Full Year or More">Full Year or More


</div>  

<div class="grants-listing">


<table border="1" id="table" class="grants-table">
<thead>
<tr class="first">
<th>Title</th>
<th>Description</th>
<th>Field</th>
<th>Duration</th>
<th>Year of Study</th>
<th>Other</th>
<th>Procedure</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$grargs = array ( 'post_type' => 'scholarship');
$grant_query = new WP_Query( $grargs );
while ( $grant_query -> have_posts() ):
$grant_query -> the_post(); ?>
<tr class="grant-detail">

<td>
<?php the_title(); ?>
</td>
<td><?php echo wp_trim_words(get_the_content(), 25 ); ?></td>
<td class="fields" rel="<?php echo get_field('fields'); ?>"><?php echo the_field('fields'); ?></td>
<td class="duration" rel="<?php echo the_field('desired_duration'); ?>"><?php echo the_field('desired_duration'); ?></td>
<td class="status" rel="<?php echo the_field('year-of-study'); ?>"><?php echo the_field('year-of-study'); ?></td>
<td class="other" rel="<?php echo the_field('other'); ?>"><?php echo the_field('other'); ?></td>
<td class="procedure" rel="<?php echo the_field('procedure'); ?>"><?php echo the_field('procedure'); ?></td>
<td class="url"><a href="<?php echo get_field('url'); ?>"><?php echo get_field('url'); ?></a></td>
</tr>
<?php endwhile;
wp_reset_postdata(); //reset the first query
?>

</tbody>
</table>

【问题讨论】:

    标签: javascript jquery wordpress jquery-ui filtering


    【解决方案1】:

    我认为你只是在调用第一个复选框 [0]

    if ($(this)[0].checked) {
    

    通过插入类似的东西来调用所有的东西

    $('input[type=checkbox]').each(function () {
               if (this.checked) {
                   console.log($(this).val()); 
               }
    });
    

    【讨论】:

      【解决方案2】:

      您的代码运行良好,这是一个简化版本

      $("input:checkbox").click(function() {
        var showAll = true;
        $('tr').not('.first').hide();
        $('input:checkbox:checked').each(function() {
          showAll = false;
          var status = $(this).attr('rel');
          var value = $(this).val();
          $('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show();
        });
        if (showAll) {
          $('tr').show();
        }
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <div class="grants-filter">
        <h3 class="filter-title">Filter Scholarships</h3>
        <br/>
        <h3>Year of Study:</h3>
        <input type="checkbox" name="chx" rel="status" value="Freshman">Freshman
        <input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore
        <input type="checkbox" name="chx" rel="status" value="Junior">Junior
        <input type="checkbox" name="chx" rel="status" value="Senior">Senior
      </div>
      
      <div class="grants-listing">
      
      
        <table border="1" id="table" class="grants-table">
          <thead>
            <tr class="first">
              <th>Title</th>
              <th>Description</th>
              <th>Fields</th>
              <th>Status</th>
              <th>Procedure</th>
            </tr>
          </thead>
          <tbody>
            <tr class="grant-detail">
              <td>
                Name
              </td>
              <td>Description</td>
              <td class="fields" rel="Freshman">Freshman</td>
              <td class="status" rel="Freshman">Freshman</td>
              <td class="procedure" rel="Freshman">Freshman</td>
            </tr>
            <tr class="grant-detail">
              <td>
                Name
              </td>
              <td>Description</td>
              <td class="fields" rel="Sophmore">Sopho</td>
              <td class="status" rel="Sophmore">Sopho</td>
              <td class="procedure" rel="Sophmore">Sopho</td>
            </tr>
            <tr class="grant-detail">
              <td>
                Name
              </td>
              <td>Description</td>
              <td class="fields" rel="Junior">Junior</td>
              <td class="status" rel="Junior">Junior</td>
              <td class="procedure" rel="Junior">Junior</td>
            </tr>
          </tbody>
        </table>

      【讨论】:

      • 感谢您的快速响应!我遇到的问题是,表中的某些 td 将具有多个值(例如,freshman、sophmore 和 Junior)。例如,选择 Sophmore 将不会显示具有多个值的行,即使 sophmore 是其中一个值。想法?
      • 关于如何让过滤器识别该值是否存在于 td 中的多个值中的任何想法?
      • 对不起,我无法理解您的意思,您能否提供一个示例输出 html(不是带有 php 的那个),td 中的多个值是什么意思?会不会像&lt;td class="status" rel="Freshman, Sophomore"&gt;Freshman, Sophomore&lt;/td&gt;
      猜你喜欢
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 2019-05-05
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多