# ajax-datatables-rails [![Build Status](https://travis-ci.org/antillas21/ajax-datatables-rails.svg?branch=master)](https://travis-ci.org/antillas21/ajax-datatables-rails) [![Gem Version](https://badge.fury.io/rb/ajax-datatables-rails.svg)](http://badge.fury.io/rb/ajax-datatables-rails) [![Code Climate](https://codeclimate.com/github/antillas21/ajax-datatables-rails/badges/gpa.svg)](https://codeclimate.com/github/antillas21/ajax-datatables-rails) ### Versions [Datatables](http://datatables.net) recently released version 1.10 and deprecated version 1.9 which includes a new API and features. 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`: ```ruby # 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. ## Description 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](http://railscasts.com/episodes/340-datatables). I needed to implement a similar solution in a couple projects I was working on so I extracted it out into a gem. ## ORM support 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](https://github.com/antillas21/ajax-datatables-rails/issues/new) and get in touch. ## Installation 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](https://github.com/rweng/jquery-datatables-rails). ## Usage *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](#searching-on-non-text-based-columns) entry in the Additional Notes section.* ### Generate 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](#generator-syntax) for an explanation about the generator syntax. ### Customize ```ruby # uncomment the appropriate paginator module, # depending on gems available in your project. # include AjaxDatatablesRails::Extensions::Kaminari # include AjaxDatatablesRails::Extensions::WillPaginate # include AjaxDatatablesRails::Extensions::SimplePaginator def sortable_columns # list columns inside the Array in string dot notation. # Example: 'users.email' @sortable_columns ||= [] end def searchable_columns # list columns inside the Array in string dot notation. # Example: 'users.email' @searchable_columns ||= [] end ``` * For `paginator options`, just uncomment the paginator you would like to use, given the gems bundled in your project. For example, if your models are using `Kaminari`, uncomment `AjaxDatatablesRails::Extensions::Kaminari`. You may remove all commented lines. * `SimplePaginator` is the most basic of them all, it falls back to passing `offset` and `limit` at the database level (through `ActiveRecord` of course, as that is the only ORM supported for the time being). * 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. * For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]` This gives us: ```ruby include AjaxDatatablesRails::Extensions::Kaminari def sortable_columns @sortable_columns ||= ['users.f_name', 'users.l_name', 'users.bio'] end def searchable_columns @searchable_columns ||= ['users.f_name', 'users.l_name'] end ``` [See here](#searching-on-non-text-based-columns) for notes regarding database config (if using something different from `postgre`). ### Map data ```ruby 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. ```ruby def data records.map do |record| [ record.f_name, record.l_name, record.bio ] end end ``` [See here](#using-view-helpers) if you need to use view helpers in the returned 2d array, like `link_to`, `mail_to`, `resource_path`, etc. #### Get Raw Records ```ruby def get_raw_records # insert query here end ``` This is where your query goes. ```ruby def get_raw_records User.all 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. #### Associated and nested models 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: ```ruby '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: ```ruby def sortable_columns @sortable_columns ||= [ 'coursetypes.name', 'courses.name', 'events.title', 'events.event_start', 'events.event_end', 'contacts.last_name', 'competency_types.name', 'events.status' ] end def searchable_columns @searchable_columns ||= [ 'coursetypes.name', 'courses.name', 'events.title', 'events.event_start', 'events.event_end', 'contacts.last_name', 'competency_types.name', 'events.status' ] end def get_raw_records Event.joins( { course: :coursetype }, { allocations: { teacher: [:contact, {competencies: :competency_type}] } }).distinct end ``` __Some comments for the above code:__ 1. In the list we show `full_name`, but in `sortable_columns` and `searchable_columns` we use `last_name` from the `Contact` model. The reason is we can use only database columns as sort or search fields and the full_name is not a database field. 2. In the `get_raw_records` method we have quite a complex query having one to many and may to many associations using the joins ActiveRecord method. The joins will generate INNER JOIN relations in the SQL query. In this case we do not include all event in the report if we have events which is not associated with any model record from the relation. 3. To have all event records in the list we should use the `.includes` method, which generate LEFT OUTER JOIN relation of the SQL query. __IMPORTANT:__ Make sure to append `.references(:related_model)` with any associated model. That forces the eager loading of all the associated models by one SQL query, and the search condition for any column works fine. Otherwise the `:recordsFiltered => filter_records(get_raw_records).count(:all)` will generate 2 SQL queries (one for the Event model, and then another for the associated tables). The `:recordsFiltered => filter_records(get_raw_records).count(:all)` will use only the first one to return from the ActiveRecord::Relation object in `get_raw_records` and you will get an error message of __Unknown column 'yourtable.yourfield' in 'where clause'__ in case the search field value is not empty. So the query using the `.includes()` method is: ```ruby def get_raw_records Event.includes( { course: :coursetype }, { allocations: { teacher: [:contact, { competencies: :competency_type }] } } ).references(:course).distinct end ``` ### Controller Set up the controller to respond to JSON ```ruby def index respond_to do |format| format.html format.json { render json: UserDatatable.new(view_context) } end end ``` Don't forget to make sure the proper route has been added to `config/routes.rb`. ### View * Set up an html `
First Name | Last Name | Brief Bio |
---|