Datatables recently released version 1.10 (which includes a new API and features) and deprecated version 1.9.
If you have dataTables 1.9 in your project and want to keep using it, please
use this gem's version 0.1.x
in your Gemfile
:
# specific version number
gem 'ajax-datatables-rails', '0.1.2'
# or, support on datatables 1.9
gem 'ajax-datatables-rails', git: 'git://github.com/antillas21/ajax-datatables-rails.git', branch: 'legacy'
If you have dataTables 1.10 in your project, then use the gem's latest version,
or point to the master
branch.
Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and search your html tables. When dealing with large tables (more than a couple hundred rows) however, we run into performance issues. These can be fixed by using server-side pagination, but this breaks some datatables functionality.
ajax-datatables-rails
is a wrapper around datatable's ajax methods that allow
synchronization with server-side pagination in a rails app. It was inspired by
this Railscast. I needed to
implement a similar solution in a couple projects I was working on, so I
extracted a solution into a gem.
Currently AjaxDatatablesRails
only supports ActiveRecord
as ORM for
performing database queries.
Adding support for Sequel
, Mongoid
and MongoMapper
is a planned feature
for this gem. If you'd be interested in contributing to speed development,
please open an issue
and get in touch.
Add these lines to your application's Gemfile:
gem 'jquery-datatables-rails'
gem 'ajax-datatables-rails'
And then execute:
$ bundle
The jquery-datatables-rails
gem is listed as a convenience, to ease adding
jQuery dataTables to your Rails project. You can always add the plugin assets
manually via the assets pipeline. If you decide to use the
jquery-datatables-rails
gem, please refer to its installation instructions
here.
The following examples assume that we are setting up ajax-datatables-rails for
an index of users from a User
model, and that we are using postgresql as
our db, because you should be using it, if not, please refer to the
Searching on non text-based columns
entry in the Additional Notes section.
Run the following command:
$ rails generate datatable User
This will generate a file named user_datatable.rb
in app/datatables
.
Open the file and customize in the functions as directed by the comments.
Take a look here for an explanation about the generator syntax.
def sortable_columns
# Declare strings in this format: ModelName.column_name
@sortable_columns ||= []
end
def searchable_columns
# Declare strings in this format: ModelName.column_name
@searchable_columns ||= []
end
-
For
sortable_columns
, assign an array of the database columns that correspond to the columns in our view table. For example[users.f_name, users.l_name, users.bio]
. This array is used for sorting by various columns. The sequence of these 3 columns must mirror the order of declarations in thedata
method below. You cannot leave this array empty as of 0.3.0. -
For
searchable_columns
, assign an array of the database columns that you want searchable by datatables. Suppose we need to sort and search users:first_name
,last_name
andbio
.
This gives us:
include AjaxDatatablesRails::Extensions::Kaminari
def sortable_columns
@sortable_columns ||= %w(User.first_name User.last_name User.bio)
# this is equal to:
# @sortable_columns ||= ['User.first_name', 'User.last_name', 'User.bio']
end
def searchable_columns
@searchable_columns ||= %w(User.first_name User.last_name User.bio)
# this is equal to:
# @searchable_columns ||= ['User.first_name', 'User.last_name', 'User.bio']
end
- See here for notes about the
searchable_columns
settings (if using something different frompostgre
). - Read these notes about
considerations for the
searchable_columns
andsortable_columns
methods.
def data
records.map do |record|
[
# comma separated list of the values for each cell of a table row
# example: record.attribute,
]
end
end
This method builds a 2D array that is used by datatables to construct the html table. Insert the values you want on each column.
def data
records.map do |record|
[
record.first_name,
record.last_name,
record.bio
]
end
end
In the example above, we use the same sequence of column declarations as in
sortable_columns
. This ordering is important! And as of 0.3.0, the first
column must be a sortable column. For more, see
this issue.
See here if you need to use view helpers in the
returned 2D array, like link_to
, mail_to
, resource_path
, etc.
If you want the gem inserts automatically the ID of the record in the <tr>
element
as shown in this DataTable example,
you have to perform some modifications in both some_datatable.rb
file and in your javascript.
Here is an example:
def data
records.map do |record|
{
'0' => record.first_name,
'1' => record.last_name,
'2' => record.email,
'DT_RowId' => record.id
}
end
end
and in your javascript file:
$(function() {
return $('#table_id').dataTable({
processing: true,
serverSide: true,
ajax: 'ajax_url',
columns: [
{data: '0' },
{data: '1' },
{data: '2' }
]
});
});
def get_raw_records
# insert query here
end
This is where your query goes.
def get_raw_records
# suppose we need all User records
# Rails 4+
User.all
# Rails 3.x
# User.scoped
end
Obviously, you can construct your query as required for the use case the
datatable is used. Example: User.active.with_recent_messages
.
IMPORTANT: Make sure to return an ActiveRecord::Relation
object as the
end product of this method. Why? Because the result from this method, will
be chained (for now) to ActiveRecord
methods for sorting, filtering
and pagination.
The previous example has only one single model. But what about if you have some associated nested models and in a report you want to show fields from these tables.
Take an example that has an Event, Course, Coursetype, Allocation, Teacher, Contact, Competency and CompetencyType
models. We want to have a datatables
report which has the following column:
'coursetypes.name',
'courses.name',
'events.title',
'events.event_start',
'events.event_end',
'contacts.full_name',
'competency_types.name',
'events.status'
We want to sort and search on all columns of the list. The related definition would be:
def sortable_columns
@sortable_columns ||= [
'Coursetype.name',
'Course.name',
'Event.title',
'Event.event_start',
'Event.event_end',
'Contact.last_name',
'CompetencyType.name',
'Event.status'
]
end
def searchable_columns
@searchable_columns ||= [
'Coursetype.name',
'Course.name',
'Event.title',
'Event.event_start',
'Event.event_end',
'Contact.last_name',
'CompetencyType.name',
'Event.status'
]
end
def get_raw_records
Event.joins(
{ course: :coursetype },
{ allocations: {
teacher: [:contact, {competencies: :competency_type}]
}
}).distinct
end