这是您的代码...现场演示
HTML
您的数据部分没有变化,只是添加了select
元素。
<select id="alpha" onchange="selectData();">
<option value="">Choose A-Z</option>
<option value="0">0-9</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<select id="store" onchange="selectData();">
<option value="">Any Store</option>
<option value="Arnotts">Arnotts</option>
<option value="BrownThomas">BrownThomas</option>
<option value="Dundrum">Dundrum</option>
</select>
<select id="category" onchange="selectData();">
<option value="">Any Category</option>
<option value="Mens Wear">Mens Wear</option>
<option value="Shoes">Shoes</option>
<option value="Womens Wear">Womens Wear</option>
<option value="Cosmetics">Cosmetics</option>
</select>
<div classs="brand-holder">
<div class="brand-column">
<h3>0-9</h3>
<ul class="brand-list">
<li data-alpha="0" data-store="Arnotts,BrownThomas" data-category="Mens Wear, Shoes">Brand name <i>[store:Arnotts,BrownThomas :: category: Mens Wear, Shoes]</i></li>
</ul>
</div>
<div class="brand-column">
<h3>A</h3>
<ul class="brand-list">
<li data-alpha="A" data-store="Arnotts,Dundrum" data-category="Womens Wear, Cosmetics" >A Brand Name <i>[store:Arnotts,Dundrum :: category: Womens Wear, Cosmetics]</i></li>
</ul>
</div>
</div>
CSS
.hidden {
display:none;
}
Javascript
function selectData() {
var alpha = document.getElementById('alpha').value;
var store = document.getElementById('store').value;
var category = document.getElementById('category').value;
var i;
var j;
var flag;
items = document.getElementsByTagName("li");
// Hide/show items
for(i = 0; i < items.length; i++) {
a = items[i].getAttribute('data-alpha');
s = items[i].getAttribute('data-store').split(",");
c = items[i].getAttribute('data-category').split(",");
// To be safe
for(j = 0; j < s.length; j++) s[j] = s[j].trim();
for(j = 0; j < c.length; j++) c[j] = c[j].trim();
if((alpha == "" || a == alpha) && (store == "" || s.indexOf(store) != -1) && (category == "" | c.indexOf(category) != -1)) {
items[i].classList.remove("hidden");
} else {
items[i].classList.add("hidden");
}
}
// Clear empty groups
groups = document.getElementsByClassName("brand-list");
for(i = 0; i < groups.length; i++) {
flag = true;
for(j = 0; j < groups[i].childNodes.length; j++) {
node = groups[i].childNodes[j];
if(node.nodeName.trim() == "LI" && (node.className == "" || node.className.indexOf('hidden') == -1)) {
flag = false;
break;
}
}
// Remove the whole div
if (flag) {
groups[i].parentNode.classList.add("hidden");
} else {
groups[i].parentNode.classList.remove("hidden");
}
}
}
jQuery (而不是 Javascript)
$(document).ready(function() {
function selectData() {
var alpha = $("#alpha").val();
var store = $("#store").val();
var category = $("#category").val();
var j;
// Hide items
$("li").each(function() {
a = $(this).attr('data-alpha');
s = $(this).attr('data-store').split(",");
c = $(this).attr('data-category').split(",");
// To be safe
for(j = 0; j < s.length; j++) s[j] = s[j].trim();
for(j = 0; j < c.length; j++) c[j] = c[j].trim();
if((alpha == "" || a == alpha) && (store == "" || s.indexOf(store) != -1) && (category == "" | c.indexOf(category) != -1)) {
$(this).removeClass('hidden');
} else {
$(this).addClass('hidden');
}
});
// Hide empty sections
$(".brand-list").each(function() {
if($(this).children("li").not(".hidden").length > 0) {
$(this).parent().removeClass("hidden");
} else {
$(this).parent().addClass("hidden");
}
});
}
$("#alpha").on("change", selectData);
$("#store").on("change", selectData);
$("#category").on("change", selectData);
});
解决方案:2 现场演示
HTML
<select id="alpha"">
<option value="">Choose A-Z</option>
<option value="0">0-9</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<select id="store"">
<option value="">Any Store</option>
<option value="Arnotts">Arnotts</option>
<option value="BrownThomas">BrownThomas</option>
<option value="Dundrum">Dundrum</option>
</select>
<select id="category">
<option value="">Any Category</option>
<option value="Beauty">Beauty</option>
<option value="Shoes">Shoes</option>
<option value="Womens Wear">Womens Wear</option>
<option value="Cosmetics">Cosmetics</option>
</select>
<div classs="brand-holder">
<div class="brand-column">
<h3>0-9</h3>
<ul class="brand-list">
<li data-alpha="0" data-store="Arnotts,BrownThomas" data-arnotts-category="Mens Wear, Cosmetics" data-dundrum-category="" data-brownthomas-category="Beauty" >1. Brand Name <i>[data-store="Arnotts,BrownThomas" data-arnotts-category="Mens Wear, Cosmetics" data-dundrum-category="" data-brownthomas-category="Beauty"]</i></li>
</ul>
</div>
<div class="brand-column">
<h3>A</h3>
<ul class="brand-list">
<li data-alpha="A" data-store="Arnotts,Dundrum,BrownThomas" data-arnotts-category="Womens Wear, Cosmetics" data-dundrum-category="Womens Wear" data-brownthomas-category="Beauty" >A Brand Name <i>[data-store="Arnotts,Dundrum,BrownThomas" data-arnotts-category="Womens Wear, Cosmetics" data-dundrum-category="Womens Wear" data-brownthomas-category="Beauty"]</i></li>
</ul>
</div>
</div>
CSS
.hidden {
display:none;
}
jQuery
$(document).ready(function() {
function selectData() {
var alpha = $("#alpha").val();
var store = $("#store").val();
var category = $("#category").val();
var j;
// Hide items
$("li").each(function() {
a = $(this).attr('data-alpha');
s = $(this).attr('data-store').split(",");
if (store == "") {
c = ($(this).attr('data-arnotts-category') + "," + $(this).attr('data-dundrum-category') + "," + $(this).attr('data-brownThomas-category')).split(",");
} else {
c = $(this).attr('data-' + store.toLowerCase() + '-category');
}
// To be safe
for(j = 0; j < s.length; j++) s[j] = s[j].trim();
for(j = 0; j < c.length; j++) c[j] = c[j].trim();
if((alpha == "" || a == alpha) && (store == "" || s.indexOf(store) != -1) && (category == "" | c.indexOf(category) != -1)) {
$(this).removeClass('hidden');
} else {
$(this).addClass('hidden');
}
});
// Hide empty sections
$(".brand-list").each(function() {
if($(this).children("li").not(".hidden").length > 0) {
$(this).parent().removeClass("hidden");
} else {
$(this).parent().addClass("hidden");
}
});
}
$("#alpha").on("change", selectData);
$("#store").on("change", selectData);
$("#category").on("change", selectData);
});