【问题标题】:rails 4.2 DataTables Cannot reinitialise DataTablerails 4.2 DataTables无法重新初始化DataTable
【发布时间】:2015-05-30 12:13:16
【问题描述】:

我知道这方面有一些线索,我一直在尝试不同的解决方案。

我有一个使用 DataTables gem 的非常简单的 1 页 rails 4 应用程序,当我加载页面时出现重新初始化错误 " DataTables 警告:table id=pivotal - 无法重新初始化 DataTable。有关此错误的更多信息,请参阅http://datatables.net/tn/3 "

在我的咖啡脚本中,如果我有这一行,它可以正常工作

jQuery ->
  $('#pivotal').dataTable

添加一个选项行,我得到了错误。我添加什么都没关系。该表只加载弹出错误。

jQuery ->
  $('#pivotal').dataTable
    bJQueryUI: true
    pagingType: "simple"

应用程序 JS

// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require dataTables/jquery.dataTables
//= require_tree .

应用程序 CSS

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require jquery-ui
 *= require dataTables/src/demo_table_jui
 *= require_tree . 
*/

宝石文件

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'tracker_api', '~> 0.2.7'

查看

<table id="pivotal">  
  <thead>
    <tr>
      <th>Story Name</th>
      <th>Story Date Created</th>
    </tr>
  </thead>
 <tbody>
            <% @piv.each do |story| %>

            <tr>
            <td><%= story.name %>  </td>
            <td><%= story.created_at.strftime("%F") %>  </td>
            </tr>

            <% end %>
        </tbody>
</table>

【问题讨论】:

  • 您是否尝试添加retrieve: true 选项?
  • 代码中有两个jQuery -&gt; $('#pivotal').dataTable 实例。
  • 我只有 1 个咖啡脚本,其中有 1 行,我在哪里可以找到它的多个副本?
  • 你能解决这个问题吗? @ducati1212

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


【解决方案1】:

您需要将bDestroy : true 添加到datatable 选项,如下所示 jQuery ->

$('#pivotal').dataTable
  bJQueryUI: true
  pagingType: "simple"
  bDestroy: true

【讨论】:

    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2012-09-09
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多