0

我将 DataTables 与我的 Rails 应用程序一起使用。

出于某种原因,DataTables/JQuery 似乎认为我正在尝试重新初始化 DataTable。每当我加载使用 DataTables 的页面时,都会出现以下错误:

DataTables warning (table id = 'DataTables_Table_0'): Cannot reinitialise DataTable.
To retrieve the DataTables object for this table, pass no arguments or see the docs for bRetrieve and bDestroy

为什么会这样?据我所知,我的页面只加载一次脚本,我知道它每页只生成一个表格元素......

/application/views/layouts/application.html.haml

!!!
%html
  %head
    = javascript_include_tag "application"
  %body
    = yield

/application/views/reports/index.html.haml

%table
  %thead
    %tr
      %th Short Name
      %th Controls
  %tbody
    - @reports.each do |report|
      %tr
        %td= link_to report.full_name, report_path(report)
        %td
          = link_to "Edit", edit_report_path(report)
          \|
          = delete_link "Delete", report_path(report)

我很确定我已经正确设置了资产管道......

/assets/javascripts/application.js

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

而且我很确定我正确地调用了 DataTables ......

/assets/javascripts/tables.js.coffee

jQuery ->
  $('table').dataTable
    sPaginationType: "full_numbers"
    bJQueryUI: true

而且我认为我的任何宝石都不会导致这种行为:

宝石文件

source "http://rubygems.org"

gem "rails", "3.2.11"
gem "activesupport"
gem "actionmailer"
gem "mail"
gem "railties"
gem "compass"
gem "pg"
gem "sass"
gem "sass-rails"
gem "haml"
gem "haml-rails"
gem "susy"
gem "jquery-rails"
gem "google-analytics-rails"
gem "declarative_authorization"
gem "authlogic"
gem "simple_form"
gem "country-select"
gem "rails3-generators"
gem "hirb"
gem "debugger"
gem "redis"
gem "RedCloth"
gem "friendly_id"
gem "high_voltage"
gem "validatious"
gem "houdini"
gem "rspec-rerun"
gem "html5shiv-rails"
gem "normalize-rails"
gem "simple-navigation"
gem "thin"

group :development do
  gem "taps"
  gem "better_errors"
  gem "binding_of_caller"
end

group :test do
  gem "capybara"
  gem "rspec-rails"
  gem "database_cleaner"
  gem "launchy"
  gem "spork", "0.9.2"
  gem "respec"
end

group :assets do
  gem "better_errors"
  gem "sass-rails"
  gem "coffee-rails"
  gem "compass-rails"
  gem "compass-susy-plugin"
  gem "fancy-buttons"
  gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
  gem "jquery-ui-rails"
  gem "uglifier"
end

更新:我知道这资产管道有关,因为 Heroku 上生产中的相同代码(之后rake assets:precompile)不会导致它在开发中引起的问题(应用程序为我即时组装资产) .)

4

2 回答 2

0

如果在使用数据初始化表之前触发 dataTables js,则可能会发生此错误。尝试移动您的

= javascript_include_tag “应用程序”

到页面底部。

======

!!!
%html
  %head

  %body
    = yield
    = javascript_include_tag "application"
于 2013-07-29T10:11:50.573 回答
0

尝试整理如下

$('selector').dataTable({...});

1. 大部分情况下,您应该使用 ID 代替选择器,

  1. 实际问题是您不应该使用重复的 ID,或者如果您使用类作为选择器,则不应将该类用于多个元素(听起来很烦人),因此最好将选择器用作 ID(ID 不应该重复元素)
于 2015-03-12T07:05:05.523 回答