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>
相关推荐
你挚爱的强哥9 分钟前
【sgCreateCallAPIFunctionParam】自定义小工具:敏捷开发→调用接口方法参数生成工具
前端·javascript·vue.js
喝旺仔la14 分钟前
Element 表格相关操作
javascript·vue.js·elementui
繁依Fanyi17 分钟前
使用 Spring Boot + Redis + Vue 实现动态路由加载页面
开发语言·vue.js·pytorch·spring boot·redis·python·算法
米老鼠的摩托车日记17 分钟前
【vue element-ui】关于删除按钮的提示框,可一键复制
前端·javascript·vue.js
forwardMyLife17 分钟前
element-plus的菜单组件el-menu
javascript·vue.js·elementui
猿饵块1 小时前
cmake--get_filename_component
java·前端·c++
大表哥61 小时前
在react中 使用redux
前端·react.js·前端框架
十月ooOO1 小时前
【解决】chrome 谷歌浏览器,鼠标点击任何区域都是 Input 输入框的状态,能看到输入的光标
前端·chrome·计算机外设
qq_339191141 小时前
spring boot admin集成,springboot2.x集成监控
java·前端·spring boot
pan_junbiao1 小时前
Vue使用代理方式解决跨域问题
前端·javascript·vue.js