ducati1212
ducati1212

Reputation: 201

rails 4.2 DataTables Cannot reinitialise DataTable

I know there are some threads on this and I have been trying different solutions.

I have a very simple 1 page rails 4 app using DataTables gem and when I load the page I get the reinitialize error " DataTables warning: table id=pivotal - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3 "

In my Coffee script if I have this line it works fine

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

add in a option line and I get the error. Does not matter what I add. The table does load just with the popup error.

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

Application 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 .

Application 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 . 
*/

Gem File

# 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'

View

<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>

Upvotes: 2

Views: 818

Answers (1)

Stanley Okpala Nwosa
Stanley Okpala Nwosa

Reputation: 306

You will need to add bDestroy : true to thedatatable option as shown below jQuery ->

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

Upvotes: 1

Related Questions