Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Rails 3.1 with DataTables

In Rails 3.1 what is the recommended gem to integrate with DataTables ?

like image 890
gulden PT Avatar asked Sep 09 '11 16:09

gulden PT


2 Answers

I'm using the jquery-datatables-rails gem with bootstrap (twitter-bootstrap-rails gem) and it is perfect. The railscast episode on it is great -- but don't put the gem in your assets group or it will not work when deploying to heroku (since the assets group is not used in production).

Put this line in your gemfile:

gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'

and run:

bundle install

Also, make sure to put this line in your application.rb:

config.assets.initialize_on_precompile = false

Add this to your application.js

//= require dataTables/jquery.dataTables

And this one if you are using bootstrap:

//= require dataTables/jquery.dataTables.bootstrap

Add this to your application.css:

*= require dataTables/jquery.dataTables

Or this one if you are using bootstrap:

*= require dataTables/jquery.dataTables.bootstrap

And if you are using bootstrap add this to your js.coffee file for your controller you are using datatables in:

If you are using fluid containers:

#// For fluid containers
$('#dashboard').dataTable({
  "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
  "sPaginationType": "bootstrap"
});

If you are using fixed width containers:

#// For fixed width containers
$('.datatable').dataTable({
  "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
  "sPaginationType": "bootstrap"
});
like image 52
Ira Herman Avatar answered Nov 03 '22 13:11

Ira Herman


In Ryan Bates' RailsCast on the topic (http://railscasts.com/episodes/340-datatables) he uses jquery-datatables-rails (https://github.com/rweng/jquery-datatables-rails)

like image 2
Gareth Jones Avatar answered Nov 03 '22 13:11

Gareth Jones