Last active
December 24, 2015 20:39
-
-
Save mitchdowney/6859229 to your computer and use it in GitHub Desktop.
See anything wrong with this Bootstrap 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
<head> | |
<script type="text/javascript"> | |
$('.searchAhead').typeahead({ | |
source: function(query, process) { | |
return $.get('/lookup/university/', { query: query }, function (data) { | |
return process(data.options); | |
}); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<form method="post">{% csrf_token %} | |
<label>My Label</label> | |
<input type="text" data-provide="typeahead" class="searchAhead" name="i1" /> | |
<input type="submit" value="Go!" class="button" /> | |
</form> | |
</body> |
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
from everyvote_mini.models import ParentConstituency | |
from django.http import HttpResponse | |
from django.utils.html import escape | |
# TYPEAHEAD PARENT CONSTITUENCY | |
def typeahead(request, search_type): | |
resultSet = {} | |
if search_type == 'university': | |
if request.method == "GET": | |
if request.GET.has_key(u'query'): | |
query = request.GET[u'query'] | |
obj = ParentConstituency.objects.filter(name__contains=query) | |
results = [ escape(x.name) for x in obj ] | |
resultSet["options"] = results | |
return HttpResponse(json.dumps(resultSet), content_type="application/json") |
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
# TYPEAHEAD | |
url(r'^lookup/?P<search_type>\w+)/$', 'everyvote_mini.views.typeahead.typeahead', name='typeahead'), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment