当前位置:网站首页>原生js 实现table表格
原生js 实现table表格
2022-08-06 08:18:00 【逆风优雅】
首先: 定义一个表头(改表头的作用是,固定表头的展示,不至于tabody数据过多时,页面滚动导致表格头部滚动)
<table id="dev-title" class="table table-hover" style="margin-bottom: 0; table-layout:fixed;">
<colgroup>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:13%"/>
</colgroup>
<thead>
<tr class="title">
<th>所在小区</th>
<th>台变</th>
<th>异动类型</th>
<th>表箱名</th>
<th>时间</th>
</tr>
</thead>
</table>其次定义表格的body部分:
<div class="table-scrollable table-scrollable-my ">
<table class="table table-hover dev-tab" style="table-layout:fixed;">
<colgroup>
<col style="width:10%;"/>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:10%"/>
<col style="width:13%"/>
</colgroup>
<tbody>
</tbody>
</table>
</div>
</div>需要注意的是,两个colgroup的col定义的宽度要一致,这样才可以实现表格的宽度自适应。
其次,给表格body赋值:
function drawFormList_bd(data){ // 定义一个渲染表格数据的方法
console.log(data,'6666666666')
var $listItem = $('<tr>\
<td class="xiaoQu"></td>\
<td class="taiBian"></td>\
<td class="type"></td>\
<td class="meterBoxName"></td>\
<td class="repairTime"></td>\
</tr>');
var $from = $(".dev-tab tbody").empty(); // 每次进入该方法,先让表格的tbody为空
if(data.records && data.records.length>0){
$(data.records).each(function(i){ // 循环给表格赋值
var self = this;
var $tmp = $listItem.clone();
$tmp.find('.taiBian').html(this.substationName);
$tmp.find('.xiaoQu').html(this.courtName)
$tmp.find('.meterBoxName').html(this.meterBoxName);
if(this.diffType == '2'){
$tmp.find('.type').html("增加");
}else{
$tmp.find('.type').html("减少");
}
$tmp.find('.repairTime').html(this.createTime ? moment(this.createTime).format("YYYY-MM-DD HH:mm:ss") : "-");
$from.append($tmp); // 将定义的html结构添加到刚刚表格的tbody下面
});
}else{
$from.append('<tr><td class="text-center danger" colspan="10">抱歉,没有找到数据</td></tr>');
}
}
边栏推荐
- 使用R从Excel中删除带有空单元格的行
- Parameter ‘courseId’ not found. Available parameters are [arg1, arg0, param1, para
- R语言统计与绘图:生存曲线的两两比较
- 从幻核疑似裁撤看如何保证NFT的安全
- 代码签名证书多少钱?
- yum offline installation
- 2022-08-05:以下go语言代码输出什么?A:65, string;B:A, string;C:65, int;D:报错。
- Advanced Programming in UNIX Environment - Chapter 2
- 快速学会文件操作模块
- Day020 方法重写与多态
猜你喜欢
随机推荐
使用R从Excel中删除带有空单元格的行
Datax3.0+DataX-Web builds distributed visual ETL system
Hands-on Deep Learning_Batch Normalization
No, no, no, it's 2022, you don't know the principle of Jmeter, right?
Typescript variable
The origin of the name, concave language -, and moral
reportROC 一行代码输出ROC曲线的各项统计数值及ROC曲线
UNIX环境高级编程-第一章
用tidyverse包做探索性数据分析,常用函数总结
【matlab的积分,傅里叶,拉普拉斯变换,信号分析】
动手学深度学习_Batch Normalization
LeetCode——345. 反转字符串中的元音字母
How to ensure the security of NFT from the suspected abolition of the magic core
finalfit包可视化模型结果
OpenAtom XuperChain 开源双周报 |2022.7.25-2022.8.5
超好用R包(grafify)
autoReg | 自动线性和逻辑回归和生存分析
How to improve the quality of articles without being "recommended and affected" by the post assistant
深度学习——以花朵识别为例,分析构造神经网络时用到的各个类构造函数(Dense、Conv2D、Flatten等)
Day020 Method Overriding and Polymorphism








