I have a table that is populated by data in a JSON table and I would like to be able filter the table based on options in a select menu. How would I go about this?
JSON File is formatted like so, with categories having different numbers
"products" : [
{
"id" : "0",
"name" : "Logitech G910 Orion Spectrum RGB Mechanical Gaming Keyboard -
UK-Layout",
"price" : "119.99",
"category" : "0",
"description" : "Logitech 920-008017 G910 Orion Spectrum RGB Mechanical
Gaming Keyboard - Black.",
"button" : "<button type='button'>Add</button>",
"input" : "<input type='text' name='quantity'>",
"images" : [
{
"id" : "0",
"src" : "images/00.jpg"
},
{
"id" : "1",
"src" : "images/01.jpg"
},
{
"id" : "2",
"src" : "images/02.jpg"
}
]
}
Select menu:
`<select id= "dropDown">
<option value="">All</option>
<option value="0">Mice</option>
<option value="1">Keyboard</option>
<option value="2">Monitor</option>
</select>`
Table
<table id =myTable class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>Image</th>
</thead>
</table>
</table>
Script for table
$.getJSON('products.json', function(data){
var items = [];
$.each(data.products, function(key, val){
items.push("<tr>");
items.push("<td id=''"+key+"''>"+val.id+"</td>");
items.push("<td id=''"+key+"''>"+val.name+"</td>");
items.push("<td id=''"+key+"''>"+val.price+"</td>");
items.push("<td id=''"+key+"''>"+val.description+"</td>");
items.push("<td id=''"+key+"''>"+"<img src='"+val.image+"'/></td>");
items.push("</tr>");
});
$("<tbody/>", {html:items.join("")}).appendTo("table");
});