forked from jbox-web/ajax-datatables-rails
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparams.json
6 lines (6 loc) · 6.38 KB
/
params.json
1
2
3
4
5
6
{
"name": "Rails & DataTables",
"tagline": "A wrapper around datatable's ajax methods that allow server-side pagination, ordering & searching out of box in a rails app",
"body": "## Welcome to ajax-datatables-rails\r\nDataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, and will add advanced interaction controls to any HTML table. For more details look at home [page](https://datatables.net/)\r\nThis gem fully supports DataTables behavior and easily integrates to rails app. No more need to write JS or Ruby code, just setup your model datatable. \r\n\r\n## Description\r\nThis gem is based on DataTables idiom so It fully supports DataTable.\r\n\r\n## How-to project\r\nI always build admin page with some data presenting and I love to use DataTable in my projects. But It's to hard to build so many table with server-side pagination, ordering & searching. But now we have this tool! And Today I am going to show usage of this gem with a super easy [pattern](https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to).\r\n\r\n## Basic usage\r\nLet's suppose that we have 2 models: countries and cities. So every country has many cities.\r\nwe build rails app.\r\n\r\n`$ rails new how-to`\r\n\r\n`$ bin/rails g model country name:string iata:string`\r\n\r\n`$ bin/rails g model city name:string iata:string timezone:string country:references`\r\n\r\n`$ bin/rails g controller cities index`\r\n\r\n`$ rake db:migrate`\r\n\r\n### Gemfile\r\n```ruby\r\ngem 'ajax-datatables-rails', github: 'ajahongir/ajax-datatables-rails', branch: 'v-0-4-0'\r\ngem 'rails_script'\r\ngem 'js-routes'\r\ngem 'responders' # if you are using rails 4.2+\r\n```\r\n\r\nAnd then execute:\r\n\r\n`$ bundle`\r\n\r\n### Initialization third-party libs\r\n\r\n**rails_script**\r\n\r\nAfter bundling you need to run the initial installation generator for [rails_script](https://github.com/gemgento/rails_script):\r\n\r\n\r\n`$ rails g rails_script:install`\r\nand include `<%= include_rails_script %>` in application layout.\r\n\r\n**js-routes**\r\n\r\nAdd `//= require js-routes` in your `application.js` and after clear your cache `$ rake tmp:cache:clear`\r\n\r\n**DataTables**\r\n\r\nAdding latest version of [DataTables](https://datatables.net) and style in application layout:\r\n\r\n```html\r\n<link rel=\"stylesheet\" href=\"//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css\" >\r\n<script src=\"//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js\"></script>\r\n```\r\n\r\n###Javascript\r\n\r\nI add some defaults for my DataTables in global.coffee:\r\n\r\n```coffee\r\n$ ->\r\n $.extend $.fn.DataTable.defaults,\r\n searching: true\r\n ordering: true\r\n deferRender: true\r\n lengthMenu: [10, 25, 50, 100]\r\n serverSide: true\r\n\r\n initComplete: ->\r\n $('.filters input, .filters select', @).on 'change', (e) =>\r\n th = $(e.target).closest(\"th\")\r\n @api().column(th.index()).search($(e.target).val()).draw()\r\n```\r\n\r\nand add these lines in to cities.coffee \r\n\r\n```coffee\r\nclass App.Cities extends App.Base\r\n\r\n index: ->\r\n $ ->\r\n $('#dataTable').dataTable\r\n ajax: Routes.cities_index_path()\r\n columns: [\r\n data: \"id\"\r\n ,\r\n data: \"name\"\r\n ,\r\n data: \"iata\"\r\n ,\r\n data: \"custom_column\"\r\n ,\r\n data: \"country_name\"\r\n ]\r\n```\r\n\r\n### Model Datatable\r\n\r\nGenerating Datatable for model City:\r\n\r\n`$ bin/rails generate datatable City`\r\n\r\nWell, we have a app/datatables/city_datatatble.rb file, lets define model fields:\r\n\r\n```ruby\r\nclass CityDatatable < AjaxDatatablesRails::Base\r\n\r\n def view_columns\r\n @view_columns ||= {\r\n id: { source: \"City.id\", cond: :eq },\r\n name: { source: \"City.name\" },\r\n custom_column: { source: \"custom_column\", cond: filter_custom_column_condition },\r\n iata: { source: \"City.iata\" },\r\n country_name: { source: \"City.country_id\", cond: filter_country_condition },\r\n }\r\n end\r\n\r\n private\r\n def data\r\n records.map do |city|\r\n {\r\n id: city.id,\r\n name: city.name,\r\n iata: city.iata,\r\n country_name: city.country.try(:name),\r\n custom_column: city[:custom_column]\r\n }\r\n end\r\n end\r\n\r\n def get_raw_records\r\n City.select('cities.*, timezone AS custom_column').includes(:country)\r\n end\r\n\r\n def filter_country_condition\r\n ->(column) { column.table[column.field].eq(column.search.value.to_i + 1) }\r\n end\r\n\r\n def filter_custom_column_condition\r\n ->(column) { ::Arel::Nodes::SqlLiteral.new(column.field.to_s).matches(\"#{ column.search.value }%\") }\r\n end\r\nend\r\n```\r\n\r\n### Controller\r\n\r\nDon't forget to return response with our Datatable in controller:\r\n\r\n```ruby\r\nclass CitiesController < ApplicationController\r\n respond_to :html, :json\r\n\r\n def index\r\n respond_to do |format|\r\n format.html\r\n format.json do\r\n render json: CityDatatable.new(view_context)\r\n end\r\n end\r\n end\r\nend\r\n```\r\n\r\n###View\r\n\r\nLast thing to is adding table in out cities/index.html.erb\r\n\r\n```html\r\n<div class=\"table-responsive\">\r\n <table id=\"dataTable\" class=\"table table-striped table-bordered\" cellspacing=\"0\" width=\"100%\">\r\n <thead>\r\n <tr class=\"filters\">\r\n <th><input/></th>\r\n <th><input/></th>\r\n <th><input/></th>\r\n <th><input/></th>\r\n <th>\r\n <%= collection_select(:city, :country_id, Country.all, :id, :name, include_blank: true) %>\r\n </th>\r\n </tr>\r\n <tr>\r\n <th>Id</th>\r\n <th>Name</th>\r\n <th>IATA</th>\r\n <th>Timezone</th>\r\n <th>Country</th>\r\n </tr>\r\n </thead>\r\n <tbody></tbody>\r\n </table>\r\n</div>\r\n```\r\n\r\n`$ bin/rails s`\r\n\r\nThat is it!\r\n\r\n##Requirements\r\nThe gem supports ruby 2.0 and higher, rails 3.2 and higher versions.\r\n\r\n##Sample project\r\nHere is [how-to](https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to) project\r\n\r\n### Authors and Contributors\r\nFeel free to fork & contribute or leave a feedback if you have any issues.",
"note": "Don't delete this file! It's used internally to help with page regeneration."
}