JS解析JSON数据到TABLE表格

news/2024/11/14 9:24:33/文章来源:https://www.cnblogs.com/zhouein/p/18400197

效果图:

 

 

数据采用JSON,

[{"时间段": "上午","XX小学班课表": [{"名称": "教师上班","时间": "8:00","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "第一节课","时间": "8:30-9:10","星期一": "语文","星期二": "数学","星期三": "语文","星期四": "语文","星期五": "结合实践活动"},{"名称": "大课间活动","时间": "9:10-9:40","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "第二节课","时间": "9:45-10:25","星期一": "数学","星期二": "语文","星期三": "书法","星期四": "数学","星期五": "语文"},{"名称": "眼保健操","时间": "10:25-10:30","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "第三节课","时间": "10:40-11:20","星期一": "道德与法制","星期二": "科学","星期三": "科学","星期四": "道德与法制","星期五": "音乐"}]},{"时间段": "午休","杭州市东冠小学六(6)班课表": [{"名称": "午休","时间": "11:20-12:50","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""}]},{"时间段": "下午","杭州市东冠小学六(6)班课表": [{"名称": "第四节课","时间": "13:00-13:35","星期一": "英语","星期二": "体育与健康","星期三": "数学","星期四": "英语","星期五": "科学"},{"名称": "眼保健操","时间": "13:35-13:40","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "第五节","时间": "13:50-14:25","星期一": "体育与健康","星期二": "美术","星期三": "体育与健康","星期四": "劳动","星期五": "信息科学"},{"名称": "第六节","时间": "14:35-15:10","星期一": "拓展性课程","星期二": "美术","星期三": "英语","星期四": "音乐","星期五": "少先队活动"},{"名称": "课外活动","时间": "15:10-15:50","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "学生放学","时间": "16:00","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""},{"名称": "学后托管放学","时间": "17:40","星期一": "","星期二": "","星期三": "","星期四": "","星期五": ""}]}
]

  前端,  AJAX去获取JSON数据.

<!doctype html>
<html lang="cn">
<head><meta charset="utf-8"><meta http-equiv = "x-ua-compatible" content = "ie=edge"><meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8"><meta http-equiv = "Content-Language" content = "zh-CN"><title>班课表</title><meta name = "keywords" content = "班课表" /><meta name = "description" content = "班课表"><meta name = "viewport" content = "width=device-width, initial-scale=1"><link rel = "shortcut icon" href = "../favicon.ico"><script>var currentmenu = "Json.jsontable";</script><style>body {font-family:宋体;}@media (min-width: 1200px).aaaa {width: 98%;padding-top: 10px;}@media (min-width: 992px).aaaa {width: 970px;padding-top: 10px;}@media (min-width: 768px).aaaa {width: 750px;padding-top: 10px;}row{margin-right: 0px;margin-left: 0px;}.myTable{margin: auto;table-layout: fixed;font-size: 13px;border-collapse: collapse;border: 1px solid #000;}caption{padding: 10px;font-size: 30px;font-weight: bold;}	th,td{text-align: center;white-space: nowrap;  /* 防止单元格内容换行 */overflow: hidden;     /* 超出单元格范围的内容将被隐藏 */padding: 0 40px;border: 1px solid #000;/* border: 1px dashed #000; */height: 40px;}th{background: #2196f3;color: #FFF;position: -webkit-sticky;position: sticky;top: 0;               /* TH一直在顶端 */}table tbody td{}table tbody tr:nth-child(even) {background-color: #C9C9C9;}/* td:nth-child(-n+2) {text-align: center;}table tbody td:nth-child(3),td:nth-child(4) {text-align: left;}table tbody td:nth-child(5),table tbody td:nth-child(6) {text-align: right;} */table tbody tr:hover {background-color: #ffc107; /* 更改为你想要的颜色 */}</style>
</head>
</div><div id = "jsonTable"></div>
</div>
<script src = "https://www.saoban.cn/js/vendor/jquery-1.12.4.min.js"></script><script src = "https://www.saoban.cn/js/json/public.js"></script><script src = "https://www.saoban.cn/js/json/json5-to-table.js"></script><script>const { generateHTMLTable } = JSON5_TO_TABLE$(document).ready(function(){var xhr = new XMLHttpRequest();xhr.onreadystatechange = function(){if (this.readyState === 4 && this.status === 200){var dataJson = JSON.parse(this.responseText);var content = dataJson.data;if(content){// console.log(content);$(this).val(unData(JSON.stringify(content, null, 2)));var jsonHtmlTable = generateHTMLTable(content, null, {attributes: {table: {class: "myTable"},	}});$("#jsonTable").html( jsonHtmlTable );}}};xhr.open("GET", "?id=json", true);xhr.send();	});</script>	
</body>

  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/793069.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

登陆微软账号,输入window密码后,提示「哎呀,出错了」

存在问题解决方案暂时关闭dai~Li 设置 账户 下滑,电子邮件和账户 添加Microsoft账户解决后的效果版权木有,侵权不究,欢迎转载

TDBC回顾 | 钛铂数据肖贝贝:TapData — 自主可控

7月,TapData CTO 肖贝贝出席“2024可信数据库发展大会”,分享了“TapData — 自主可控的实时数据平台”的主题演讲。本文为完整内容。2024年7月17日上午,“2024可信数据库发展大会”数据库生态与国际化分论坛在北京隆重召开。会上,深圳钛铂数据有限公司CTO肖贝贝重点与听众…

服务器时间同步

一、安装ntpddate #centos,redhat系列 yum install ntpdate#debian,ubuntu系列 apt install ntpdate二、在服务器都能联网情况下时间同步 #手动同步阿里云时间服务器时间 ntpdate ntp.aliyun.com 阿里云时间同步地址 ntp1.aliyun.com ntp2.aliyun.com ntp3.aliyun.com ntp4.ali…

D35XT120-ASEMI新能源专用D35XT120

D35XT120-ASEMI新能源专用D35XT120编辑:ll D35XT120-ASEMI新能源专用D35XT120 型号:D35XT120 品牌:ASEMI 封装:DXT-5 批号:2024+ 现货:50000+ 正向电流(Id):35A 反向耐压(VRRM):1200V 正向浪涌电流:450A 正向电压(VF):1.10V 引脚数量:5 芯片个数:5 芯片尺寸:…

高等数学 1.1 映射与函数

目录一、映射映射概念逆映射与复合映射二、函数函数概念函数的几种特性(1)函数的有界性(2)函数的单调性(3) 函数的奇偶性(4)函数的周期性反函数与复合函数反函数复合函数函数的运算初等函数基本初等函数初等函数双曲函数与反双曲函数 一、映射 映射概念定义 设 \(X\) ,…

rfc编写

rfc检索:https://www.rfc-editor.org/rfc-index.html rfc绘图:https://asciiflow.com/ 绘图后保存成ascii art:

前端 - Failed to load module script 解决方案

Failed to load module script 解决方案 问题描述 打包好项目发布上传到 nginx 后,浏览器访问,出现一下报错信息:Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME ty…

有关API接口的介绍及简单的API接口代码示例

https://img2024.cnblogs.com/blog/3506472/202409/3506472-20240906135806395-1508567224.png在当今数字化时代,应用程序不再孤立存在。它们通过API(应用程序编程接口)相互连接,共享数据和功能。API是现代软件开发中不可或缺的一部分,它允许不同的软件系统相互通信,实现…

ext4.vhdx占用C盘过大

ext4.vhdx 是个虚拟磁盘,默认位置为 C:\Users{用户名}\AppData\Local\Docker\wsl\data\ext4.vhdx,会占用很大磁盘空间,可采用下列办法将其改到其它磁盘或分区,如D分区。 1、查看有哪些2、导出目标: wsl --export docker-desktop "D:\\docker-desktop.tar" wsl -…

CRM软件的演进:从传统到连接型CRM

1、CRM定义与分类 1.1CRM的定义 CRM,英文Customer Relationship Management的缩写,中文全称为客户关系管理。通常情况下,人们通常用CRM直接表达客户关系管理软件系统——一个以客户为中心的专门用于管理与客户关系的软件工具,以确保与客户在营销、销售、服务的每一环节上都能…

项目依赖和配置

mysql1、添加相关依赖 <!-- 增加相关依赖包 --><!-- Postgresql驱动包 --><!-- <dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope></dependency> --&…