el-table 纵向垂直表头

html 复制代码
<template>
  <div class="element-main">
    <div> Element-ui 官方提供 table Demo</div>
    <el-table
      border
      style="width: 100%"
      :data="tableData">
      <el-table-column prop="courseName" label="课程信息" width="180"></el-table-column>
      <el-table-column prop="courseBook" label="教材信息" width="180"></el-table-column>
      <el-table-column prop="price" label="单价"></el-table-column>
    </el-table>
    <div>===================================== 分割线 =====================================</div>
    <div>行列转换后的 Demo</div>
    <el-table
      border
      style="width: 100%"
      :data="getValues"
      :show-header="false">
      <el-table-column
        v-for="(item, index) in getHeaders"
        :key="index"
        :prop="item">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      headers: [
        {prop: 'courseName',label: '课程信息'},
        {prop: 'courseBook', label: '教材信息'},
        {prop: 'price',label: '单价'},
      ],
      tableData:[
        {"courseId": 1,"courseName": "英语写作", "courseBook": "英语写作基础教程","price": 3500},
        {"courseId": 2,"courseName": "综合英语(三)",": "新编英语教程2","price": 3350}, 
        {"courseId": 3,"courseName": "英语国家社会与文化","courseBook": "致用英语 英语国家概况 (第二版)","price": 2900 },
        {"courseId": 3,"courseName": "英语国家社会与文化","courseBook": "致用英语 英语国家概况 (第二版)","price": 2900,}
     ],
    }
  },
  computed: {
    getHeaders() {
      return this.tableData.reduce((pre, cur, index) => pre.concat(`value${index}`), ['title'])
    },
    getValues() {
      return this.headers.map(item => {
        return this.tableData.reduce((pre, cur, index) => Object.assign(pre, {['value' + index]: cur[item.prop]}), {'title': item.label,});
      });
    }
  },
};
</script>

参考链接:https://www.jianshu.com/p/1f38eaffd070

相关推荐
雪芽蓝域zzs14 天前
第7节:多选模式、自定义选项渲染、受控弹窗显隐
vue.js·elementui
雪芽蓝域zzs15 天前
第6节:远程搜索弹窗(remote 模式),弹窗滚动加载分页
vue.js·elementui
雪芽蓝域zzs16 天前
第4节:el-select 弹窗事件控制(打开 / 关闭、选中事件、弹窗滚动事件)
vue.js·elementui
ss27316 天前
AI全栈实战 | 2.3-01 组件库实战:Element Plus 怎么搭出专业界面,过度封装为什么成维护负担
前端·javascript·elementui·vue
雪芽蓝域zzs16 天前
第3节:el-select 弹窗定位、滚动穿透与挂载容器(popper-append-to-body /teleported)
vue.js·elementui
雪芽蓝域zzs17 天前
第 2 节:基础扩展 - 下拉弹窗常用原生属性
vue.js·elementui
雪芽蓝域zzs17 天前
Vue3 + Element Plus el-select 下拉弹窗
vue.js·elementui
朱 欢 庆21 天前
el-table 表格组件二次封装
javascript·vue.js·elementui
ℋᙚᵐⁱᒻᵉ鲸落1 个月前
ETypeError: nameRef.value?.focus is not a functio. 设置el-input自动聚焦时报错
前端·javascript·vue.js·elementui
愛芳芳1 个月前
基于 Electron + Vue3 的仿 PC 微信客户端项目实战
前端·javascript·css·elementui·typescript·electron·vue