You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+7-1Lines changed: 7 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,4 +2,10 @@
2
2
3
3
## 0.1.0
4
4
* A fresh start. Changes base class name to: `DatatablesRails`.
5
-
*
5
+
* Extracts pagination functions to mixable modules.
6
+
* A user would have the option to stick to the base
7
+
`DatatablesRails::Extensions::SimplePaginator` or replace it with
8
+
`DatatablesRails::Extensions::Kaminari` or
9
+
`DatatablesRails::Extensions::WillPaginate`, depending on what he/she is using to handle record pagination.
10
+
* Removes dependency to pass in a model name to the generator. This way, the developer has more flexibility to implement whatever datatable feature is required.
11
+
* Datatable constructor accepts an optional `options` hash to provide more flexibility. See [README](https://github.com/antillas21/ajax-datatables-rails/blob/master/README.md) for examples.
Copy file name to clipboardExpand all lines: README.md
+93-31Lines changed: 93 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -13,59 +13,83 @@ Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and
13
13
14
14
`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.
15
15
16
+
## ORM Support
17
+
18
+
Currently, it only supports `ActiveRecord` as ORM for performing database queries.
19
+
16
20
## Installation
17
21
18
22
Add these lines to your application's Gemfile:
19
23
20
24
gem 'jquery-datatables-rails'
21
-
gem 'ajax-datatables-rails'
25
+
gem 'rails-datatables'
22
26
23
27
And then execute:
24
28
25
29
$ bundle
26
30
27
31
## Usage
32
+
*The following examples assume that we are setting up rails-datatables for an index of users from a `User` model*
28
33
29
-
*The following examples assume that we are setting up ajax-datatables-rails for an index of users from a `User` model*
30
-
31
-
### Model
34
+
### Generate
32
35
Run the following command:
33
36
34
-
$ rails generate ajaxdatatable User
37
+
$ rails generate datatable User
35
38
36
-
This will generate a file named `users_datatable.rb` in `app/datatables`. Open the file and customize in the functions as directed by the comments
37
39
38
-
#### Initializer
40
+
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
41
+
42
+
### Customize
39
43
```ruby
40
-
definitialize(view)
41
-
@model_name=User
42
-
@columns=# insert array of column names here
43
-
@searchable_columns=#insert array of columns that will be searched
44
-
super(view)
44
+
# uncomment the appropriate paginator module,
45
+
# depending on gems available in your project.
46
+
# include AjaxDatatablesRails::Extensions::Kaminari
47
+
# include AjaxDatatablesRails::Extensions::WillPaginate
48
+
# include AjaxDatatablesRails::Extensions::SimplePaginator
49
+
50
+
defsortable_columns
51
+
# list columns inside the Array in string dot notation.
52
+
# Example: 'users.email'
53
+
@sortable_columns||= []
54
+
end
55
+
56
+
defsearchable_columns
57
+
# list columns inside the Array in string dot notation.
58
+
# Example: 'users.email'
59
+
@searchable_columns||= []
45
60
end
46
61
```
47
62
48
-
* For `@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
63
+
* For `extensions`, just uncomment the paginator you would like to use, given
64
+
the gems bundled in your project. For example, if your models are using `Kaminari`, uncomment `AjaxDatatablesRails::Extensions::Kaminari`. You may remove all commented lines.
65
+
*`SimplePaginator` falls back to passing `offset` and `limit` at the database level (through `ActiveRecord` of course).
66
+
67
+
* 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.
49
68
50
-
* For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
69
+
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
@@ -138,17 +162,55 @@ Finally, the javascript to tie this all together. In the appropriate `js.coffee`
138
162
139
163
```coffeescript
140
164
$->
141
-
$('#users-table').dataTable
165
+
$('#user-table').dataTable
142
166
bProcessing:true
143
167
bServerSide:true
144
-
sAjaxSource:$('#users-table').data('source')
168
+
sAjaxSource:$('#user-table').data('source')
145
169
```
146
170
171
+
### Additional Notes
172
+
173
+
#### Options
174
+
175
+
An `AjaxDatatablesRails::Base` inherited class can accept an options hash at initialization. This provides room for flexibility when required. Example:
Also, a class that inherits from `AjaxDatatablesRails::Base` is not tied to an existing model, module, constant or any type of class in your Rails app. You can pass a name to your datatable class like this:
193
+
194
+
195
+
```
196
+
$ rails generate datatable users
197
+
# returns a users_datatable.rb file with a UsersDatatable class
198
+
199
+
$ rails generate datatable contact_messages
200
+
# returns a contact_messages_datatable.rb file with a ContactMessagesDatatable class
201
+
202
+
$ rails generate datatable UnrespondedMessages
203
+
# returns an unresponded_messages_datatable.rb file with an UnrespondedMessagesDatatable class
204
+
```
205
+
206
+
207
+
In the end, it's up to the developer which model(s), scope(s), relationship(s) (or else) to employ inside the datatable class to retrieve records from the database.
208
+
209
+
147
210
## Contributing
148
211
149
212
1. Fork it
150
213
2. Create your feature branch (`git checkout -b my-new-feature`)
151
214
3. Commit your changes (`git commit -am 'Added some feature'`)
152
215
4. Push to the branch (`git push origin my-new-feature`)
0 commit comments