【问题标题】:jquery-datatables-rails gem not applying to tablejquery-datatables-rails gem 不适用于表
【发布时间】:2016-01-08 20:33:25
【问题描述】:

我正在使用 rails 4.2.4,在生成基本产品脚手架后,我似乎无法获得 gem jquery-datatables-rails。我正在使用 github 自述文件和 railscast 剧集来尝试让它完全没有运气。

http://railscasts.com/episodes/340-datatables

https://github.com/rweng/jquery-datatables-rails

宝石文件

gem 'jquery-datatables-rails', '~> 3.3.0'

应用程序.js

//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require turbolinks
//= require_tree .

jQuery -> $('.datatable').DataTable();

应用程序.css

*= require dataTables/jquery.dataTables
*= require_tree .
*= require_self

index.html.erb

<table class="datatable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>      
    </tr>
  </thead>

  <tbody>
    <% @products.each do |product| %>
      <tr>
        <td><%= product.name %></td>
        <td><%= product.price %></td>
      </tr>
    <% end %>
  </tbody>
</table>

我尝试在我的 gemfile 中直接引用 github repo。生成安装似乎没有以任何方式正确添加需求,所以我手动将它们放入。

【问题讨论】:

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


    【解决方案1】:

    通过将此脚本放入视图来解决此问题

    <script>
      $(document).ready(function() {
        $('.datatable').dataTable();
      } );
    </script>
    

    【讨论】:

    • 有同样的问题,但它不适合我。我按照你的解决方案,但仍然没有工作
    【解决方案2】:

    对于未来的读者,您可以这样做,而不是上面接受的答案。您可以将其直接转储到您的 appliation.js 文件中 - 我不喜欢将脚本放在您的视图中,因为上面的答案表明:

    添加到 Application.js

    $(document).ready(function(){
      $("table[role='datatable']").each(function(){
        $(this).DataTable({
          processing: true      
        });
      });  
    })
    
    • 仅在文档准备好后加载。​​
    • 作用于角色元素,而不是 id - 如果您希望在几个表上应用相同的逻辑,这会变得很麻烦。

    不要忘记将 role='datatable' 添加到您的 html 表中。

    • 将 role='datatable' 添加到您的表中。
    • 确保你有一个标题元素和一个
    • tbody 元素

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多