【问题标题】:Rails - Table <TR> - Add filter with a button / JqueryRails - 表 <TR> - 使用按钮 / Jquery 添加过滤器
【发布时间】:2016-09-23 11:52:14
【问题描述】:

我想通过单击按钮在我的表上实现过滤器

我在这里有一张桌子:

    <div class="table-container">
        <table class="table table-filter">
        <tbody>
         <% if current_user %>
         <% @book.each do |book| %>
         <tr data-status = "###">   
<!-- I would like to implement <%= book.style %> inside that data-status, to filter on the type of books   -->
           <td><%= book.name %></td>
           <td> <%= book.author %></td>
          </tr>
         </tbody>
         <% end %>
         <% end %>
     </table>
   </div>

当我点击按钮时要过滤的脚本

 <button type="button" class="btn btn-success btn-filter" data-target="novel">PC</button>

这是脚本:

<script>
$(document).ready(function () {   

    $('.btn-filter').on('click', function () {
      var $target = $(this).data('target');
      if ($target != 'all') {
        $('.table tr').css('display', 'none');
        $('.table tr[data-status="' + $target + '"]').fadeIn('slow');
      } else {
        $('.table tr').css('display', 'none').fadeIn('slow');
      }
    });
   });
</script>

我不知道如何在 Data-status = 中传递书籍类型的值,即使我输入了一个文本值,比如 Novel ,它也不会在表格上过滤。

我真的不知道我哪里错了,我正在寻求帮助。我需要使用 content_tag 吗?

【问题讨论】:

    标签: javascript jquery ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    你可以简单地:

    <tr data-status = "<%= book.style %>"> 
    

    【讨论】:

    • 是的,我正在尝试,它正在工作,但我真的不知道为什么当我点击那个按钮时我的表格是空的,也许是 JS 或我的 CSS 错误,谢谢你的确认 :)我要进一步调查
    • 我找到了解决方案,我必须使用 ID 而不是该数据状态上的字符串,当它写得不好时,例如全部大写,它不起作用。
    猜你喜欢
    • 2013-03-29
    • 2017-04-27
    • 1970-01-01
    • 2013-01-09
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多