Skip to content

Commit

Permalink
searchresults get updated by ajax while typing
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaltom committed Mar 8, 2012
1 parent 231969b commit e4c4fbf
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 44 deletions.
4 changes: 3 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def searchresult
@packages = Seeker.prepare_result("#{params[:q]}", nil, nil, nil, nil)
@packagenames = @packages.map{|p| p.name}.uniq
@result = @packagenames.map{|p| {:name => p, :description_package => Rails.cache.read( "description_package_#{p}" ) } }

if request.xhr?
render :partial => 'find_results' and return
end
end


Expand Down
45 changes: 45 additions & 0 deletions app/views/search/_find_form.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<% content_for :ready_function do %>

$("#search_form").keyup(function() {

if ($("#search_form").val().length > 2) {
$.ajax({ url: "<%= url_for :controller => :search, :action => :searchresult %>",
cache: true,
data: {q: $("#search_form").val() },
beforeSend: function() {
$('#search-loader').show();
},
complete: function(){
$('#search-loader').hide();
},
success: function(html){
$("#search_result_container").html(html);
$('#msg').html("");
},
error: function(jqXHR, textStatus, errorThrown) {
$('#msg').html(textStatus + errorThrown);
},


});

} else {
$('#msg').html("Please enter more than 2 characters");
}

})

<% end -%>


<div class="box box-shadow grid_12 pkg-search-box">

<% form_tag( {:controller => 'search', :action => :searchresult}, :method => :get ) do %>
<%= text_field_tag 'q', @search_term, :size => 40, :id => "search_form" %>
<%= submit_tag _('Search'), :name => nil, :class => "search-go-button" %>
<% end -%>

</div>

<div id='search-loader' class="hidden"> updating search results... <%= image_tag "ajax-loader.gif" %></div>
<div id='msg'></div>
17 changes: 17 additions & 0 deletions app/views/search/_find_results.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@


<div class="box box-shadow grid_12">
<div class="box-header"> </div>

<% @result.each do |package| %>

<p> <%= image_tag( "http://screenshots.debian.net/thumbnail/" + package[:name], :height => 50 ) %>
<%= link_to highlight(package[:name], @search_term), :controller => :package, :action => :show, :package => package[:name] %>
<% if package[:description_package] %>
<i><%= highlight(package[:description_package].summary, @search_term) %></i>
<% end %>
</p>
<% end %>
<div class="box-footer"> </div>
</div>
28 changes: 0 additions & 28 deletions app/views/search/_search_form.rhtml

This file was deleted.

25 changes: 10 additions & 15 deletions app/views/search/searchresult.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
<% content_for :content_for_head do %>
<% end %>
<div class="box box-shadow grid_12 pkg-search-box">
<%= render :partial => 'search_form' %>
</div>
<div class="box box-shadow grid_12">
<div class="box-header"> </div>
<% @result.each do |package| %>
<%= image_tag( "http://screenshots.debian.net/thumbnail/" + package[:name], :width => 160 ) %>
<p><%= link_to highlight(package[:name], @search_term), :controller => :package, :action => :show, :package => package[:name] %>

(<%= highlight(package[:description_package].summary, @search_term) if package[:description_package] %>)
</p>
<% end %>
<div class="box-footer"> </div>
</div>
<%= render :partial => 'find_form' %>

<div id="search_result_container">
<%= render :partial => 'find_results' %>
</div>





Binary file added public/images/ajax-loader.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e4c4fbf

Please sign in to comment.