【问题标题】:Sort table data by columns in rails with bootstrap使用引导程序按rails中的列对表数据进行排序
【发布时间】:2015-07-17 20:01:53
【问题描述】:

编辑:如果可能的话,我更愿意使用引导程序来实现此功能,因为我的项目中有引导程序。看来我可能只是缺少在我的 rails 项目中利用 bootstrap 的 javascript 的东西。

单击列名时,表格应按该列名对数据进行排序。下表是:

我尝试按照at this website 显示的示例使用引导程序对数据进行排序,但它对我不起作用。我错过了什么?

我的 Gemfile 中的相关宝石:

#Gemfile
gem 'bootstrap-sass'
gem 'autoprefixer-rails'

CSS:

#app/assets/stylesheets/application.css.scss
@import "bootstrap-sprockets";
@import "bootstrap";
/*
 *= require_tree .
 *= require_self
 */

Javascript:

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

查看显示记录:

#app/views/index.html.erb
<h4>Listing Users</h4>

<table class=" table table-striped" data-sort-name="name" data-sort-order="desc">
  <thead>
    <tr>
      <th data-field="name" data-sortable="true">Name</th>
      <th data-field="age" data-sortable="true">Age</th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= user.age %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>

【问题讨论】:

  • 示例显示data-sortable="true" 被赋予&lt;th&gt;。尝试在您的代码中为&lt;th&gt; 提供data-sortable="true",并为&lt;td&gt; 删除它。
  • @Pavan 在将代码移到堆栈溢出时是一个拼写错误。不幸的是,它仍然不适合我。
  • 尝试将//= require jquery-ui/sortable 添加到您的apllication.js
  • @Pavan 我尝试安装 jquery-ui-rails gem 并在 javascript 和 css 文件中添加 jquery-ui 的东西,但仍然没有工作。

标签: ruby-on-rails ruby twitter-bootstrap


【解决方案1】:

感谢Hunter Stevens 的大量反馈。我为我找到的最佳选择是使用sorttable.js。我使用的过程如下:

  1. 将 gem 'jquery-turbolinks' 添加到您的 Gemfile 以修复 turbolink javascript 问题和 bundle install
  2. jquery.turbolinks 添加到 javascript 清单文件中:

    #app/assets/javascripts/application.js
    //= require jquery
    //= require jquery_ujs
    //= require jquery.turbolinks
    //= require bootstrap-sprockets
    //= require turbolinks
    //= require_tree .
    
  3. here to copy the code for sorttable.js

  4. app/assets/javascripts/shared/&lt;whatever_you_want_to_call_it&gt;.js 创建js 文件。如图所示使用$(document).ready 来修复turbo 链接问题。指定一个 IIFE(可选),然后在该 IIFE 中粘贴供应商代码:

    $(document).ready(function(){
      (function vendorTableSorter(){ 
        /* paste all the vendor code here */
      }());
    });// end document.ready
    
  5. 然后,只需将 sortable 类添加到您的表中,这使得所有列都可以排序:

    <table class=" table table-striped sortable">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
        </tr>
      </thead>
    
     <tbody>
       <% @users.each do |user| %>
         <tr>
          <td><%= user.name %></td>
          <td><%= user.age %></td>
        </tr>
       <% end %>
     </tbody>
    

【讨论】:

    【解决方案2】:

    快速 - sortTable.js

    警告:如果您打算使用分页,此方法将不起作用。

    更快的方法(需要零查询)涉及库sortTable.js。该网站在解释如何设置 HTML 文档方面做得很好。 (只需向列标题添加一些类。)您还可以启用不区分大小写的排序。

    假设您使用的是 Rails 4,或者拥有 turbolinks gem,我强烈建议遵循detailed gist with instructions 在您的环境类型中设置sortTable.js。 (完整的讨论可以在我的Q&A on using turbolinks with sorttable.js找到。)


    可靠 - 自定义控制器方法

    警告:此方法要求对每个页面浏览至少进行一次查询。

    此方法来自Railscast 228: Sortable Table Columns。阅读随附的代码几乎是您所需要的。此方法确实适用于分页。

    如果您选择关注Railscast for pagination,只需在 Ajax 部分之前停止,因为所涉及的 Javascript 已被弃用。其次,注意该截屏视频中可能存在的 SQL 注入。谢天谢地,它解释了如何避免它。

    【讨论】:

    • 我查看了您上面的所有链接。看起来sortTable.js 可能是一个选项。令人沮丧的是,bootstrap 已经带有许多很棒的 JavaScript 功能,包括表格排序。由于我已经在我的项目中使用 bootstrap,因此我更愿意使用 bootstrap 而不是 sortTable.js。似乎我只是没有做一些事情来包含引导程序的 javascript 功能。有什么想法吗?
    • 据我了解,在引导程序中,表排序是 not 列排序。它是拖放排序,就像重新排列列表中项目的优先级一样。我们为公司应用程序使用了 sorttable.js 选项,并且我们正在使用 Bootstrap 3。我们认为这是最好的选择。
    • 除非我弄错了,否则他们似乎只是在这里使用引导程序。所以这似乎是可能的? wenzhixin.net.cn/p/bootstrap-table/docs/…
    • 我不推荐这种方法。它不是普通的引导程序,而是添加的库。它必须下载,而不是宝石。因此必须手动检查任何更新(而不是捆绑安装)。在代码方面,它也比 1 个 javascript 文件(方法 #1)更需要维护。 Bootstrap 只不过是预先打包的 CSS 和 JavaScript,以使网站具有响应性。任何不是普通引导程序的东西都只是使用类似的 CSS 或响应式概念。 Bootstrap 主要用于网页的外观/运动,而不是功能。
    • @Niel,让我知道选项 #1 对您的效果如何。如前所述,请参阅我关于 sorttable.js 的问答
    【解决方案3】:

    使用 bootstrap-sassbootstrap-table-rails 宝石:

    Gemfile

    gem 'bootstrap-sass'
    gem 'autoprefixer-rails' # i'm not sure whether this is relevant
    gem 'bootstrap-table-rails'
    

    更新Gemfile后别忘了运行bundle

    app/assets/javascripts/application.js

     //= require bootstrap-sprockets
     //= require bootstrap-table
     //  # Replace below with desired locale if needed
     //= require locale/bootstrap-table-ru-RU
    

    app/assets/stylesheets/application.scss

    @import "bootstrap-sprockets";
    @import "bootstrap";
    @import "bootstrap-table";
    

    app/views/index.html.erb

    <table data-toggle="table">
      <thead>
        <tr>
          <th data-sortable="true">Name</th>
          <th data-sortable="true">Age</th>
        </tr>
      </thead>
    
      <tbody>
        <% @users.each do |user| %>
          <tr>
            <td><%= user.name %></td>
            <td><%= user.age %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
    

    仅供参考,我的 gem 版本:

    bootstrap-sass-3.3.5.1
    bootstrap-table-rails-1.8.2.1
    

    【讨论】:

    • &lt;thead&gt; 是必填项。
    • 如果你使用turbolinks进行ajax加载,还有jquery.turbolinks
    • 现在 jquery.turbolinks doesn't work 使用 Rails 5。您应该禁用此页面的 turbolinks 或完全删除它
    • @Evmorov 或设置版本小于 5 gem 'turbolinks', '&lt; 5'
    • @Evmorov,或者不禁用 turbolinks,并实际学习如何通过 javascript/jquery 使用它。 @installero,而不是使用 $(document).on(load, function() {...}); 使用 $(document).on('turbolinks:load', function() {...});
    【解决方案4】:

    试试这个:

    <table class=" table table-striped" data-sort-name="name" data-sort-order="desc">
    <thead>
      <tr>
        <th data-field="name" data-sortable="true">Name</th>
        <th data-field="age" data-sortable="true">Age</th>
      </tr>
    </thead>
    
     <tbody>
       <% @users.each do |user| %>
        <tr>
          <td><%= user.name %></td>
          <td><%= user.age %></td>
        </tr>
      <% end %>
     </tbody>
    </table>
    

    【讨论】:

    • 我试过了,但不幸的是仍然不适合我。
    • 值得一提的是,此示例仅适用于另一个答案中描述的bootstrap-table-rails
    【解决方案5】:
    1. 在您的 Gemfile 中添加 gem 'bootstrap-table-rails' 并运行 bundle install。
    2. 在您的 application.js 中添加 require bootstrap-table
    3. 在您的 application.css 中添加 require bootstrap-table
    4. data-toggle="table" 添加到您的表格标签中,例如&lt;table data-toggle="table"&gt;
    5. data-sortable="true" 添加到您的标签中,例如&lt;th data-sortable="true"&gt;。如果您不想对任何特定列进行排序,请将此属性设为 false,例如&lt;th data-sortable="false"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2011-06-07
      • 2010-11-20
      相关资源
      最近更新 更多