
在构建现代Web应用时,产品或内容筛选功能是常见的需求。简单的筛选通常通过“或”逻辑实现,即只要满足任一选中条件即可显示。然而,在面对多维度筛选(例如,既要红色又要小尺寸的产品)时,我们需要更复杂的“与”(AND)逻辑。本文将指导您如何使用JavaScript实现这种多维度的、可切换“与/或”逻辑的筛选功能。
为了实现多维筛选,首先需要清晰地定义HTML结构,包括过滤器控件和可筛选的产品元素。关键在于为不同类型的过滤器分配独立的类名,并为产品元素使用自定义数据属性来存储其特性。
<div> <h3>颜色</h3> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="red">红色</label> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="green">绿色</label> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="blue">蓝色</label> </div> <div> <h3>尺寸</h3> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="small">小</label> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="medium">中</label> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="large">大</label> </div> <div> <h1>筛选结果</h1> <!-- filterable 元素使用 data-colors 属性存储其颜色和尺寸信息 --> <div class="filterable" data-colors="blue large">产品 A</div> <div class="filterable" data-colors="green small">产品 B</div> <div class="filterable" data-colors="red medium">产品 C</div> <div class="filterable" data-colors="red large">产品 D</div> </div>
关键点:
核心的过滤逻辑在于 updateFilter 函数,它负责根据用户的选择动态更新产品显示。
立即学习“Java免费学习笔记(深入)”;
const filterCheckboxes = document.querySelectorAll('.filter-checkbox');
const colorCheckboxes = document.querySelectorAll('.color-checkbox');
const sizeCheckboxes = document.querySelectorAll('.size-checkbox');
const filterables = document.querySelectorAll('.filterable');
function updateFilter() {
// 1. 获取当前选中的颜色和尺寸过滤器值
const colorChecked = Array.from(colorCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
const sizeChecked = Array.from(sizeCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
// 2. 处理无任何过滤器被选中的情况:显示所有产品
if (!(colorChecked.length || sizeChecked.length)) {
filterables.forEach(filterable => {
filterable.style.display = 'block';
});
return; // 结束函数执行
}
// 3. 遍历所有可筛选产品,应用过滤逻辑
filterables.forEach(filterable => {
// 解析产品的颜色和尺寸属性
// 假定 data-colors 格式为 "颜色 尺寸"
const productAttributes = filterable.dataset.colors.split(' ');
const productColor = productAttributes[0];
const productSize = productAttributes[1];
let shouldDisplay = false;
// 4. 判断过滤逻辑:
// 如果颜色和尺寸两组过滤器都有选中项,则应用“与”逻辑
if (colorChecked.length >= 1 && sizeChecked.length >= 1) {
shouldDisplay = colorChecked.includes(productColor) && sizeChecked.includes(productSize);
}
// 否则(只有颜色或只有尺寸过滤器有选中项,或两者都没有,但这种情况已在步骤2处理),
// 则应用“或”逻辑(即只要产品符合任一已选的颜色或尺寸即可)
else {
shouldDisplay = colorChecked.includes(productColor) || sizeChecked.includes(productSize);
}
// 5. 根据判断结果显示或隐藏产品
if (shouldDisplay) {
filterable.style.display = 'block';
} else {
filterable.style.display = 'none';
}
});
}
// 6. 为所有过滤器添加事件监听器
filterCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', updateFilter);
});
// 7. 页面加载时执行一次初始过滤
updateFilter();代码解析:
通过上述方法,您可以构建一个功能强大且灵活的JavaScript多维过滤系统,极大地提升用户在复杂数据集中的导航和筛选体验。
以上就是JavaScript 多维过滤:实现复杂条件下的产品筛选的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号