
在Web应用中,尤其是在电子商务或数据展示平台,动态筛选是提升用户体验的关键功能。用户通过选择不同的筛选条件(如颜色、尺寸、品牌等),实时查看符合要求的产品或数据。然而,当涉及到多个筛选维度时,如何正确处理它们之间的逻辑关系(“AND”或“OR”)是一个常见的挑战。例如,用户可能希望看到“红色”或“蓝色”的产品(OR关系),但当同时选择“红色”和“小尺寸”时,则可能希望看到既是“红色”又是“小尺寸”的产品(AND关系)。
本教程将聚焦于解决这一问题,通过一个产品筛选的实例,演示如何使用JavaScript实现灵活的多维度筛选逻辑。
为了实现多维度筛选,我们需要清晰地定义筛选器和可筛选的产品项。每个筛选器组(如颜色、尺寸)应有独立的标识,同时每个产品项需要包含其对应的属性信息。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多维度产品筛选</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.filter-group { margin-bottom: 15px; border: 1px solid #eee; padding: 10px; border-radius: 5px; }
.filter-group h3 { margin-top: 0; color: #333; }
label { display: block; margin-bottom: 5px; }
.filterable {
border: 1px solid #ddd;
padding: 10px;
margin-bottom: 10px;
background-color: #f9f9f9;
border-radius: 4px;
}
.filterable h1 { margin-top: 0; font-size: 1.5em; }
</style>
</head>
<body>
<div class="filter-group">
<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 class="filter-group">
<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>
<h1>筛选结果</h1>
<div class="filterable" data-attributes="blue large">产品 A</div>
<div class="filterable" data-attributes="green small">产品 B</div>
<div class="filterable" data-attributes="red medium">产品 C</div>
<div class="filterable" data-attributes="red large">产品 D</div>
<script src="script.js"></script>
</body>
</html>关键点:
立即学习“Java免费学习笔记(深入)”;
我们将编写 updateFilter 函数来处理筛选逻辑。这个函数会在任何筛选复选框状态改变时被调用。
// script.js
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-attributes 中解析产品的颜色和尺寸
// 假设 data-attributes 格式为 "颜色 尺寸"
const attributes = filterable.dataset.attributes.split(' ');
const productColor = attributes[0];
const productSize = attributes[1];
let shouldDisplay = false; // 默认不显示
// 3.1. 如果同时选择了颜色和尺寸筛选器 (AND 逻辑)
if (colorChecked.length >= 1 && sizeChecked.length >= 1) {
// 产品必须同时满足选中的颜色和选中的尺寸
shouldDisplay = colorChecked.includes(productColor) && sizeChecked.includes(productSize);
}
// 3.2. 如果只选择了颜色筛选器 或 只选择了尺寸筛选器 (OR 逻辑)
else {
// 产品满足任意一个选中的颜色 或 任意一个选中的尺寸即可
shouldDisplay = colorChecked.includes(productColor) || sizeChecked.includes(productSize);
}
// 根据 shouldDisplay 决定产品是否可见
filterable.style.display = shouldDisplay ? 'block' : 'none';
});
}
// 4. 为所有筛选复选框添加 change 事件监听器
filterCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', updateFilter);
});
// 5. 页面加载时执行一次筛选,以反映初始状态
updateFilter();代码解析:
本教程提供了一个实用的 JavaScript 解决方案,用于实现多维度产品筛选功能,并灵活处理不同筛选维度之间的“AND”和“OR”逻辑。通过清晰的 HTML 结构、分离的筛选器管理以及核心的条件判断逻辑,我们可以构建出响应迅速且用户友好的筛选界面。在实际项目中,应根据具体需求和数据规模,进一步考虑代码的健壮性、可扩展性和性能优化。
以上就是基于JavaScript实现多维度产品筛选:AND/OR逻辑的应用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号