Commit 2a226d3d authored by Tyler Hallada's avatar Tyler Hallada
Browse files

Filter search results instead of highlighting.

Also, made var restaurants in js a global variable so other scripts can access
the data (e.g. typeAhead.js).
parent bf5c9be7
function sort_restaurants(restaurants) { var restaurants = [];
var open = $.grep(restaurants,
function sort_restaurants(filtered_restaurants) {
var open = $.grep(filtered_restaurants,
function (r, idx) { return (r.open === true) }); function (r, idx) { return (r.open === true) });
var closed = $.grep(restaurants, var closed = $.grep(filtered_restaurants,
function (r, idx) { return (r.open === false) }); function (r, idx) { return (r.open === false) });
return $.merge(open, closed); return $.merge(open, closed);
} }
function construct_grid(restaurants) { function construct_grid(filtered_restaurants) {
restaurants = sort_restaurants(restaurants); // Hide footer while generating grid to prevent it flying across the screen
$.each(restaurants, function (idx, restaurant) { $('#footer').hide();
$('#grid').empty();
$('#grid').html('<div class="row"></div>');
if (filtered_restaurants.length == 0) {
$('#grid').append('<span class="span2 offset5">No results found.</span>');
return;
}
sorted_restaurants = sort_restaurants(filtered_restaurants);
$.each(sorted_restaurants, function (idx, restaurant) {
var open_class = 'closed'; var open_class = 'closed';
if (restaurant.open) { if (restaurant.open) {
open_class = 'opened'; open_class = 'opened';
...@@ -25,16 +35,13 @@ function construct_grid(restaurants) { ...@@ -25,16 +35,13 @@ function construct_grid(restaurants) {
); );
} }
}); });
$('#footer').show();
} }
$.ajax({ $.ajax({
url: '/ajax/schedule/', url: '/ajax/schedule/',
}).done(function (data) { }).done(function (data) {
// Hide footer while generating grid to prevent it flying across the screen restaurants = data.data;
$('#footer').hide();
$('#grid').empty();
$('#grid').html('<div class="row"></div>');
var restaurants = data.data;
$.each(restaurants, function (idx, restaurant) { $.each(restaurants, function (idx, restaurant) {
var now = new Date(); var now = new Date();
var date = new Date().setHours(0,0,0,0); var date = new Date().setHours(0,0,0,0);
...@@ -101,5 +108,4 @@ $.ajax({ ...@@ -101,5 +108,4 @@ $.ajax({
}); });
}); });
construct_grid(restaurants); construct_grid(restaurants);
$('#footer').show();
}); });
...@@ -2,38 +2,27 @@ $.ajax({ ...@@ -2,38 +2,27 @@ $.ajax({
url: '/ajax/schedule/', url: '/ajax/schedule/',
}).done(function (data) { }).done(function (data) {
//collecting list of restaurant names from server data //collecting list of restaurant names from server data
var restaurants = []; var rest_names = [];
for (var i = 0; i < data.data.length; i++) { for (var i = 0; i < data.data.length; i++) {
restaurants.push(data.data[i].name); rest_names.push(data.data[i].name);
}; };
//Monitors for keyboard activity to detect when the input field is empty
$('#searchBar').keyup(function() {
var value = $('#searchBar').val();
if (value == 0){
//resets all backgrounds to white if the search bar is empty
$('.open').css('background-color','white');
$('.closed').css('background-color','white');
}
});
// For doumentation on jQuery's autocomplete: (api.jqueryui.com/autocomplete) // For doumentation on jQuery's autocomplete: (api.jqueryui.com/autocomplete)
$("#searchBar").autocomplete({ $("#searchBar").autocomplete({
source: restaurants, source: rest_names,
//making it so the search result list doesn't physically appear //making it so the search result list doesn't physically appear
messages: { messages: {
noResults: '', noResults: '',
results: function(){} results: function(){}
}, },
minLength: 0,
response: function(event, ui) { response: function(event, ui) {
//resets all backgrounds to white if the search bar is edited
$('.open').css('background-color','white');
$('.closed').css('background-color','white');
//ui.content array contains all names that are returned from the search //ui.content array contains all names that are returned from the search
for (var result in ui.content){ results = $.map(ui.content, function(r) { return r.value; });
//Highlights all search results filtered = $.grep(restaurants, function (r, idx) {
$('.open:contains("'+ui.content[result].value+'")').css('background-color','#FDFFBF'); return ($.inArray(r.name, results) != -1);
$('.closed:contains("'+ui.content[result].value+'")').css('background-color','#FDFFBF'); });
} construct_grid(filtered);
// To prevent the page width from extending // To prevent the page width from extending
$('.ui-autocomplete').remove(); $('.ui-autocomplete').remove();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment