Element UI的table不同应用

目录

一、自定义表头

二、纵向表头(动态表头)

2.1、分别拿到表头和表头中日期对应的行数据

2.2、拿到每个日期对应的列数据

一、自定义表头

html 复制代码
          <el-table-column prop="chu" align="center">
            <!-- 自定义表头 -->
            <template slot="header"
              ><span class="circle" style="background: #5dd49f"></span
              >出勤</template
            >
          </el-table-column>

二、纵向表头(动态表头)

以课表考勤图为例:

有两种格式的数据:

2.1、分别拿到表头和表头中该课节对应的行数据

html 复制代码
<el-table
:data="tableOldData"
style="width: 100%"
border
:row-style="{ height: '100px' }"
:header-cell-style="{background: '#F8F8F8',color: '#172b4d',height: '70px',}"
v-if="visibleCalendar.length > 0">
            <el-table-column
              prop="name"
              label="课节"
              fixed="left"
              width="120"
              align="center"
            >
            </el-table-column>
            <el-table-column
              :label="item.date + item.week"
              v-for="(item, index) in visibleCalendar"
              :key="index"
              align="center"
            >
              <template slot-scope="{ row }">
                <div class="cell_boxs" v-if="row.dayData[item.date].type > -1">
                  <div class="cell_tops">
                    <span
                      :style="
                        'background-color:' +
                        colorType(row.dayData[item.date].type)
                      "
                      class="circle"
                    ></span>
                    <span>{{
                      typeStatistics(row.dayData[item.date].type)
                    }}</span>
                  </div>
                  <div class="cell_bottom">
                    <span>{{ row.dayData[item.date].subject }}</span>
                    <span>{{ row.dayData[item.date].name }}</span>
                  </div>
                </div>
              </template>
            </el-table-column>
</el-table>
<script>
data() {
   return {
      visibleCalendar: [
        { date: "2023-09-26", week: "周六" },
        { date: "2023-10-25", week: "周一" },
        { date: "2023-10-26", week: "周二" },
        { date: "2023-10-27", week: "周三" },
        { date: "2023-10-28", week: "周四" },
        { date: "2023-10-29", week: "周五" },
      ],
      dateTable: [
        {
          name: "第1-2节",
          dayData: {
            "2023-10-25": {
              type: 1,
              name: "赵翔",
              subject: "英语写作基础",
            },
            "2023-10-26": {
              type: 0,
              name: "赵翔",
              subject: "英语写作基础",
            },
            "2023-10-27": {
              type: 2,
              name: "赵翔",
              subject: "英语写作基础",
            },
          },
        },
        {
          name: "第3-4节",
          dayData: {
            "2023-09-26": {
              type: 2,
              name: "都会迟",
              subject: "奥术模拟",
            },
          },
        },
        {
          name: "第5-6节",
          dayData: {
            "2023-10-28": {
              type: 3,
              name: "王鹏",
              subject: "新闻播报",
            },
            "2023-10-29": {
              type: 4,
              name: "王鹏",
              subject: "新闻播报",
            },
          },
        },
      ],
}
},
  computed: {
    tableOldData() {
      const oldData = [];
      this.dateTable.forEach((item) => {
        const newItem = { ...item };
        const dayData = newItem.dayData;
        newItem.dayData = {};
        this.visibleCalendar.forEach((item) => {
          let oldDate = item.date;
          if (dayData[oldDate]) {
            newItem.dayData[oldDate] = dayData[oldDate];
          } else {
            newItem.dayData[oldDate] = {};
          }
        });
        oldData.push(newItem);
      });
      return oldData;
    },
  },
  methods: {
    typeStatistics(index) {
      const status = ["出勤", "迟到", "旷课", "早退", "请假"];
      return status[index] || "";
    },
    colorType(index) {
      const colors = ["#6BC25E", "#F0A55C", "#E77575", "#F0A55C", "#6FBFF1"];
      return colors[index] || "";
    },
}
</script>

tableOldData是表格上最终要渲染的数据源;visibleCalendar是返回的表头;dateTable是表头中的日期对应的全部数据。渲染的时候,根据visibleCalendar里的日期去找对应的值。

2.2、拿到每个日期对应的列数据

html 复制代码
<el-table
style="width: 100%"
:data="getValues"
:show-header="false"
border>
            <el-table-column fixed width="120" align="center">
              <template slot-scope="{ row }">
                <div class="cell_header">
                  {{ row.title }}
                </div>
              </template>
            </el-table-column>
            <!--(拿到列数据时) 纵向垂直表头 -->
            <!-- 左侧固定,右侧列数动态变化,min-width设置宽度 -->
            <template v-for="(item, i) in getHeaders">
              <el-table-column
                v-if="item != 'title'"
                :show-overflow-tooltip="true"
                :label="item"
                :key="i"
                min-width="174"
                align="center"
              >
                <template slot-scope="{ row }">
                  <div class="cell_boxs" v-if="row[item] && row[item].name">
                    <div class="cell_tops">
                      <span
                        :style="'background-color:' + colorType(row[item].type)"
                        class="circle"
                      ></span>
                      <span>{{ typeStatistics(row[item].type) }}</span>
                    </div>
                    <div class="cell_bottom">
                      <span>{{ row[item].subject }}</span>
                      <span>{{ row[item].name }}</span>
                    </div>
                  </div>
                  <div class="cell_header" v-else>
                    {{ row[`value${i - 1}`] }}
                  </div>
                </template>
              </el-table-column>
            </template>
</el-table>
<script>
data() {
   return {
       headers: [
        {
          prop: "date",
          label: "课节",
        },
        {
          prop: "one",
          label: "第1-2节",
        },
        {
          prop: "two",
          label: "第3-4节",
        },
        {
          prop: "three",
          label: "第5-6节",
        },
        {
          prop: "four",
          label: "第7-8节",
        },
        {
          prop: "five",
          label: "第9-10节",
        },
      ],
    otherDatas: [
        {
          date: "2023-10-25",
          one: { type: 1, name: "赵翔1", subject: "英语写作基础" },
        },
        {
          date: "2023-10-26",
          three: { type: 2, name: "赵翔6", subject: "英语写作基础" },
        },
        {
          date: "2023-10-27",
          one: { type: 3, name: "赵翔7", subject: "英语写作基础" },
        },
        {
          date: "2023-10-28",
          one: { type: 4, name: "赵翔7", subject: "英语写作基础" },
          two: { type: 1, name: "赵翔8", subject: "英语写作基础" },
          three: { type: 2, name: "赵翔9", subject: "英语写作基础" },
        },
      ],
}
},
  computed: {
    getHeaders() {
      return this.otherDatas.reduce(
        //对数组累积操作
        (pre, cur, index) => pre.concat(`value${index}`),
        ["title"]
      );
    },
    getValues() {
      return this.headers.map((item) => {
        return this.otherDatas.reduce(
          (pre, cur, index) =>
            Object.assign(pre, { ["value" + index]: cur[item.prop] }),
          { title: item.label }
        );
      });
    },
  },
  methods: {
    typeStatistics(index) {
      const status = ["出勤", "迟到", "旷课", "早退", "请假"];
      return status[index] || "";
    },
    colorType(index) {
      const colors = ["#6BC25E", "#F0A55C", "#E77575", "#F0A55C", "#6FBFF1"];
      return colors[index] || "";
    },
}
</script>

通过headers这个数组确定要组装的数据源otherDatas格式,这样出来的数据每一行就是表格的row了。

第二种格式数据参考博客:el-table 纵向垂直表头_el-table纵向表头_wh4834的博客-CSDN博客

相关推荐
秃头网友小李3 天前
前端难点:keep-alive 缓存什么?RouterView 的 key 为什么要带 scopeId?
前端·vue.js
徐小夕3 天前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
奋斗吧程序媛3 天前
补充一个小知识点:有关@click.native
前端·vue.js
laowangpython3 天前
Photoshop 2025 下载安装全攻略
其他·ui·photoshop
英勇无比的消炎药3 天前
一行命令背后:TinyRobot CLI 如何重构 AI 对话接入的效率范式
vue.js·aigc
jay神3 天前
基于 FastAPI + Vue 的宠物领养管理系统
前端·vue.js·python·毕业设计·fastapi·宠物
一杯奶茶¥3 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
风华圆舞4 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
英勇无比的消炎药4 天前
一站式搞定品牌风格:TinyRobot 主题定制从入门到精通
vue.js
鲲穹AI超级员工4 天前
多款实用配色工具汇总,适配设计、UI 创作等多元场景
ui·色彩设计