0

I'm trying to build a form with drop downs that will query a json object with jQuery and then display that product with its data on my html page depending on what was chosen in the drop downs.

Here is my json object:

var obj = {
 "ProductOne":
 {
 "url":["http://www.google.com"],
 "map":["test-one"],
 "options":["test-one","test-three","test-four"],
 "dim":{
 "bam":"5",
 "tin":"4"
 },
 "acc":{
 "bam":"1",
 "tin":"2"
 }
 },
 "ProductTwo":
 {
 "url":["http://www.google-two.com"],
 "map":["test-two"],
 "options":["test-two","test-three","test-five"],
 "dim":{
 "bam":"6",
 "tin":"9"
 },
 "acc":{
 "bam":"8",
 "tin":"6"
 }
 }
 };

Here is my form:

<form name="searchForm" id="searchForm">
 <select name="dim">
 <option value="" selected="">Select One</option>
 <option value="5">5</option>
 <option value="4">4</option>
 <option value="6">6</option>
 <option value="9">9</option>
 </select>
 <select name="acc">
 <option value="" selected="">Select One</option>
 <option value="1">1</option>
 <option value="2">2</option>
 <option value="6">6</option>
 <option value="8">8</option>
 </select>
 <select name="options">
 <option value="" selected="">Select One</option>
 <option value="test-one">Test One</option>
 <option value="test-two">Test Two</option>
 <option value="test-three">Test Three</option>
 <option value="test-four">Test Four</option>
 <option value="test-five">Test Five</option>
 </select>
 <input id="button" type="button" value="submit"/>
 </form>
asked Jan 5, 2014 at 21:18
6
  • 3
    What have you tried so far? Can you explain what didn't work in your attempt to solve the problem? Can you post a live demo? Help us help you. Commented Jan 5, 2014 at 21:19
  • How do you want the results to be shown? in a table? or how? Commented Jan 5, 2014 at 21:24
  • @elclanrs i tried using jQuery.each to loop through it and then display it on my page. Commented Jan 5, 2014 at 21:32
  • @Edgar i want the products name and url to be displayed in div tags (ie...<div class="title">ProductOne<br/><a href="url">url</a></div>) Commented Jan 5, 2014 at 21:35
  • @user I added an answer with all you want :) Commented Jan 5, 2014 at 21:45

1 Answer 1

1

You can filter with this:

var filteredProducts = {},
 dim = $("select[name=dim]").val(),
 acc = $("select[name=acc]").val(),
 option = $("select[name=options]").val();
function valueInObject(object, value){
 for(var key in object){
 if(object[key] == value) return true;
 }
 return false; 
}
$.each(obj, function(key, product){
 if(
 (option == "" || $.inArray(option, product.options)) >= 0 && 
 (acc == "" || valueInObject(product.acc, acc)) &&
 (dim == "" || valueInObject(product.dim, dim)) 
 ){
 filteredProducts[key] = product;
 }
});
console.log(filteredProducts);
alert(JSON.stringify(filteredProducts));

Then, you have the filtered products in the filteredProducts object, that has same structure as the original obj object. Then you can traverse it and show it in a table or something.

Assuming you want to show them on a list, let's say you have:

<div id="filteredProducts"></div>

you would do:

$('#filteredProducts').empty(); //Clears previous results
$.each(filteredProducts, function(productKey, product){
 $('<div class="title">'+productKey+'<br/>'+
 '<a href="'+ product.url+'">'+ product.url + '</a>'+
 '</div>').appendTo('#filteredProducts');
}); 

It would add each product to the list.

Cheers, from La Paz, Bolivia

answered Jan 5, 2014 at 21:36
Sign up to request clarification or add additional context in comments.

16 Comments

thanks! i tried to apply the filter. I changed the submit button to a button and i added .click(function()) but it doesnt filter at all, it just shows both products all the time?
ok, its working now thank you...but in the options drop down when i choose Test One ProductTwo shows up and when i choose Test Two ProductOne shows up???
Right, does it have to filter by the map property of obj OR by the options array ?
I updated the answer, there was a small error in the $.inArray line. Here the demo jsfiddle.net/edgarinvillegas/V8CTV/2 Note: I'm igonring the map property, I'm filtering by the options array
btw if you wanted to filter with the map property instead of options, just change $.inArray(option, product.options)) by $.inArray(option, product.map)) Cheers
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.