vue3+elementui实现表格样式可配置

后端接口传回的数据格式如下图
需要依靠后端传回的数据控制表格样式

实现代码

javascript 复制代码
<!-- 可视化配置-表格 -->
<template>
  <div class="tabulation_main" ref="myDiv">
    <!-- 尝试过在mounted中使用this.$refs.myDiv.offsetHeight,获取父元素高度赋值给height的方法,发现该值只能在created之前确定,且不为动态 -->
    <el-table
      :data="tableData"
      :stripe="tableStyleObj.tableStyle.stripe == 'true' ? true : false"
      :row-style="rowStyleHandle"
      :row-class-name="tableRowClassName"
      :border="tableStyleObj.tableStyle.border == 'true' ? true : false"
      v-model:align="tableStyleObj.tableStyle.bodyTextAlign"
      :show-header="tableStyleObj.tableStyle.showHeader == 'true' ? true : false"
      :header-cell-style="{
        color: tableStyleObj.tableStyle.headerColor,
        'font-size': tableStyleObj.tableStyle.headerFontSize + 'px',
        'text-align': tableStyleObj.tableStyle.headerTextAlign,
      }"
      :header-row-class-name="headerRowClassName"
      :height="tableStyleObj.tableStyle.tableHeight + ''"
      style="width: 100%"
    >
      <!-- 固定序号列 -->
      <el-table-column
        v-if="tableStyleObj.tableStyle.isShowTableIndex == 'true' ? true : false"
        :fixed="tableStyleObj.tableStyle.isTableIndexFixed == 'true' ? true : false"
        type="index"
        :width="tableStyleObj.tableStyle.tableIndexWidth + ''"
      ></el-table-column>
      <el-table-column
        v-for="(tableItem, tableIndex) in tableStyleObj.tableColStyle"
        :key="tableIndex"
        :prop="tableItem.prop"
        :label="tableItem.label"
        :width="tableItem.width + ''"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableStyleObj: {
        // 表格整体样式
        tableStyle: {},
        // 表格列配置
        tableColStyle: [],
      },
      tableData: [],
    };
  },
  props: {
    flatteningCurrentItemForm: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  computed: {
    nthColor() {
      return this.tableStyleObj.tableStyle.nthColor;
    },
    othColor() {
      return this.tableStyleObj.tableStyle.othColor;
    },
    headerBackground() {
      return this.tableStyleObj.tableStyle.headerBackground;
    },
  },
  created() {
    // 获取表格配置
    this.tableStyleObj.tableStyle = this.flatteningCurrentItemForm.attributeCaseMap;
    this.tableStyleObj.tableColStyle = JSON.parse(
      this.flatteningCurrentItemForm.attributeCaseMap.tableColstyle
    );
    // 获取表格数据
    tableDataSource({
      dataSource: this.flatteningCurrentItemForm.dataSource,
    }).then((res) => {
      this.tableData = res.data;
    });
  },
  methods: {
    headerRowClassName({ row, rowIndex }) {
      return "head-row";
    },
    rowStyleHandle({ row, rowIndex }) {
      // rowIndex从0开始
      if (rowIndex % 2 == 0) {
        // 奇数行
        var obj = {
          "text-align": this.tableStyleObj.tableStyle.bodyTextAlign,
          color: this.tableStyleObj.tableStyle.bodyColor,
          "font-size": this.tableStyleObj.tableStyle.bodyFontSize + "px",
        };
        return obj;
      } else {
        // 偶数行
        var obj = {
          "text-align": this.tableStyleObj.tableStyle.bodyTextAlign,
          color: this.tableStyleObj.tableStyle.bodyColor,
          "font-size": this.tableStyleObj.tableStyle.bodyFontSize + "px",
        };
        return obj;
      }
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 0) {
        return "even-row";
      } else {
        return "odd-row";
      }
    },
  },
};
</script>

<style scoped lang="scss">
.tabulation_main {
  // 表格表头
  ::v-deep(.el-table .el-table__header-wrapper th),
  ::v-deep(.el-table .el-table__fixed-header-wrapper th) {
    background-color: v-bind("headerBackground") !important;
  }
  // 表格斑马纹
  ::v-deep(.even-row td) {
    background-color: v-bind("nthColor") !important;
  }
  ::v-deep(.odd-row td) {
    background-color: v-bind("othColor") !important;
  }
  ::v-deep(.el-table) {
    background-color: v-bind("nthColor") !important;
  }
}
</style>
相关推荐
程序员马晓博5 分钟前
深入聊聊Qwen3的混合推理:全球唯三,开源唯一
前端·后端
懋学的前端攻城狮6 分钟前
Vue源码解析-01:从创建到挂载的完整流程
前端·vue.js·源码
Allen Bright11 分钟前
【CSS-5】掌握CSS文本样式:从基础到高级技巧
前端·css
贩卖纯净水.18 分钟前
Webpack常见的插件和模式
前端·webpack·node.js
brzhang26 分钟前
Flutter 调用原生代码,看这篇就够了:从零教你搭起通信的桥
前端·后端·架构
袁煦丞27 分钟前
知识管理的六边形战士Trilium Notes:cpolar内网穿透实验室第520个成功挑战
前端·程序员·远程工作
失败又激情的man36 分钟前
python爬虫之数据存储
前端·数据库·python
互联网搬砖老肖37 分钟前
Web 架构之 API 安全防护:防刷、防爬、防泄漏
前端·安全·架构
小声读源码1 小时前
【技巧】dify前端源代码修改第一弹-增加tab页
前端·pnpm·next.js·dify
假客套1 小时前
2025 后端自学UNIAPP【项目实战:旅游项目】7、景点详情页面【完结】
前端·uni-app·旅游