Ok I took this to the next step, it is not optimal, but it seems to
work. I added an autocompleter to the table, and when you type in that
it finds the field you want, when you select the field it acts as a
filter and find the matching rows and displays all mathches in the
table, in my case it always matches just one item.
I modified the component.rhtml here just before the table headings...
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<td colspan="<%= num_columns %>">
Search for alias:
<%= text_field_with_auto_complete :alias, :alias, {:size =>
15 },
:after_update_element => "function(element,value){" +
remote_function(:url => { :action => :component_update }, :update =>
scaffold_content_id(params), :with =>"'alias='+element.value") + "}" %>
</td>
</tr>
<tr>
<%= render :partial => 'column_headings' %>
</tr>
</thead>
Added this to the controller...
auto_complete_for :alias, :alias
and added this to the def component method...
if params[:alias].nil? || params[:alias].empty?
@paginator, @login_accounts = paginate(:login_accounts,
:order_by => @sort_by, :per_page => default_per_page)
else
@paginator, @login_accounts= paginate(:login_accounts, :joins
=> "LEFT JOIN alias_tbl ON alias_tbl.login_id =
login_account_tbl.login_id", :conditions => ["alias ilike ?",
"%"+params[:alias]+"%"], :order_by => @sort_by, :per_page =>
default_per_page )
end
and to quote Richard "..et Viola" ;) (sic)
(I know I am using a legacy DB here so your exact parameters will be
different.)
I am also actually filtering on something that is in a has_many
association, hence the :joins in the paginate. But you should get the
idea.
Problems are I need a simple way to cancel the filter. (probably a
button next to it saying cancel)
*****************************************************************
(couldn't reply to original message since it was more than 30 days old)
This is exactly what I was looking for, awesome! I'm learning Rails
by trying to customize an ajax scaffold, so having thing layed out
explicitly really helps. Anyway, I managed to get canceling to work by
using an observer. You mentioned it in another post so I looked up the
syntax, and with some trial and error I figured out what works:
<%=
text_field_with_auto_complete :model, :field_name,
{:size => 15, :value => params[:field_name] },
:after_update_element => "function(element,value){" +
remote_function(:url => { :action => :component_update }, :update
=>
scaffold_content_id(params), :with
=>"'field_name='+element.value") + "}"
%>
<%= observe_field(:model_field_name,
:function =>
remote_function(:url => { :action => :component_update }, :update
=>
scaffold_content_id(params), :with
=>"'field_name='+element.value"))
%>
I also added the the :value parameter to the default value in the field
name. Now you don't even need to click on the field -- mousing off the
field will trigger an update, as well as hitting return.
Anyway, it would be nice to take this one step futher and allow the
search to be applied to more than one field -
so...instead of just searching against the name field, maybe we want to
search for records which match in any of a number of fields.
this doesn't work....
:conditions => ["name like ?", "%"+params[:name]+"%"] or ["address like
?", "%"+params[:name]+"%"] or ["state like ?", "%"+params[:name]+"%"],
buy what will?
I've tried it on a table with a filter-box for each column. To allow
filtering by more than one field at time, I'm using session variables
(surely there's a better way).
<%= text_field_with_auto_complete :model, :field, {:size => 10 } .....
<%= text_field_with_auto_complete :model2, :field, {:size => 10 } .....
...
On the controller, two methods to manage session variables:
def clear_filters
session[:model_filter] = nil
...
return_to_main
end
def update_filters
session[:model_filter] = params[:model] unless params[:model].nil?
|| params[:model].empty?
...
end
and at the component method:
update_filters
@model_filter = session[:model_filter]
@model2_filter = session[:model2_filter]
@filters = ["model.field = '", @model_filter, "'" ] unless
@model_filter.nil?
unless @model2_filter.nil?
if @filters.nil?
@filters = ["model2.field2 = '", @model_filter, "'" ]
else
@filters += [" AND model2.field2 = '", @model2_filter, "'" ]
end
end
end
@paginator = paginate(........
:conditions => "#{@filters}"
)
Thanks again!
I think its in the call to :component_update is going to the parent
scaffold controller instead of the child. I've tried specifying
:controller=> but that doesn't seem to help.
<%= text_field_with_auto_complete :run_script, :name, {:size => 15 },
:after_update_element => "function(element,value){" +
remote_function(:url => {:action => :component_update }, :update =>
scaffold_content_id(params),
:with =>"'name='+element.value") + "}" %>