Instantly share code, notes, and snippets.
Last active
April 26, 2024 04:59
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save grantstandridge/11288295 to your computer and use it in GitHub Desktop.
Select Menu Filter (from json obj)
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
<!-- | |
DEMO FOR FILTERING JSON OBJECT & POPULATING DROPDOWN FIELDS. | |
=== | |
jQuery not required for the parsing/filtering | |
jQuery only required for retrieving json object | |
=== | |
by: Grant S | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1.0, user-scalable=0, minimal-ui" /> | |
<title>Json Dropdown Menu</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> | |
<style> | |
body{ | |
margin:0;padding:0; | |
font-size:62.5%; | |
font-family:"Avenir","Helvetica Neue","Helvetica",sans-serif; | |
} | |
a{ | |
text-decoration:none; | |
color:inherit; | |
} | |
.hide{ | |
display:none; | |
} | |
select{ | |
display:block; | |
} | |
</style> | |
</head> | |
<body> | |
<form action="index.php" method=""> | |
<select name="country" id="country"></select> | |
<select name="state" id="state"></select> | |
<select name="city" id="city" class="hide"></select> | |
<select name="storeId" id="storeName" class="hide"></select> | |
</form> | |
<script> | |
(function($){ | |
var myJson | |
; | |
$.getJSON("/stores.json", function(json){ | |
if (json.status) { | |
myJson = json.stores; | |
steppedSelect({ | |
"locations" : myJson | |
}); | |
} else { | |
alert(json.error); | |
} | |
}); | |
var steppedSelect = function(params){ | |
// PRIVATE VARZ | |
var cache = this, | |
geoObj = params.locations || myJson, | |
stateObj = [], | |
cityObj = [], | |
locationObj = [], | |
tempArray = [], | |
optionHtml = '<option selected="selected" disabled>select</option>' | |
; | |
var _construct = function(args){ | |
// AUTO FILTER BY COUNTRY & SET UP CLICK EVENT | |
initCountry(); | |
// 3 CLICK EVENTS | |
initState(); | |
initCity(); | |
initLocation(); | |
}; | |
var doChange = function(el) { | |
var event = new Event('change', { | |
'view': window, | |
'bubbles': false, | |
'cancelable': true | |
}); | |
var el = el, | |
canceled = !el.dispatchEvent(event) | |
; | |
if (!canceled) { | |
return | |
} else { | |
console.log("Error: There was a problem with your change handler"); | |
} | |
}; | |
var exec = function(args){ | |
var parentObj = args.parentObj || {}, | |
parentKey = args.parentKey || "", | |
parentMatch = args.parentMatch || "", | |
childObj = args.childObj || {}, | |
childKey = args.childKey || "", | |
childElement = args.childElement | |
; | |
optionHtml = '<option selected="selected" disabled>select</option>'; | |
for (i = 0; i < parentObj.length; i++) { | |
var obj = parentObj[i]; | |
// if we haven't already pulled this particular object out as an HTML option | |
if ( obj[parentKey] == parentMatch && tempArray.indexOf(obj[childKey]) === -1 ) { | |
tempArray.push(obj[childKey]); | |
// if we're setting up the "locations" menu, | |
// make the value equal to the storeId | |
if (childKey == "storeName") { | |
optionHtml += '<option value="'+ obj["storeId"] +'">'+ obj[childKey] +'</option>'; | |
// else, make the value equal to the name | |
} else { | |
optionHtml += '<option value="'+ obj[childKey] +'">'+ obj[childKey] +'</option>'; | |
} | |
} | |
// push all appropriate objects into object | |
// for looping through a child object | |
if ( obj[parentKey] == parentMatch ) { | |
childObj.push(obj); | |
} | |
} | |
tempArray = []; | |
childElement.innerHTML = optionHtml; | |
childElement.className = childElement.className.replace(/(?:^|\s)hide(?!\S)/, ''); | |
}; | |
var initCountry = function(args){ | |
for (i = 0; i < geoObj.length; i++) { | |
var obj = geoObj[i]; | |
if ( tempArray.indexOf(obj["country"]) === -1 ) { | |
tempArray.push(obj["country"]); | |
if (obj["country"] == "United States") { | |
optionHtml += '<option selected="selected" value="'+ obj["country"] +'">'+ obj["country"] +'</option>'; | |
} else { | |
optionHtml += '<option value="'+ obj["country"] +'">'+ obj["country"] +'</option>'; | |
} | |
} | |
} | |
tempArray = []; | |
document.getElementById("country").innerHTML = optionHtml; | |
document.getElementById("country").className = document.getElementById("country").className.replace(/(?:^|\s)hide(?!\S)/, ''); | |
exec({ | |
parentObj: geoObj, | |
parentKey: "country", | |
parentMatch: document.getElementById("country").options[document.getElementById("country").selectedIndex].value, | |
childObj: stateObj, | |
childKey: "state", | |
childElement: document.getElementById("state") | |
}); | |
document.getElementById("country").addEventListener("change", function(e){ | |
if ( document.getElementById("city").className.indexOf("hide")===-1 ) { | |
document.getElementById("city").className += " hide"; | |
} | |
if ( document.getElementById("storeName").className.indexOf("hide")===-1 ) { | |
document.getElementById("storeName").className += " hide"; | |
} | |
if (stateObj.length) { | |
stateObj = []; | |
} | |
exec({ | |
parentObj: geoObj, | |
parentKey: "country", | |
parentMatch: this.value, | |
childObj: stateObj, | |
childKey: "state", | |
childElement: document.getElementById("state") | |
}); | |
if ( document.getElementById("state").length < 3 && document.getElementById("state").options[1].value == "" ) { | |
var elem = document.getElementById("state"), | |
elemClass = elem.className | |
; | |
elem.selectedIndex = 1; | |
doChange(elem); | |
if ( elemClass.indexOf("hide")===-1 ) { | |
elem.className += " hide"; | |
} | |
} | |
}); | |
}; | |
var initState = function(args){ | |
document.getElementById("state").addEventListener("change", function(e){console.log(e); | |
if (cityObj.length) { | |
cityObj = []; | |
} | |
if ( document.getElementById("storeName").className.indexOf("hide")===-1 ) { | |
document.getElementById("storeName").className += " hide"; | |
} | |
exec({ | |
parentObj: stateObj, | |
parentKey: "state", | |
parentMatch: this.value, | |
childObj: cityObj, | |
childKey: "city", | |
childElement: document.getElementById("city") | |
}); | |
}); | |
}; | |
var initCity = function(args){ | |
document.getElementById("city").addEventListener("change", function(e){console.log(e); | |
if (locationObj.length) { | |
locationObj = [] | |
} | |
exec({ | |
parentObj: cityObj, | |
parentKey: "city", | |
parentMatch: this.value, | |
childObj: locationObj, | |
childKey: "storeName", | |
childElement: document.getElementById("storeName") | |
}); | |
}); | |
}; | |
var initLocation = function(args){ | |
document.getElementById("storeName").onchange = function(){ | |
selectedLocation = this.value; | |
alert(selectedLocation); | |
}; | |
}; | |
_construct(); | |
}; | |
})(jQuery); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parses single json object through object literal, jQuery not required!