Skip to content

Commit 66a1303

Browse files
committed
Select filtering, multiple filter combination fix in filterData function,
1 parent d1f281a commit 66a1303

File tree

4 files changed

+235
-105
lines changed

4 files changed

+235
-105
lines changed

dist/categories-example.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ <h1>Using Chipotle as an Example</h1>
7272
</label>
7373
</li>
7474
</ul>
75+
76+
<ul id="city-filter" class="bh-storelocator-filters">
77+
<li><h3>City</h3></li>
78+
<li>
79+
<select name="city">
80+
<option value="">All cities</option>
81+
<option value="Minneapolis">Minneapolis</option>
82+
<option value="Bloomington">Bloomington</option>
83+
<option value="Golden Valley">Golden Valley</option>
84+
<option value="St. Louis Park">St. Louis Park</option>
85+
</select>
86+
</li>
87+
</ul>
7588
</div>
7689
</form>
7790

@@ -93,6 +106,7 @@ <h1>Using Chipotle as an Example</h1>
93106
$(function() {
94107
$('.bh-storelocator-map-container').storeLocator({
95108
'taxonomyFilters' : {
109+
'city' : 'city-filter',
96110
'category' : 'category-filters-container1',
97111
'features' : 'category-filters-container2'
98112
}

dist/js/jquery.storelocator.js

Lines changed: 110 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-06-22
1+
/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-06-29
22
* http://www.bjornblog.com/web/jquery-store-locator-plugin
33
* Copyright (c) 2014 Bjorn Holine; Licensed MIT */
44

@@ -220,20 +220,47 @@ $.fn.storeLocator = function(options){
220220
*/
221221
function checkFilters(){
222222
$.each(settings.taxonomyFilters,function(k, v){
223-
//Find the existing checked boxes for each filter
223+
//Find the existing checked boxes for each checkbox filter
224224
$(v + ' input[type=checkbox]').each(function(){
225225
if($(this).prop('checked')){
226-
var catVal = $(this).attr('id');
226+
var filterVal = $(this).attr('id');
227227

228228
//Only add the taxonomy id if it doesn't already exist
229-
if(filters[k].indexOf(catVal) === -1){
230-
filters[k].push(catVal);
229+
if(filters[k].indexOf(filterVal) === -1){
230+
filters[k].push(filterVal);
231231
}
232232
}
233233
});
234+
235+
//Find the existing selected value for each select filter
236+
$(v + ' select').each(function(){
237+
var filterVal = $(this).attr('id');
238+
239+
//Only add the taxonomy id if it doesn't already exist
240+
if(filters[k].indexOf(filterVal) === -1){
241+
filters[k].push(filterVal);
242+
}
243+
});
234244
});
235245
}
236246

247+
/**
248+
* Get the filter key from the taxonomyFilter setting
249+
*
250+
* @param filterContainer {string} ID of the changed filter's container
251+
*/
252+
function getFilterKey(filterContainer){
253+
for(var key in settings.taxonomyFilters){
254+
if(settings.taxonomyFilters.hasOwnProperty(key)){
255+
for(var i = 0; i< settings.taxonomyFilters[key].length; i++){
256+
if(settings.taxonomyFilters[key] === filterContainer){
257+
return key;
258+
}
259+
}
260+
}
261+
}
262+
}
263+
237264
//Taxonomy filtering
238265
if(settings.taxonomyFilters !== null){
239266

@@ -243,72 +270,100 @@ $.fn.storeLocator = function(options){
243270
});
244271

245272
//Handle filter updates
246-
$('.bh-storelocator-filters-container').on('change.'+prefix, 'input', function(e){
273+
$('.bh-storelocator-filters-container').on('change.'+prefix, 'input, select', function(e){
247274
e.stopPropagation();
248275

249-
//First check if it's a checkbox or select drop-down
250-
if($(this).is('input[type="checkbox"]')){
251-
var catId, locFilterContainer, locFilterKey; //TODO: Change variable loc names to something else
276+
var filterId, filterContainer, filterKey;
252277

278+
//Handle checkbox filters
279+
if($(this).is('input[type="checkbox"]')){
253280
//First check for existing selections
254281
checkFilters();
255282

256-
catId = $(this).val();
257-
locFilterContainer = $(this).closest('ul').attr('id');
283+
filterId = $(this).val();
284+
filterContainer = $(this).closest('.bh-storelocator-filters').attr('id');
285+
filterKey = getFilterKey(filterContainer);
258286

259-
//Get the correct filter key
260-
for(var key in settings.taxonomyFilters){
261-
if(settings.taxonomyFilters.hasOwnProperty(key)){
262-
for(var i = 0; i< settings.taxonomyFilters[key].length; i++){
263-
if(settings.taxonomyFilters[key] === locFilterContainer){
264-
locFilterKey = key;
287+
if(filterKey){
288+
//Add or remove filters based on checkbox values
289+
if($(this).prop('checked')){
290+
//Add ids to the filter arrays as they are checked
291+
filters[filterKey].push(filterId);
292+
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
293+
reset();
294+
if((olat) && (olng)){
295+
settings.mapSettings.zoom = 0;
296+
begin_mapping();
297+
}
298+
else{
299+
mapping(originalData);
265300
}
266301
}
267302
}
268-
}
269-
270-
//Add or remove filters based on checkbox values
271-
if($(this).prop('checked')){
272-
//Add ids to the filter arrays as they are checked
273-
filters[locFilterKey].push(catId);
274-
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
275-
reset();
276-
if((olat) && (olng)){
277-
settings.mapSettings.zoom = 0;
278-
begin_mapping();
279-
}
280-
else{
281-
mapping(originalData);
303+
else {
304+
//Remove ids from the filter arrays as they are unchecked
305+
var filterIndex = filters[filterKey].indexOf(filterId);
306+
if(filterIndex > -1){
307+
filters[filterKey].splice(filterIndex, 1);
308+
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
309+
reset();
310+
if((olat) && (olng)){
311+
if(countFilters() === 0){
312+
settings.mapSettings.zoom = originalZoom;
313+
}
314+
else{
315+
settings.mapSettings.zoom = 0;
316+
}
317+
begin_mapping();
318+
}
319+
else {
320+
mapping(originalData);
321+
}
322+
}
282323
}
283324
}
284325
}
285-
else {
286-
//Remove ids from the filter arrays as they are unchecked
287-
var filterIndex = filters[locFilterKey].indexOf(catId);
288-
if(filterIndex > -1){
289-
filters[locFilterKey].splice(filterIndex, 1);
326+
}
327+
//Handle select filters
328+
else if($(this).is('select')){
329+
//First check for existing selections
330+
checkFilters();
331+
332+
filterId = $(this).val();
333+
filterContainer = $(this).closest('.bh-storelocator-filters').attr('id');
334+
filterKey = getFilterKey(filterContainer);
335+
336+
//Check for blank filter on select since default val could be empty
337+
if(filterId){
338+
if(filterKey){
339+
filters[filterKey] = [filterId];
290340
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
291341
reset();
292342
if((olat) && (olng)){
293-
if(countFilters() === 0){
294-
settings.mapSettings.zoom = originalZoom;
295-
}
296-
else{
297-
settings.mapSettings.zoom = 0;
298-
}
343+
settings.mapSettings.zoom = 0;
299344
begin_mapping();
300345
}
301-
else {
346+
else{
302347
mapping(originalData);
303348
}
304349
}
305350
}
306351
}
352+
//Reset if the default option is selected
353+
else{
354+
if(filterKey){
355+
filters[filterKey] = [];
356+
}
357+
reset();
358+
if((olat) && (olng)){
359+
settings.mapSettings.zoom = originalZoom;
360+
begin_mapping();
361+
}
362+
else{
363+
mapping(originalData);
364+
}
365+
}
307366
}
308-
//Select filters
309-
/*else {
310-
311-
}*/
312367
});
313368
}
314369

@@ -721,15 +776,18 @@ $.fn.storeLocator = function(options){
721776
});
722777
}
723778

724-
function filterData(data, filters) {
779+
function filterData(data, filters){
780+
var filterTest = true;
781+
725782
for (var k in filters) {
726783
if (!(new RegExp(filters[k].join("")).test(data[k]))){
727-
return false;
728-
}
729-
else{
730-
return true;
784+
filterTest = false;
731785
}
732786
}
787+
788+
if(filterTest){
789+
return true;
790+
}
733791
}
734792

735793
//Taxonomy filtering setup

0 commit comments

Comments
 (0)