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: README.md
+116-5Lines changed: 116 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,132 @@ Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and
6
6
7
7
## Installation
8
8
9
-
Add this line to your application's Gemfile:
9
+
Add these lines to your application's Gemfile:
10
10
11
+
gem 'jquery-datatables-rails'
11
12
gem 'ajax-datatables-rails'
12
13
13
14
And then execute:
14
15
15
16
$ bundle
16
17
17
-
Or install it yourself as:
18
+
## Usage
19
+
*The following examples assume that we are setting up ajax-datatables-rails for an index of users from a `User` model*
20
+
### Model
21
+
Run the following command:
18
22
19
-
$ gem install ajax-datatables-rails
23
+
$ rails generate ajaxdatatable User
20
24
21
-
## Usage
25
+
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
26
+
27
+
#### Initializer
28
+
```ruby
29
+
definitialize(view)
30
+
@model_name=User
31
+
@columns=# insert array of column names here
32
+
@searchable_columns=#insert array of columns that will be searched
33
+
super(view)
34
+
end
35
+
```
36
+
37
+
* 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
38
+
39
+
* For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
40
+
41
+
This gives us:
42
+
```ruby
43
+
definitialize(view)
44
+
@model_name=User
45
+
@columns= [users.f_name, users.l_name, users.bio]
46
+
@searchable_columns= [users.f_name, users.l_name]
47
+
super(view)
48
+
end
49
+
```
50
+
51
+
#### Data
52
+
```ruby
53
+
defdata
54
+
users.map do |user|
55
+
[
56
+
# comma separated list of the values for each cell of a table row
57
+
]
58
+
end
59
+
end
60
+
```
61
+
62
+
This method builds a 2d array that is used by datatables to construct the html table. Insert the values you want on each column.
0 commit comments