Last active
March 23, 2016 15:20
-
-
Save biganth/5846871 to your computer and use it in GitHub Desktop.
Rails search with Twitter Typeahead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="search_form" action="/listings/search" method="get"> | |
<%= hidden_field_tag :search_item_id %> | |
<%= hidden_field_tag :search_item_type %> | |
<fieldset> | |
<span class="search"> | |
<% q = params[:term].to_s.empty? ? "Instant search result as you type" : params[:term] %> | |
<script>var start_search = true</script> | |
<input type="text" class="input" id="search_field" name="q" value="<%=q%>" onclick="if(this.value=='Instant search results as you type'){this.value=''};start_search=true;$('search_results').hide()" autocomplete="off" /> | |
<%= indicator("search_ind") %> | |
</span> | |
</fieldset> | |
</form> | |
<div id="search_results" style="border: 3px solid #63A3F1; border-top: 0px; display:none; margin-top: 0px; padding:12px; text-align: left; position:relative; top: 85px; width: 650px; background: white;"> | |
</div> | |
<script> | |
jQuery("#search_field").autocomplete({ | |
source: "/listings/search", | |
minLength: 2, | |
select: function(event, ui){ | |
start_search=false; | |
$('search_item_id').value = ui.item.id; | |
$('search_item_type').value = ui.item.klass; | |
$('search_form').submit(); | |
} | |
}); | |
</script> | |
</div> | |
<div class="mobile_apps"> <strong>Try our mobile apps</strong> <span><img src="/images/img1.png" alt="" /> <img src="/images/img2.png" alt="" /></span> </div> | |
</div> | |
<% if false %> | |
<div id="search"> | |
<p>Find any business phone number in 3 seconds. <span class="orange">Try it!</span></p> | |
<form id="search_form" action="/search" method="get"> | |
<%= hidden_field_tag :search_item_id %> | |
<%= hidden_field_tag :search_item_type %> | |
<div id="search-default"> | |
<% q = params[:q].to_s.empty? ? "Enter a business name or a business type" : params[:q] %> | |
<script>var start_search = true</script> | |
<div style="margin-left:30px;"> | |
<input type="text" class="search" id="search_field" name="q" value="<%=q%>" onclick="if(this.value=='Enter a business name or a business type'){this.value=''};start_search=true;$('search_results').hide()" autocomplete="off" /> <%= indicator("search_ind") %> | |
<br /> | |
<span class="small">Example: Chefette, Chickmont or Hotels, Taxis...</span> | |
</div> | |
<div id="search_results" style="border: 3px solid #dddddd; border-top: 0px; display:none; margin-top: 3px; padding:12px; background-color:#ffffff; text-align: left"></div> | |
<%= observe_field 'search_field', :url => search_listings_path, :frequency => 1, :with => "'q='+$('#search_field').value", :before => "if($('search_field').value.length<2 || start_search==false){$('search_results').hide();return false;}", :loading => "$('search_ind').show()", :complete => "$('search_ind').hide()" %> | |
</div> | |
</form> | |
</div> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment