Skip to content

Commit 82f3445

Browse files
committed
Update README [ci skip]
1 parent 9b2555a commit 82f3445

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ end
212212

213213
See [here](#columns-syntax) to get more details about columns definitions and how to play with associated models.
214214

215+
You can customize or sanitize the search value passed to the DB by using the `:formater` option :
216+
217+
```ruby
218+
def view_columns
219+
@view_columns ||= {
220+
id: { source: "User.id" },
221+
first_name: { source: "User.first_name" },
222+
last_name: { source: "User.last_name" },
223+
email: { source: "User.email", formater: -> (o) { o.upcase } },
224+
bio: { source: "User.bio" },
225+
}
226+
end
227+
```
228+
215229
#### b. Map data
216230

217231
Then we need to map the records retrieved by the `get_raw_records` method to the real values we want to display :
@@ -225,7 +239,7 @@ def data
225239
last_name: record.last_name,
226240
email: record.email,
227241
bio: record.bio,
228-
DT_RowId: record.id, # This will set the id attribute on the corresponding <tr> in the datatable
242+
DT_RowId: record.id, # This will automagically set the id attribute on the corresponding <tr> in the datatable
229243
}
230244
end
231245
end
@@ -398,6 +412,8 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
398412
end
399413
```
400414

415+
### Using view decorators
416+
401417
If you want to keep things tidy in the data mapping method, you could use
402418
[Draper](https://github.com/drapergem/draper) to define column mappings like below.
403419

@@ -640,7 +656,9 @@ In the end, it's up to the developer which model(s), scope(s), relationship(s)
640656
(or else) to employ inside the datatable class to retrieve records from the
641657
database.
642658

643-
### Creating indices for Postgresql
659+
## Pro Tips
660+
661+
### Create indices for Postgresql
644662

645663
In order to speed up the `ILIKE` queries that are executed when using the default configuration, you might want to consider adding some indices.
646664
For postgresql, you are advised to use the [gin/gist index type](http://www.postgresql.org/docs/current/interactive/pgtrgm.html).
@@ -683,6 +701,71 @@ $ bundle install
683701

684702
That's all :) ([Automatically prefer Yajl or JSON backend over Yaml, if available](https://github.com/rails/rails/commit/63bb955a99eb46e257655c93dd64e86ebbf05651))
685703

704+
### Use HTTP `POST` method
705+
706+
Use HTTP `POST` method to avoid `414 Request-URI Too Large` error. See : [#278](https://github.com/jbox-web/ajax-datatables-rails/issues/278).
707+
708+
You can easily define a route concern in `config/routes.rb` and reuse it when you need it :
709+
710+
```ruby
711+
Rails.application.routes.draw do
712+
concern :with_datatable do
713+
post 'datatable', on: :collection
714+
end
715+
716+
resources :posts, concerns: [:with_datatable]
717+
resources :users, concerns: [:with_datatable]
718+
end
719+
```
720+
721+
then in your controllers :
722+
723+
```ruby
724+
# PostsController
725+
def index
726+
end
727+
728+
def datatable
729+
render json: PostDatatable.new(view_context)
730+
end
731+
732+
# UsersController
733+
def index
734+
end
735+
736+
def datatable
737+
render json: UserDatatable.new(view_context)
738+
end
739+
```
740+
741+
then in your views :
742+
743+
```html
744+
# posts/index.html.erb
745+
<table id="posts-datatable" data-source="<%= datatable_posts_path(format: :json) %>">
746+
747+
# users/index.html.erb
748+
<table id="users-datatable" data-source="<%= datatable_users_path(format: :json) %>">
749+
```
750+
751+
then in your Coffee/JS :
752+
753+
```coffee
754+
$ ->
755+
$('#posts-datatable').dataTable
756+
ajax:
757+
url: $('#posts-datatable').data('source')
758+
type: 'POST'
759+
# ...others options, see [here](#5-wire-up-the-javascript)
760+
761+
$ ->
762+
$('#users-datatable').dataTable
763+
ajax:
764+
url: $('#users-datatable').data('source')
765+
type: 'POST'
766+
# ...others options, see [here](#5-wire-up-the-javascript)
767+
```
768+
686769
## Tutorial
687770

688771
You'll find a sample project [here](https://github.com/ajahongir/ajax-datatables-rails-v-0-4-0-how-to). Its real world example.

0 commit comments

Comments
 (0)