Skip to content

Commit 5327e28

Browse files
author
Francesc Esplugas
committed
Added observe_field for the searches
1 parent 29fb114 commit 5327e28

4 files changed

Lines changed: 66 additions & 54 deletions

File tree

app/controllers/typus_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def index
2121
elsif params[:search]
2222
@search = []
2323
@model.search_fields.each { |search| @search << "LOWER(#{search}) LIKE '%#{params[:search]}%'" }
24-
@item_pages, @items = paginate @model, :conditions => "#{@search.join(" OR ")}"
24+
@item_pages, @items = paginate @model, :conditions => "#{@search.join(" OR ")}", :per_page => 20
25+
render :partial => "table"
2526
elsif params[:order_by]
2627
@order = params[:order_by]
2728
@sort_order = params[:sort_order]

app/views/typus/_table.rhtml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<table>
2+
3+
<tr>
4+
5+
<% @fields.each do |column| %>
6+
7+
<th><a href="?order_by=<%= column %>&sort_order=<% if params[:order_by].include? column %><%= (@status = (params[:sort_order] == "asc") ? "desc" : "asc" ) %><% else %><%= "asc" %><% end %>"><div class="<% if params[:order_by].include? column %><%= (@status = (params[:sort_order] == "asc") ? "order_desc" : "order_asc" ) %><% else %><%= "order" %><% end %>"><%= column.humanize %></div></a></th>
8+
<% end %>
9+
<th>&nbsp;</th>
10+
</tr>
11+
12+
<% @items.each do |item| %>
13+
<tr class="<%= cycle('even', 'odd') %>" id="item_<%= item.id %>">
14+
<% @fields.each do |column| %>
15+
<% if %w(id title name full_name first_name last_name).include? column -%>
16+
<td><%= link_to item.send(column), :action => 'edit', :id => item.id %></td>
17+
<% elsif %w(email).include? column -%>
18+
<td><%= mail_to item.send(column), item.send(column), :title => "" %></td>
19+
<% elsif %w(status).include? column -%>
20+
<td width="20px" align="center"><%= image_tag(status = item.send(column)? "typus/status_true.gif" : "typus/status_false.gif") %></td>
21+
<% elsif %w(verified is_admin).include? column -%>
22+
<td><%= status = item.send(column)? "Yes" : "No" %></td>
23+
<% elsif %w(created_at updated_at).include? column -%>
24+
<td width="80px"><%= fmt_date(item.send(column)) %></td>
25+
<% else -%>
26+
<td><%=h item.send(column) %></td>
27+
<% end -%>
28+
<% end %>
29+
<td width="10px"><%= link_to image_tag("typus/trash.gif"), { :action => 'destroy', :id => item }, :confirm => "Remove this entry?" %></td>
30+
</tr>
31+
<% end %>
32+
33+
</table>

app/views/typus/index.rhtml

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,52 @@
11
<h2>
22
<div class="left"><%= link_to @model.to_s.capitalize, :model => params[:model], :action => "index" %></div>
3-
<div class="right"><%= link_to image_tag('typus/arrow_right.gif'), { :page => @item_pages.current.previous, :filter_by => params[:filter_by], :filter_id => params[:filter_id], :model => params[:model], :status => params[:status], :search => params[:search], :order_by => params[:order_by], :sort_order => params[:sort_order] } if @item_pages.current.previous %> <%= link_to image_tag('typus/arrow_left.gif'), { :page => @item_pages.current.next, :filter_by => params[:filter_by], :filter_id => params[:filter_id], :model => params[:model], :status => params[:status], :search => params[:search], :order_by => params[:order_by], :sort_order => params[:sort_order] } if @item_pages.current.next %><%= "&nbsp;" if @item_pages.current.next %><%= link_to_function image_tag("typus/search_right.gif"), "['search'].each(Element.toggle);" if @model.search_fields.size > 0 %><%= "&nbsp;" if @model.search_fields.size > 0 %><%= link_to image_tag("typus/add.gif", :title => "New #{@model}"), :action => "new" %></div>
3+
<div class="right"><%= link_to image_tag('typus/arrow_right.gif'), { :page => @item_pages.current.previous, :filter_by => params[:filter_by], :filter_id => params[:filter_id], :model => params[:model], :status => params[:status], :search => params[:search], :order_by => params[:order_by], :sort_order => params[:sort_order] } if @item_pages.current.previous %> <%= link_to image_tag('typus/arrow_left.gif'), { :page => @item_pages.current.next, :filter_by => params[:filter_by], :filter_id => params[:filter_id], :model => params[:model], :status => params[:status], :search => params[:search], :order_by => params[:order_by], :sort_order => params[:sort_order] } if @item_pages.current.next %><%= "&nbsp;" if @item_pages.current.next %><%= link_to_function image_tag("typus/search_right.gif"), "['search_box'].each(Element.toggle);" if @model.search_fields.size > 0 %><%= "&nbsp;" if @model.search_fields.size > 0 %><%= link_to image_tag("typus/add.gif", :title => "New #{@model}"), :action => "new" %></div>
44
</h2>
55

66
<%= feedback %>
77

8+
<!--
89
<div id="search" <%= "style=\"display: none;\"" if !params[:search] %> class="box">
910
<% form_tag ({:action => 'index', :model => params[:model]}, :method => 'get') do %>
1011
<p><%= text_field_tag :search, params[:search] %>&nbsp;&nbsp;<%= submit_tag "Search", :name => nil %></p>
1112
<% end %>
1213
</div>
14+
-->
1315

14-
<% if @model.count > 0 -%>
16+
<div id="search_box" <%= "style=\"display: none;\"" if !params[:search] %> class="box">
17+
<p><input id="search" stylname="search" type="text" value=""> <span id="spinner" style="height: 40px; display: none;"><%= image_tag("typus/spinner.gif") %></span></p>
18+
<%= observe_field 'search', :frequency => 0.5, :update => 'list', :url => { :action => 'index', :model => params[:model] }, :with => "'search=' + escape(value)", :before => "Element.show('spinner');", :complete => "Element.hide('spinner');" %>
19+
</div>
1520

16-
<% if @items.size > 0 -%>
17-
18-
<table>
19-
20-
<tr>
21-
22-
<% @fields.each do |column| %>
23-
24-
<th><a href="?order_by=<%= column %>&sort_order=<% if params[:order_by].include? column %><%= (@status = (params[:sort_order] == "asc") ? "desc" : "asc" ) %><% else %><%= "asc" %><% end %>"><div class="<% if params[:order_by].include? column %><%= (@status = (params[:sort_order] == "asc") ? "order_desc" : "order_asc" ) %><% else %><%= "order" %><% end %>"><%= column.humanize %></div></a></th>
25-
<% end %>
26-
<th>&nbsp;</th>
27-
</tr>
28-
29-
<% @items.each do |item| %>
30-
<tr class="<%= cycle('even', 'odd') %>" id="item_<%= item.id %>">
31-
<% @fields.each do |column| %>
32-
<% if %w(id title name full_name first_name last_name).include? column -%>
33-
<td><%= link_to item.send(column), :action => 'edit', :id => item.id %></td>
34-
<% elsif %w(email).include? column -%>
35-
<td><%= mail_to item.send(column), item.send(column), :title => "" %></td>
36-
<% elsif %w(status).include? column -%>
37-
<td width="20px" align="center"><%= image_tag(status = item.send(column)? "typus/status_true.gif" : "typus/status_false.gif") %></td>
38-
<% elsif %w(verified is_admin).include? column -%>
39-
<td><%= status = item.send(column)? "Yes" : "No" %></td>
40-
<% elsif %w(created_at updated_at).include? column -%>
41-
<td width="80px"><%= fmt_date(item.send(column)) %></td>
42-
<% else -%>
43-
<td><%=h item.send(column) %></td>
44-
<% end -%>
45-
<% end %>
46-
<td width="10px"><%= link_to image_tag("typus/trash.gif"), { :action => 'destroy', :id => item }, :confirm => "Remove this entry?" %></td>
47-
</tr>
48-
<% end %>
49-
50-
</table>
21+
<div id="list">
22+
23+
<% if @model.count > 0 -%>
24+
25+
<% if @items.size > 0 -%>
5126

52-
<!-- -->
27+
<%= render :partial => "table" %>
28+
<!-- -->
5329

54-
<% else -%>
30+
<% else -%>
5531

56-
<div class="box" id="empty">
57-
<% if params[:search] %>
58-
<p>No <strong><%= params[:model] %></strong> match the search <strong><%= params[:search] %></strong></p>
59-
<% else %>
60-
<p>There are no <strong><%= params[:model] %></strong> with the filter <strong><%= params[:filter_by] %></strong> set to <strong><%= params[:filter_id] %></strong>. <%= link_to "Remove filter?", :action => "index", :model => params[:model] %></p>
61-
<% end %>
62-
</div>
32+
<div class="box" id="empty">
33+
<% if params[:search] %>
34+
<p>No <strong><%= params[:model] %></strong> match the search <strong><%= params[:search] %></strong></p>
35+
<% else %>
36+
<p>There are no <strong><%= params[:model] %></strong> with the filter <strong><%= params[:filter_by] %></strong> set to <strong><%= params[:filter_id] %></strong>. <%= link_to "Remove filter?", :action => "index", :model => params[:model] %></p>
37+
<% end %>
38+
</div>
6339

64-
<script type="text/javascript">
65-
new Effect.Highlight('empty');
66-
</script>
40+
<script type="text/javascript">
41+
new Effect.Highlight('empty');
42+
</script>
6743

68-
<% end -%>
44+
<% end -%>
6945

70-
<% else -%>
46+
<% else -%>
7147

72-
<p><%= link_to "Add the first #{params[:model].singularize}", :action => "new", :model => params[:model] %></p>
48+
<p><%= link_to "Add the first #{params[:model].singularize}", :action => "new", :model => params[:model] %></p>
7349

74-
<% end -%>
50+
<% end -%>
51+
52+
</div>

public/images/spinner.gif

1.99 KB
Loading

0 commit comments

Comments
 (0)