
本文旨在提供一种使用 JavaScript 对包含学生信息的列表进行分组,并为每个分组添加“全选”复选框功能的解决方案。通过使用 reduce 方法按 Student.Id 对数据进行分组,然后动态生成 HTML 结构,实现数据的可视化和交互。本文将提供详细的代码示例和解释,帮助开发者理解和应用该技术。
首先,我们需要一个包含学生信息的列表。假设我们有以下数据结构:
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"}}]};接下来,我们使用 reduce 方法根据 Student.Id 对数据进行分组。reduce 方法接受一个回调函数和一个初始值作为参数。回调函数接收累加器 a、当前值 c 和索引 i 作为参数。
const result = res.List.reduce((a,c,i)=>{
(a[c.Student.Id]??=[]).push(c.Student.Name);
return a;
},{});这段代码的含义是:
立即学习“Java免费学习笔记(深入)”;
经过这段代码的处理,result 对象将包含按 Student.Id 分组的学生姓名列表。例如:
{
"1": ["Student1", "Student3", "Student4"],
"5": ["Student6", "Student5"],
"7": ["Student9", "Student11"]
}接下来,我们需要将分组后的数据动态生成 HTML 结构,包括“全选”复选框和每个学生的复选框。
document.getElementById("container").innerHTML=
Object.values(result).map(grp=>
'<div><label>Select All Studentds <input type="checkbox" class="group"></label><br>'
+grp.map(s=>`<label><input type="checkbox">${s}</label>`).join("<br>")+'</div>').join("");这段代码的含义是:
立即学习“Java免费学习笔记(深入)”;
最后,我们需要实现“全选”功能,即点击“全选”复选框时,选中或取消选中该组的所有学生复选框。
document.querySelectorAll(".group").forEach(cb=>
cb.addEventListener("click",()=>cb.closest("div").querySelectorAll(" [type=checkbox]").forEach(c=>c.checked=cb.checked)))这段代码的含义是:
立即学习“Java免费学习笔记(深入)”;
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Grouping and Select All</title>
</head>
<body>
<div id="container"></div>
<script>
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"}}]};
const result = res.List.reduce((a,c,i)=>{
(a[c.Student.Id]??=[]).push(c.Student.Name);
return a;
},{});
document.getElementById("container").innerHTML=
Object.values(result).map(grp=>
'<div><label>Select All Studentds <input type="checkbox" class="group"></label><br>'
+grp.map(s=>`<label><input type="checkbox">${s}</label>`).join("<br>")+'</div>').join("");
document.querySelectorAll(".group").forEach(cb=>
cb.addEventListener("click",()=>cb.closest("div").querySelectorAll(" [type=checkbox]").forEach(c=>c.checked=cb.checked)))
</script>
</body>
</html>这段代码将生成一个包含分组学生列表和“全选”功能的 HTML 页面。
本文介绍了一种使用 JavaScript 对包含学生信息的列表进行分组,并为每个分组添加“全选”复选框功能的解决方案。通过使用 reduce 方法按 Student.Id 对数据进行分组,然后动态生成 HTML 结构,实现数据的可视化和交互。该技术可以应用于各种需要对数据进行分组和批量操作的场景。
以上就是JavaScript:按ID分组列表并添加“全选”功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号