2

我正在使用 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。生成安装似乎没有以任何方式正确添加需求,所以我手动将它们放入。

4

2 回答 2

3

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

<script>
  $(document).ready(function() {
    $('.datatable').dataTable();
  } );
</script>
于 2016-01-09T01:41:59.703 回答
0

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

添加到 Application.js

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

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

  • 将 role='datatable' 添加到您的表中。
  • 确保你有一个 theader 元素,还有一个
  • tbody 元素

将角色添加到表

于 2018-08-22T08:49:34.790 回答