Element UI的table不同应用

目录

一、自定义表头

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

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

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

一、自定义表头

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

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

以课表考勤图为例:

有两种格式的数据:

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

<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、拿到每个日期对应的列数据

<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博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI is a popular Vue.js UI library that provides a wide range of reusable and customizable UI components. One of these components is the table component, which allows developers to display data in a tabular format. To use the Element UI table component, you first need to install the Element UI library and import the necessary components in your project. Once you have done that, you can create a table by defining the columns and the data that you want to display. Here is an example of a basic Element UI table: ``` <template> <el-table :data="tableData"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 35, address: '123 Main St' }, { name: 'Jane', age: 28, address: '456 Elm St' }, { name: 'Bob', age: 42, address: '789 Oak St' } ] } } } </script> ``` In this example, we define a table with three columns: Name, Age, and Address. We also define an array of objects that represent the data that we want to display in the table. The `prop` attribute of each column specifies the key in the data object that corresponds to that column. There are many customization options available for the Element UI table component, such as sorting, filtering, pagination, and row selection. You can also use slots to customize the appearance of the table or add custom functionality.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值