当前位置:网站首页>js动态生成table表格,加滚动条
js动态生成table表格,加滚动条
2022-04-22 03:42:00 【追风少年?】
效果 :
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
/* 表格容器样式,用flex布局可保证table内容能铺满剩余空间 */
.tbl-container {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
}
/* 设置表格的布局方式,用于宽度对齐 */
.tbl-header table{
border-bottom: none;
border-bottom-style: none;
table-layout: fixed;
}
.tbl-body table {
table-layout: fixed;
}
/* 设置表格内容容器,用于铺满整个剩余空间 */
.tbl-header {
padding-right: 17px;
}
.tbl-body {
height: 160px;
overflow-y: scroll;
}
table {
border-collapse: collapse;
}
table th{
width: 80px;
height: 25px;
text-align: center;
background-color: cornsilk;
}
table td{
width: 80px;
height: 25px;
text-align: center;
}
</style>
</head>
<body>
<!-- 表格容器,可用于设置整个的边框及高度 -->
<div class="tbl-container">
<!-- 表头容器,必须留出17px的滚动条位置 -->
<div class="tbl-header">
<table>
<thead>
<tr>
<th >排名</th>
<th >业务员</th>
<th >金额</th>
<th >订单</th>
<th >客户</th>
</tr>
</thead>
</table>
</div>
<!-- 表格内容容器-->
<div class="tbl-body">
<table >
<tbody>
</tbody>
</table>
</div>
</div>
<script>
// 1.先去准备好学生的数据
var datas = [{
name1: '小明',
subject1: '1',
name: '小明',
subject: '1',
score: 100
}, {
name1: '小新',
subject1: '1',
name: '小明',
subject: '1',
score: 98
}, {
name1: '小夏',
subject1: '1',
name: '小明',
subject: '1',
score: 99
}, {
name1: '小玉',
subject1: '1',
name: '小明',
subject: '1',
score: 88
}, {
name1: '小猪',
subject1: '1',
name: '小明',
subject: '1',
score: 0
},{
name1: '小明',
subject1: '1',
name: '小明',
subject: '1',
score: 100
}, {
name1: '小新',
subject1: '1',
name: '小明',
subject: '1',
score: 98
}, {
name1: '小夏',
subject1: '1',
name: '小明',
subject: '1',
score: 99
}, {
name1: '小玉',
subject1: '1',
name: '小明',
subject: '1',
score: 88
}, {
name1: '小猪',
subject1: '1',
name: '小明',
subject: '1',
score: 0
}];
// 2. 往tbody 里面创建行: 有几个人(通过数组的长度)我们就创建几行
var tbody = document.querySelector('tbody');
for (var i = 0; i < datas.length; i++) { // 外面的for循环管行 tr
// 1. 创建 tr行
var tr = document.createElement('tr');
tbody.appendChild(tr);
// 2. 行里面创建单元格(跟数据有关系的3个单元格) td 单元格的数量取决于每个对象里面的属性个数 for循环遍历对象 datas[i]
for (var k in datas[i]) { // 里面的for循环管列 td
// 创建单元格
var td = document.createElement('td');
// 把对象里面的属性值 datas[i][k] 给 td
// console.log(datas[i][k]);
td.innerHTML = datas[i][k];
tr.appendChild(td);
}
}
</script>
</body>
</html>
版权声明
本文为[追风少年?]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Ghjkku/article/details/124330264
边栏推荐
- Vos3000 8.05 installation and source code
- MongoDB——聚合管道之$match操作
- 机器学习理论之(6):由逻辑回归(对数几率)方法到 SVM;为什么 SVM 是最大间隔分类器
- Collection thread safety
- Go gin framework configuration log output to file
- Copy the content of Baidu Library by releasing the monitoring through the browser console
- Xampp configuration Xdebug extension, php7 Version 2.2
- 2021-10-21 software testing theory
- Full summary of 18 tax categories of tax law with memory tips
- Lesson 121 of serial 4 of Rasa dialogue robot: the whole process demonstration of e-commerce retail dialogue robot operation process debugging of Rasa dialogue robot debugging project - 4
猜你喜欢

Deep learning and image recognition: principle and practice notes day_ seventeen

Zabbix5 series - monitor Huawei and H3C switches (snmpv2c / SNMPv3 / snmptrap) (II)

linux安装MySQL

AI video cloud vs narrowband HD, who is the darling of the video era

Common convolutional neural network structures

The third year after the epidemic: technical management and teamwork under the changing times!

Error report: modulenotfounderror: no module named 'Astra‘

STM32 notes

The basic software products of xinghuan science and technology have been fully implemented and blossomed, bringing "Star" momentum to the digital transformation of enterprises

Mysql8 hard disk version installation configuration
随机推荐
Deep learning and image recognition: principle and practice notes day_ twelve
What is the future direction of GPU?
[cloud computing] three virtual machines complete spark yard cluster deployment and write Scala applications to realize word count statistics
Some errors in TP5
Deep learning and image recognition: principle and practice notes day_ sixteen
Zabbix5 series - making topology map (XIII)
Nacos 为什么这么强
JS 数字千位分隔符且保留两位小数
使用supervisor管理laravel队列 - 配置文件supervisord.conf
Common convolutional neural network structures
Record the solution to the failure of configuring MySQL remote connection for ECS once
Docker starts the general solution of three warnings of redis official image
Single example of multithreading
export ‘createStore‘ (imported as ‘createStore‘) was not found in ‘./store/index.js‘ (possible expor
Full summary of 18 tax categories of tax law with memory tips
Xampp configuration Xdebug extension, php7 Version 2.2
Assembly language programming MOOC University of China Zhengzhou University online course test questions and answers
Vos3000 8.05 installation and source code
College English vocabulary analysis Chinese University MOOC Huazhong University of science and technology
Redis persistence