
本文旨在解决在JavaScript中,如何根据列表中对象的特定ID属性进行分组,并在每个分组的开头添加一个“全选”复选框的需求。我们将通过示例代码,详细讲解如何使用reduce方法实现数据分组,以及如何动态生成HTML代码以展示分组后的数据,并实现“全选”功能。
假设我们有一个包含学生信息的列表 res.List,每个学生对象都有一个 Student.Id 属性。我们的目标是根据 Student.Id 将学生信息分组,并将相同 Id 的学生放在一组。
可以使用 reduce 方法来实现这一目标。reduce 方法对数组中的每个元素执行一个reducer函数(由你提供),并将结果汇总为单个返回值。
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;
},{});
console.log(result);这段代码的解释如下:
立即学习“Java免费学习笔记(深入)”;
最终,result 对象将包含以 Student.Id 为键,以学生姓名数组为值的键值对。
接下来,我们需要根据分组后的数据动态生成 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免费学习笔记(深入)”;
对应的HTML结构如下:
<div id="container"></div>
最后,我们需要实现“全选”功能,即当点击“全选”复选框时,选中或取消选中该分组中的所有复选框。
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>本文详细介绍了如何使用 JavaScript 根据 ID 分组列表数据,并添加“全选”功能。通过使用 reduce 方法进行数据分组,动态生成 HTML 代码,以及添加事件监听器实现“全选”功能,我们可以高效地处理和展示分组数据。
注意事项:
以上就是JavaScript教程:根据ID分组列表数据并添加“全选”功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号