
在前端开发中,我们经常需要处理从后端获取的列表数据。假设我们有一个包含学生信息的数组,每个学生记录都包含学校、家长和学生本人的详细信息,其中student.id用于标识学生的唯一id。
原始数据示例:
const res = {
List: [
{ "School information": { RegId: 1, Name: "SJ" }, ParentInfo: { Id: 0, Name: "Abc" }, Student: { Id: 1, Name: "Student1" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 5, Name: "Student6" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 1, Name: "Student3" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 5, Name: "Student5" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 1, Name: "Student4" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 7, Name: "Student9" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 7, Name: "Student11" } }
]
};目标需求:
我们需要将这些学生记录按照Student.Id进行分组,并在每个分组的顶部添加一个“Select All Students”的复选框。点击该复选框时,同组内的所有学生复选框应同步选中或取消选中。
期望的HTML输出结构:
立即学习“Java免费学习笔记(深入)”;
<!-- 针对Student.Id为1的分组 --> <div> <label>Select All Studentds <input type="checkbox" class="group"></label><br> <label><input type="checkbox">Student1</label><br> <label><input type="checkbox">Student3</label><br> <label><input type="checkbox">Student4</label> </div> <!-- 针对Student.Id为5的分组 --> <div> <label>Select All Studentds <input type="checkbox" class="group"></label><br> <label><input type="checkbox">Student6</label><br> <label><input type="checkbox">Student5</label> </div> <!-- 针对Student.Id为7的分组 --> <div> <label>Select All Studentds <input type="checkbox" class="group"></label><br> <label><input type="checkbox">Student9</label><br> <label><input type="checkbox">Student11</label> </div>
实现此需求的第一步是将原始的扁平化列表数据根据Student.Id进行分组。Array.prototype.reduce方法是实现这一目标的强大工具。
const result = res.List.reduce((accumulator, currentItem) => {
// 使用学生ID作为键,将学生姓名添加到对应的数组中
// 如果该ID的数组不存在,则使用空数组进行初始化 (??= 是ES2021的空值合并赋值运算符)
(accumulator[currentItem.Student.Id] ??= []).push(currentItem.Student.Name);
return accumulator;
}, {}); // 初始累加器为一个空对象代码解析:
经过此步骤,result对象将是以下结构:
{
"1": ["Student1", "Student3", "Student4"],
"5": ["Student6", "Student5"],
"7": ["Student9", "Student11"]
}有了分组后的数据,接下来就是将其转换为HTML结构并呈现在页面上。我们可以使用Object.values获取分组后的数组,然后再次利用Array.prototype.map来生成每个分组的HTML。
document.getElementById("container").innerHTML =
Object.values(result) // 获取所有学生ID对应的学生姓名数组
.map(group =>
// 为每个分组创建一个div
'<div>' +
// 添加“Select All Students”复选框
'<label>Select All Studentds <input type="checkbox" class="group"></label><br>' +
// 遍历组内学生,为每个学生创建复选框
group.map(studentName => `<label><input type="checkbox">${studentName}</label>`).join("<br>") +
'</div>'
)
.join(""); // 将所有分组的HTML字符串连接起来代码解析:
最后一步是为“Select All Students”复选框添加交互功能。当用户点击这些复选框时,需要同步更新同组内所有学生复选框的状态。
document.querySelectorAll(".group").forEach(checkbox =>
checkbox.addEventListener("click", () => {
// 获取当前“Select All Students”复选框所在的父div
const parentDiv = checkbox.closest("div");
// 找到该div内所有的复选框(包括“Select All Students”本身和学生复选框)
parentDiv.querySelectorAll("[type=checkbox]").forEach(childCheckbox => {
// 将所有子复选框的状态设置为与“Select All Students”复选框相同
childCheckbox.checked = checkbox.checked;
});
})
);代码解析:
为了使上述代码能够运行,我们需要一个HTML容器。
HTML (index.html):
<!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; }
div { border: 1px solid #ccc; padding: 10px; margin-bottom: 15px; border-radius: 5px; }
label { display: block; margin-bottom: 5px; }
</style>
</head>
<body>
<h1>学生列表分组与全选示例</h1>
<div id="container">
<!-- 动态生成的内容将在此处显示 -->
</div>
<script src="app.js"></script>
</body>
</html>JavaScript (app.js):
const res = {
List: [
{ "School information": { RegId: 1, Name: "SJ" }, ParentInfo: { Id: 0, Name: "Abc" }, Student: { Id: 1, Name: "Student1" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 5, Name: "Student6" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 1, Name: "Student3" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 5, Name: "Student5" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 1, Name: "Student4" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 7, Name: "Student9" } },
{ "School information": { RegId: 1, Name: "" }, ParentInfo: { Id: 0, Name: "" }, Student: { Id: 7, Name: "Student11" } }
]
};
// 1. 数据分组
const groupedStudents = res.List.reduce((accumulator, currentItem) => {
(accumulator[currentItem.Student.Id] ??= []).push(currentItem.Student.Name);
return accumulator;
}, {});
// 2. 动态生成HTML
document.getElementById("container").innerHTML =
Object.values(groupedStudents)
.map(group =>
'<div>' +
'<label>Select All Studentds <input type="checkbox" class="group"></label><br>' +
group.map(studentName => `<label><input type="checkbox">${studentName}</label>`).join("<br>") +
'</div>'
)
.join("");
// 3. 实现全选交互逻辑
// 注意:事件监听器必须在HTML元素被添加到DOM后才能绑定
document.querySelectorAll(".group").forEach(checkbox =>
checkbox.addEventListener("click", () => {
const parentDiv = checkbox.closest("div");
parentDiv.querySelectorAll("[type=checkbox]").forEach(childCheckbox => {
childCheckbox.checked = checkbox.checked;
});
})
);本教程演示了如何利用JavaScript的强大数组和对象方法,结合DOM操作,实现复杂的数据分组、动态UI渲染和交互功能。
关键技术点:
注意事项与扩展:
通过掌握这些技术,开发者可以高效地处理和展示动态数据,构建响应式且用户友好的Web界面。
以上就是JavaScript中按ID分组数据并动态生成带有全选功能的学生列表的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号