forked from chrismoos/datatables
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
97 lines (57 loc) · 2.53 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Datatables
==========
Rails 3 plugin for adding JQuery DataTables to your application.
DataTables: http://datatables.net/index
Requirements:
JQuery, JQuery UI, and the DataTables JQuery plugin
Features:
Easy definition of a table for an ActiveRecord model.
Supports pagination using will_paginate.
Supports sorting.
TODO
=======
Add tests.
Example
=======
In your controller, set up a datatables source. This is how the data is pulled from the server and returned with AJAX.
Controller
===========
datatables_source :users_source, :user, :columns => [
:username, :fullname, {:name => "updated_at", :eval => 'obj.updated_at.getlocal.rfc2822'},
{:name => "Options", :method => :user_options_column}]
This defines a table, named users_source, for the User model.
The columns are (in order):
username, fullname, updated_at, and options
There are two special ways to display the data for a column/row.
eval:
Evaluates a string, "obj" is an instance of your model in the table (in this case, a User object).
method:
Calls a method in your controller with the instance of your model as the parameter.
When defining a method for a column, an example method in your controller would be:
def user_options_column(user)
"<a href=\"#{url_for :action => 'view', :id => user.id}\">View User</a>"
end
options:
-------
In addition to :column, additional options can be passed in:
:conditions (conditions that will be ANDed to any search query)
:join_tables (if you need to include any additional ActiveRecord table relationships from belongs_to/etc.)
example that will show both the tweet and the user who the tweet belong to:
datatables_source :tweets_source, :tweet, :columns => [
{:name => "Actions", :method => :datatables_actions_column},
:name, {:name=>"User", :method => :tweet_user_column } ],
:conditions => ['censorsed=0'],
:join_tables => ['user']
Known Issue - cannot sort/filter by joined tables
Routes
=========
Because DataTables uses AJAX to load the data in the table, you must define a route to it. The first parameter of datatables_source is a *named route*. The rails plugin uses this to link the HTML for your DataTable.
Example
=========
match 'datatables/user' => "user#users_source", :as => "users_source"
Displaying the table
=========================
Displaying a table is probably the easiest part. In a view for your controller, you just do the following:
<%= datatables :users_source %>
Copyright (c) 2010 Chris Moos, released under the MIT license
Copyright (c) 2011 Ilia Lobsanov, released under the MIT license