Progressive AJAX Filter

Hi Guys,

In my rails app, I present a table of data that I want to be able to
filter using multiple criteria. I've created a one-line table of
input fields above my table to accept filter criteria like this...

<%= javascript_include_tag :defaults %>
&nbsp;
<form name="sform" action="" style="display:inline;"
id="serverSelectTable">
  <table style="padding-right: 2px;margin-bottom: 0;">
    <tr>
      <td width="75px" align="right"><label>Search:&nbsp;</label></td>
      <td><%= text_field_tag("namequery",
@params['namequery'], :size => 19) %><td>
      <td>&nbsp;</td>
      <td><%= text_field_tag("ipquery", @params['ipquery'], :size
=> 17 ) %><td>
<--additional rows deleted here--->
</table>
</form>

<%= observe_field 'namequery', :frequency => 1,
         :update => 'systemTable',
         :before => "Element.show('spinner')",
         :success => "Element.hide('spinner')",
         :url => {:action => 'select'},
         :with => 'namequery' %>
<%= observe_field 'ipquery', :frequency => 1,
         :update => 'systemTable',
         :before => "Element.show('spinner')",
         :success => "Element.hide('spinner')",
         :url => {:action => 'select'},
         :with => 'ipquery' %>

  <...additional observers deleted here>
<div id="systemTable">
  <%= render(:partial => "system_table") %>
</div>

In my controller, I have this code to process entries in those fields:

    unless params[:namequery] == "" || params[:namequery].nil?
      conditions = "host_name LIKE \'#{@params[:namequery]}%\'"
    end
    unless params[:ipquery] == "" || params[:ipquery].nil?
      ipcondition = "ip_address like \'#{@params[:ipquery]}%\'"
      conditions = conditions.nil? ? ipcondition : "#{conditions} AND
                          #{ipcondition}"
    end

I then pass use those criteria to filter the collection I send to the
partial which displays the table of data:

    @systems = System.find(:all, :order => sort, :conditions =>
conditions)

I want to be able to enter a partial system name and a partial ip
address, e.g., and return all systems that meet both criteria.

Both filter fields work correctly alone, but if I enter one and then
the other, only the second one entered makes it to the conditions
clause. Using debug statements, I can see in the params[] fields only
get updated right after the entry is detected. On the next pass
through, the params value for the previously entered field remains ""
even though the input is still visible in the entry field.

How do I get around this problem and detect the value that was entered
previously?

Thanks for any advice you can give me.

-- Mike

Hello,

    unless params[:namequery] == "" || params[:namequery].nil?

Just while we're here, you could shorten this to:

   unless params[:namequery].blank?

Both filter fields work correctly alone, but if I enter one and then
the other, only the second one entered makes it to the conditions
clause. Using debug statements, I can see in the params[] fields only
get updated right after the entry is detected. On the next pass
through, the params value for the previously entered field remains ""
even though the input is still visible in the entry field.

How do I get around this problem and detect the value that was entered
previously?

At the moment each observer passes along to the controller only the field it is observing. I would change your view to observe the whole form rather individual fields.

See the documentation here:

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000536

Regards,
Andy Stewart

Hi Andy,

That's exactly what I needed and it worked like a champ. Thank you so
much for taking the time to help me. I appreciate your pointing out
the blank? method too.

Regards,

Mike