Skip to content

Commit 9835efa

Browse files
author
Joel Quenneville
committed
Merge pull request jbox-web#6 from JoelQ/generators
Generators
2 parents c02beb2 + 853d898 commit 9835efa

File tree

6 files changed

+131
-5
lines changed

6 files changed

+131
-5
lines changed

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

README.md

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,132 @@ Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and
66

77
## Installation
88

9-
Add this line to your application's Gemfile:
9+
Add these lines to your application's Gemfile:
1010

11+
gem 'jquery-datatables-rails'
1112
gem 'ajax-datatables-rails'
1213

1314
And then execute:
1415

1516
$ bundle
1617

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:
1822

19-
$ gem install ajax-datatables-rails
23+
$ rails generate ajaxdatatable User
2024

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+
def initialize(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+
def initialize(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+
def data
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.
63+
64+
```ruby
65+
def data
66+
users.map do |user|
67+
[
68+
user.f_name,
69+
user.l_name,
70+
user.bio
71+
]
72+
end
73+
end
74+
```
75+
76+
#### Get Raw Records
77+
```ruby
78+
def get_raw_records
79+
# insert query here
80+
end
81+
```
82+
83+
This is where your query goes.
84+
85+
```ruby
86+
def get_raw_records
87+
User.all
88+
end
89+
```
90+
91+
### Controller
92+
Set up the controller to respond to JSON
93+
94+
```ruby
95+
def index
96+
respond_to do |format|
97+
format.html
98+
format.json { render json: UsersDatatable.new(view_context) }
99+
end
100+
end
101+
```
102+
103+
### View
104+
* Set up an html `<table>` with a `<thead>` and `<tbody>`
105+
* Add in your table headers if desired
106+
* Don't add any rows to the body of the table, datatables does this automatically
107+
* Add a data attribute to the `<table>` tag with the url of the JSON feed
108+
109+
The resulting view may look like this:
110+
111+
```erb
112+
<table id="user-table", data-source="<%= users_path(format: :json) %>">
113+
<thead>
114+
<tr>
115+
<th>First Name</th>
116+
<th>Last Name</th>
117+
<th>Brief Bio</th>
118+
</tr>
119+
</thead>
120+
<tbody>
121+
</tbody>
122+
</table>
123+
```
124+
125+
### Javascript
126+
Finally, the javascript to tie this all together. In the appropriate `js.coffee` file:
22127

23-
TODO: Write usage instructions here
128+
```coffeescript
129+
$ ->
130+
$('#users-table').dataTable
131+
bProcessing: true
132+
bServerSide: true
133+
sAjaxSource: $('#users-table').data('source')
134+
```
24135

25136
## Contributing
26137

Rakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
#!/usr/bin/env rake
22
require "bundler/gem_tasks"
3+
require 'rspec/core/rake_task'
4+
5+
RSpec::Core::RakeTask.new(:spec)
6+
task default: :spec

ajax-datatables-rails.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
1414
gem.name = "ajax-datatables-rails"
1515
gem.require_paths = ["lib"]
1616
gem.version = AjaxDatatablesRails::VERSION
17+
18+
gem.add_development_dependency "rspec"
1719
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'spec_helper'
2+
3+
describe AjaxDatatablesRails do
4+
describe "as_json" do
5+
it "should return the correct json feed"
6+
end
7+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'ajax-datatables-rails'

0 commit comments

Comments
 (0)