Skip to content

Commit b6745d7

Browse files
committed
Rails 3.x, Datatables 1.9.3, locals changed to globals in case of easy use
1 parent 5593f39 commit b6745d7

File tree

4 files changed

+176
-164
lines changed

4 files changed

+176
-164
lines changed

README.rdoc

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
= Simple Datatables
22

3-
Connects two awesome plugins - Datatables for Jquery and Meta Search together for Rails 3.1
3+
Connects two awesome plugins - Datatables for Jquery and Meta Search together for Rails 3
44

55
== Install
66

77
Add the following to your Gemfile to install simple datatables:
88

99
gem 'simple_datatables'
1010

11-
After bundle install add this line to rails 3.1 assets pipeline and restart server:
11+
After bundle install add this line to rails assets pipeline and restart server:
1212

1313
//= require simple_datatables
1414

@@ -49,7 +49,7 @@ This gem uses:
4949
This gem provides integration with:
5050
* will_paginate for nice pagination request syntax mapping
5151

52-
Gem works only with rails 3.1.
52+
Gem works only with rails 3.1 and higher.
5353

5454
Gem includes datatables library and fnSetFilteringDelay plugin so you haven't include it by yourself.
5555

@@ -79,19 +79,30 @@ Independent fields search will always search using "starts_with" finder.
7979

8080
If you are getting strange results (for instance, iTotalRecords and iTotalDisplayRecords both equal 0, or entirely the wrong collection is used), you might want to specify which collection to use:
8181

82-
respond_with @products, :locals => {:collection => @products}
82+
@collection = @products
83+
respond_with @products
8384

8485
Also if you are using own or uncommon implementation of pagination you may use direct page count variables setting as follows:
8586

86-
respond_with @products, :locals => {:total_entries => 12, :current_page_entries => 10}
87+
@total_entries = 12
88+
@current_page_entries = 10
89+
respond_with @products
8790

88-
In this case gem will not try to find collection or pagination and will use the counters passed via locals.
91+
In this case gem will not try to find collection or pagination and will use the counters passed via variables.
8992

9093
== Example
9194

9295
The following code will show products list datatables. Manufacturer is belongs_to association for Product.
9396

94-
In your controller:
97+
First of all add to your routes.rb datatables search api method:
98+
99+
resources :products do
100+
collection do
101+
get :search
102+
end
103+
end
104+
105+
Then implement it in your controller:
95106

96107
respond_to :html, :datatables
97108

@@ -101,13 +112,13 @@ In your controller:
101112
respond_with @products
102113
end
103114

104-
In your search view (app/views/products/search.jsonify):
115+
In your search view (app/views/products/search.jsonify) implement output fields order:
105116

106117
@products.each do |product|
107118
json << [product.name, product.manufacturer.name]
108119
end
109120

110-
In your index view:
121+
In your index view make a table with header:
111122

112123
%table#products
113124
%thead
@@ -117,8 +128,7 @@ In your index view:
117128

118129
%tbody
119130

120-
121-
In your javascript:
131+
In your javascript add the following code to initialize the datatables (be sure to do it after document load):
122132

123133
$("#products").dataTable({
124134
"sAjaxSource" : "/products/search.datatables",
@@ -133,4 +143,4 @@ In your javascript:
133143

134144
== Copyright
135145

136-
Copyright (c) Grigory Dmitrenko, 2011. See LICENSE for details.
146+
Copyright (c) Grigory Dmitrenko, 2012. See LICENSE for details.

app/views/layouts/application.datatables.jsonify

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
total_entries ||= nil
2-
current_page_entries ||= 0
3-
collection ||= nil
1+
@total_entries ||= nil
2+
@current_page_entries ||= 0
3+
@collection ||= nil
44

55
# Looking for object with pagination if pages count not set
6-
if total_entries.nil? and current_page_entries==0
6+
if @total_entries.nil? and @current_page_entries==0
77

88
# looking for the right collection if not initiated via parameter
9-
if collection.nil?
9+
if @collection.nil?
1010
instance_variables.each do |vn|
1111
v = instance_variable_get(vn)
1212

1313
# if a collection variable was specified, use it only
14-
next if collection && collection != v
14+
next if @collection && @collection != v
1515
next if v.class.name.nil?
1616

17-
collection = v if v.class.name.start_with?("MetaSearch::Searches", "ActiveRecord::Relation", "Mongoid::Criteria")
17+
@collection = v if v.class.name.start_with?("MetaSearch::Searches", "ActiveRecord::Relation", "Mongoid::Criteria")
1818
end
1919
end
2020

2121
# receiving correct current page entries count
22-
current_page_entries = collection.size if collection.respond_to?('size')
22+
@current_page_entries = @collection.size if @collection.respond_to?('size')
2323

2424
# looking for the total entries count
25-
total_entries = collection.length if collection.class.name.start_with?("Mongoid::Criteria")
26-
total_entries = collection.total_entries if collection.respond_to?('total_entries')
25+
@total_entries = @collection.length if @collection.class.name.start_with?("Mongoid::Criteria")
26+
@total_entries = @collection.total_entries if @collection.respond_to?('total_entries')
2727

2828
# fallback to 1 page if total entries count not found
29-
total_entries = current_page_entries if total_entries.nil?
29+
@total_entries = @current_page_entries if @total_entries.nil?
3030
end
3131

32-
json.iTotalRecords current_page_entries
33-
json.iTotalDisplayRecords total_entries
32+
json.iTotalRecords @current_page_entries
33+
json.iTotalDisplayRecords @total_entries
3434
json.sEcho params["sEcho"].to_i
3535
json.aaData do
3636
json.ingest! yield

0 commit comments

Comments
 (0)