TODO
###Requirements:
- First you must install jquery-datatables-rails https://github.com/rweng/jquery-datatables-rails/releases/tag/v1.12.0 (currently version 1.12 is the only one supported)
-
Add to your Gemfile:
gem 'quick_datatables_rails', git: 'git://github.com/NaN1488/quick_datatables_rails.git'
-
Install the gem:
$ bundle install
-
Run the installer:
$ rails g quick_datatable:install
-
Run the creator:
$ rails g quick_datatable:create [MODEL]
example:
$ rails g quick_datatable:create product
It will create the class ProductsDatatable for you:
# app/datatables/products_datatable.rb
class ProductsDatatable < QuickDatatablesRails::Base
row_builder do | product |
# Insert an array where each item represents each column displayed in the view
# example:
# [
# product.name,
# product.price
# ]
# Note: the order of the columns must fit the order in the view
end
end
- Controller: Add a json response in your controller and return the instance of
[Model]Datatable
. The constructor requires theview_context
class ProductsController < ApplicationController
def index
respond_to do |format|
format.html
format.json do
render json: ProductsDatatable.new(view_context)
end
end
end
end
- View: Create a table with the header of the attributes you want to display. You must to add data-s-name attribute and the value should be the name of the desired attribute
<table id="product_table" class='quick_datatable'>
<thead>
<tr>
<th data-s-name="name">Name</th>
<th data-s-name="price">Price</th>
...
</tr>
</thead>
<tbody></tbody>
</table>
- Javascript: Call the wrapper QuickDatatable to initialize the table:
$('table.quick_datatable').QuickDatatable();