吐槽

今天,在vue中遇到 复杂表格的渲染 ,需要合并表头th的单元格,且合并单元格的那列的表头数据是动态数据,也就是不知道会有多少个表头列,而这几个表头列还分了好几个子表头

这个需求在js里用Juicer模板很好做的,思路我是有的,但就是对于vue,我也算初学者,很多概念不是很懂,这就限制了思路。

在网上搜了很多合并单元格的都是简单的数据合并,也就是td合并, 不是我们的需求,就不贴了。

哎,废话不多说了,看代码吧:

代码示例

使用iviewuitable组件:

最初,直接使用项目中的iviewuitable组件, 给 column 设置 children ,可以实现表头合并。先用写死的数据做了个样例,如下:


<Table :columns="columns" :data="studentData" border></Table>

data()中如下:



      columns: [
{
title: '序号',
width: 60,
align: 'center',
fixed: 'left',
render: (h, params) => {
return h('span', params.row._index + 1)
}
},
{
title: '姓名',
key: 'name',
align: 'center',
fixed: 'left',
width: 80
},
{
title: '学号',
key: 'code',
align: 'center',
width: 80
},
{
title: '性别',
key: 'sex',
align: 'center',
width: 80
},
{
title: '学期',
key: 'term',
align: 'center',
width: 80
},
{
title: '9月28日',
align: 'center',
children: [
{
title: '阅读',
key: 'date1_rScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '听力',
key: 'date1_lScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '写作',
key: 'date1_wScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '口语',
key: 'date1_sScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '总分',
key: 'date1_score',
align: 'center',
minWidth: 80,
sortable: true
}
]
},
{
title: '8月10日&14日',
align: 'center',
children: [
{
title: '阅读',
key: 'date2_rScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '听力',
key: 'date2_lScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '写作',
key: 'date2_wScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '口语',
key: 'date2_sScore',
align: 'center',
minWidth: 80,
sortable: true
},
{
title: '总分',
key: 'date2_score',
align: 'center',
minWidth: 80,
sortable: true
}
]
},
{
title: '听力提高',
key: 'lImprove',
align: 'center',
width: 70
},
{
title: '阅读提高',
key: 'rImprove',
align: 'center',
width: 70
},
{
title: '写作提高',
key: 'writingImprove',
align: 'center',
width: 70
},
{
title: '口语提高',
key: 'sImprovem',
align: 'center',
width: 70
},
{
title: '总分提高',
key: 'srImprove',
align: 'center',
width: 70
}
],
studentData: [
{
name: 'xxx',
code: '918989070065',
sex: '男',
term: '2018秋',
date1: '9月28日',
date1_rScore: '3.5',
date1_lScore: '3.5',
date1_wScore: '5',
date1_sScore: '4',
date1_score: '4',
date2: '8月10日&14日',
date2_rScore: '3.5',
date2_lScore: '3.5',
date2_wScore: '5',
date2_sScore: '4',
date2_score: '4',
lImprove: '-0.5',
rImprove: '0',
wImprove: '1.5',
sImprove: '0.5',
srImprove: '0.5'
}
],

实现效果如图:

重点是后端给的数据格式

以下是data数据是后端接口返回的,其中的数据格式是这样的:


[
{
"studentId": "ff808b937f50a33",
"studentName": "傅xx",
"studentCode": "91scdsc109",
"sex": {
"value": "MALE",
"name": "男"
},
"termName": "2018秋",
"examDates": [
"10月",
"9月28日"
],
"map": {
"9月28日": [
{
"courseName": "听力",
"score": 6.0
},
{
"courseName": "阅读",
"score": 7.0
},
{
"courseName": "写作",
"score": 5.5
}
]
},
"courseNames": [
"听力",
"阅读",
"写作",
"口语",
"总分"
]
},
{
"studentId": "ff80c52801bc",
"studentName": "陈xx",
"studentCode": "91edfedf3",
"sex": {
"value": "FEMALE",
"name": "女"
},
"termName": "2018秋",
"examDates": [
"10月",
"9月28日"
],
"map": {
"9月28日": [
{
"courseName": "听力",
"score": 5.5
},
{
"courseName": "阅读",
"score": 6.0
},
{
"courseName": "写作",
"score": 5.5
},
{
"courseName": "口语",
"score": 5.5
}
]
},
"courseNames": [
"听力",
"阅读",
"写作",
"口语",
"总分"
]
}
]

重点是要以上述datamap里的日期为一组的表头,每组日期包含的是五门课,然后日期是根据数据库查出来的,不确定到底是几个日期,也就是table里这个日期的th是根据数据循环生成的,请仔细看这里给出的数据格式。

使用H5的table实现

template如下:



<table class="table">
<thead>
<tr>
<th rowspan="2">序号</th>
<th rowspan="2">姓名</th>
<th rowspan="2">学号</th>
<th rowspan="2">性别</th>
<th rowspan="2">学期</th>
<th colspan="5" v-for="(it,i) in examDates" :key="i">{{it}}</th>
</tr>
<tr>
<template v-for="itd in examDates">
<th v-for="(itc,j) in courseNames" :key="itd+j">{{itc}}</th>
</template>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in studentDataList" :key="index">
<td>{{index+1}}</td>
<td>{{item.studentName}}</td>
<td>{{item.studentCode}}</td>
<td>{{item.sex.name}}</td>
<td>{{item.termName}}</td>
<template v-for="examDate in examDates">
<template v-for="(course,j) in courseNames">
<td :key="examDate+j">
{{initScoreFinal(examDate,course,item.map)}}
</td>
</template>
</template>
</tr>
</tbody>
</table>

获取到上述后端返回的数据后,对数据稍微处理下:


data () {
return {
studentDataList: [],
examDates: [],
courseNames: []
},
created () {
this.getData ()
},
methods: {
//
getData () {
this.$get( //该方法是封装过的axios
'/list.json',
{
....//此处是参数,略
},
response => {
this.examDates = response.data[0].examDates
this.courseNames = response.data[0].courseNames
this.studentDataList = response.data
}
)
},
initScoreFinal (examDate, course, map) {
let final = 0
console.log('map:' + map)
for (var it in map) {
map[it].forEach((item, index, array) => {
if (it === examDate && item.courseName === course) {
final = item.score
}
})
}
return final
}
}

效果如图:

再吐个槽

在网上搜了很多合并单元格的都是简单的数据合并,也就是td合并,
我们这边的项目需要的这个表格比较变态,结合上述效果图来说吧,图中的表头是先按日期为一列th,这日期的列下分五门课程的子列th,且日期数目不定,可能是一两个日期,可能是多个日期,每个日期下对应的课程也不确定,就像学生上课,每天的课不同,但总共就那五门课,日期列的数目不定,课程数的数据不定,于是,这就很头疼了-_-||

总之长知识了,记录下来。

或许有大神能用iviewuitable组件 能做出来动态列数的表头合并,欢迎来一起谈论办法!!!

来源:https://segmentfault.com/a/1190000016895856

vue中 表头 th 合并单元格,且表格列数不定的动态渲染方法的更多相关文章

  1. vue中 表头th 合并单元格,且表格列数不定的动态渲染方法

    吐槽 今天,在vue中遇到 复杂表格的渲染 ,需要合并表头的单元格,且合并单元格的那列还是动态数据,也就是说你不知道会有多少组要合并起来,哎,我也有点说不清楚,废话不多说了,看代码把: 代码示例 da ...

  2. 复杂的POI导出Excel表格(多行表头、合并单元格)

    poi导出excel有两种方式: 第一种:从无到有的创建整个excel,通过HSSFWorkbook,HSSFSheet HSSFCell, 等对象一步一步的创建出工作簿,sheet,和单元格,并添加 ...

  3. 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

    本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...

  4. poi导出Excel报表多表头双层表头、合并单元格

    效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @ ...

  5. 【表格设置】HTML中合并单元格,对列组合应用样式,适应各浏览器的内容换行

    1.常用表格标签 普通    <table>           |           <tr>          |           |          <th ...

  6. python-利用xlrd模块中读取有合并单元格的excel数据

    前言 对于excel中有合并单元格的情况,合并的单元格只能取到第一个单元格的值,合并的单元格后面的单元格内容的值为空,针对这个情况,写了下面一段代码实现, 对单元格进行判断,如果是传入的索引是合并单元 ...

  7. 前端Excel表格导入导出,包括合并单元格,表格自定义样式等

    表格数据导入 读取导入Excel表格数据这里采用的是 xlsx 插件 npm i xlsx 读取excel需要通过 XLSX.read(data, {type: type}) 方法来实现,返回一个叫W ...

  8. [C#]合并单元格(行、列)

    详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak説明:控件ID指的是页面上面的Grid ...

  9. el-table——可合并单元格的表格

    <el-table v-loading="loading" :data="tableData" border :span-method="col ...

随机推荐

  1. vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单

    参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...

  2. 使用springmvc实现文件上传

    该配置在javaweb上传文件篇中的基础上进行配置:https://www.cnblogs.com/flypig666/p/11745182.html 1.配置文件解析器,在springmvc.xml ...

  3. 如何撤销Git操作?

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何撤销Git操作?: Git 版本管理时,往往需要撤销某些操作. 本文介绍几种最主要的情况,给出详细的解释.更多的命令可以参考< ...

  4. Vue简单评星效果与单张图片上传

    <form class="" id="pj-frm"> <div class="assess-header"> &l ...

  5. c语言学习笔记 - 枚举类型

    今天学习了c语言的枚举类型的使用,可能是PHP里没使用过,开始看的时候还是觉得有点怪,后来做了下例子才理解,这里做个笔记记录一下. #include <stdio.h> enum anim ...

  6. Putty保存设置

    使用putty连接服务器每次都要配置编码,有时候忘了配置,提示就出现中文乱码真是坑爹(纯英文提示也行啊),好了闲话少说,步入正题. Putty的设置保存功能隐藏的实在太好了,原来在Connection ...

  7. if _name_ == " _main_"

    1.作用 py文件有2种使用方法,第1是自己本脚本自己独立执行:第2是被import到其他文件脚本中执行. if  _name_ == " _main_" 该语句控制其他下一步的脚 ...

  8. Python - 集合与元素之数据类型和变量总结

    变量 变量的作用:保存状态(程序的运行本质是一系列的变化,变量的目的就是用来保存状态,变量值的变化就构成了程序运行的不同结果.) 例如:cs枪战中,一个人的生命可以表示为life = True 表示存 ...

  9. TZ_10_常用的2中加密算法MD5,spring-sucrity

    1.MD5 在注册时需要进行加密,在登陆时也需要加密进行配对 public class MD5util { public static String stringToMD5(String psd) { ...

  10. web前端学习(四)JavaScript学习笔记部分(9)-- JavaScript面向对象详解

    1.认识面向对象 1.1.概念 1.一切事物皆是对象 2.对象具有封装和继承特性 3.信息隐藏(类的信息隐藏,包括属性和方法) <!DOCTYPE html> <html lang= ...