Topic: AJAX List Filter with Select Tag

Title says it all. I want to instantly filter down the results of an index by a select tag which lists the rows of one table column.

I stumbled across the Double Filter plugin, but the repository seems to be down..either way its probably overkill for what I want.

Re: AJAX List Filter with Select Tag

#controller
def index
  if params[:filter] # if filter is sent get only items with this filter
    @items.find :all, :conditions => ["something  = ?",params[:filter]]
  else #find all items
    @items.find :all
  end
 
  #get all distinct items
  @filter_items = Item.find :all, :select => "DISTINCT(something)", :order => "something ASC"
  #extract the "something" attributes values into an array hat can be used for the select.
  @filters = @filter_items.map { |i| [i.something,i.something]}
  # results in an array like [[value1,value1],[value2,value2],...]

#  .... Rest of code
end

#view
<select name="filter" id="filter">
<%= options_for_select @filters %>
#<option value="value1">value1</option>......
</select>

#Add an Observer for the dropdown field, make it call the index action
# by default, it will send the value of the observed field as an URL parameter
<%= observe_field "filter", :url => {action => index } %>


untested and probably full of little errors but i think you get the idea wink

Last edited by Duplex (2007-08-12 08:16:16)