Topic: Displaying results with a list view or grid view for Ajax Search

I've implemented a live search with ajax, it works okay. I also want the users to choose the way results are displayed: List View or Detail View, but I'm having some problems.

Here's the search form:

<%= form_remote_tag 
   :url => { :action => 'simple' },
   :html => { :id => 'liveSearch', :name => 'liveSearch' }
<%= text_field_tag
   { :class => 'textfield', :style => 'width: 160px;'}
<%= observe_form(
   :frequency => 0.5,
   :update => 'ajaxCymbalSearch',
   :url => { :controller => 'search', :action => 'simple' }
<%= end_form_tag %>

The results are displayed using a partial:
<div id="ajaxCymbalSearch">
<%= render(:partial => 'simpleResults') %>

And the partial has an if statement to decide the display type, which uses an instance variable:
<% if @display == "list" %>
   --- List View Code ---
<% else %>
   --- Detail View Code ----
<% end %>

So, the live search works OK. And my initial idea was to toggle @display value to change between views (the display buttons are ajaxed too). So when you change a display type, it should change the @display variable, and re-render the partial 'simpleResults'. But while doing it, the search parameters are being lost.

The display buttons are like this:

<%= link_to_remote 
   ( image_tag('icon_view_list.gif',
        :style => 'border: 0px;',
        :alt => 'List View',
        :title => 'List View'),
   { :update => 'ajaxCymbalSearch',
     :url => { :controller => 'search', :action => 'view', :type => 'list'},
   { :class => 'link_general' } )
#The same goes for detail view, with only change :type => 'detail'

The view action changes the @display instance variable, and renders the partial 'simpleResults'

So I have to maintain the results on the simpleResults partial, and change only the display type. But couldn't find a simple way to do it. I can pass all the search parameters (there's only searchName for now, but there will also be an advanced version of this search with 5-6 parameters). Or I can try to pass the results as an instance variable, but of all this seems too much work for this.

Is there a way to maintain all the data in a partial, and just render it again with a new @display variable?

Last edited by maes (2007-09-30 16:21:33)