Skip to content

Commit

Permalink
9 - Auto Search
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Oct 7, 2017
1 parent 2072edc commit c433c4a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/search/templates/search/snippets/search-form.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form method='GET' action='{% url "search:query" %}' class="form my-2 my-lg-0">
<form method='GET' action='{% url "search:query" %}' class="form my-2 my-lg-0 search-form">
<div class='input-group'>
<input class="form-control" type="text" placeholder="Search" name='q' aria-label="Search" value='{{ request.GET.q }}'>
<span class='input-group-btn'>
Expand Down
34 changes: 34 additions & 0 deletions src/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,40 @@

<script>
$(document).ready(function(){
// Auto Search
var searchForm = $(".search-form")
var searchInput = searchForm.find("[name='q']") // input name='q'
var typingTimer;
var typingInterval = 500 // .5 seconds
var searchBtn = searchForm.find("[type='submit']")
searchInput.keyup(function(event){
// key released
clearTimeout(typingTimer)

typingTimer = setTimeout(perfomSearch, typingInterval)
})

searchInput.keydown(function(event){
// key pressed
clearTimeout(typingTimer)
})

function displaySearching(){
searchBtn.addClass("disabled")
searchBtn.html("<i class='fa fa-spin fa-spinner'></i> Searching...")
}

function perfomSearch(){
displaySearching()
var query = searchInput.val()
setTimeout(function(){
window.location.href='/search/?q=' + query
}, 1000)

}


// Cart + Add Products
var productForm = $(".form-product-ajax") // #form-product-ajax

productForm.submit(function(event){
Expand Down

0 comments on commit c433c4a

Please sign in to comment.