基于elementUI 的custom-table 组件简单封装

文章展示了如何在Vue.js中封装一个自定义表格组件,包括是否显示选择框、序号、自定义属性如求和方法,以及多级列的处理。同时,组件还支持合并单元格和操作列功能。示例代码中详细解释了各个属性和方法的用法。
  • 定义table 的 scheme

table.js

export const manualOrderTable = {
  hasSelect: false, // 是否展示选择框
  hasIndex: false, // 是否展示序号
  constomTableAttrs: {  // table 中的自定义属性
    "summary-method": (param) => {  // 求和
      debugger
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    }
    // "span-method": ({ row, column, rowIndex, columnIndex })=>{ // 合并单元格
    //   if (rowIndex % 2 === 0) {
    //     if (columnIndex === 0) {
    //       return [1, 2];
    //     } else if (columnIndex === 1) {
    //       return [0, 0];
    //     }
    //   }
    // }
  },
  columns: [
    { prop: 'createTime', label: '创建时间', width: '145', comstomProp: {} },
    { prop: 'title', label: '标题', comstomProp: {} },
    { prop: 'leadOrgName', label: '责任单位', width: '145', comstomProp: {} },
    { prop: 'createName', label: '创建人', width: '145', comstomProp: {} },
    { prop: 'orderStatusName',label: '问题单状态', comstomProp: {},children: [
      {
        prop: 'orderStatusName',label: '问题单状态1',
        children: [
          { prop: 'orderStatusName',label: '问题单状态1-1'},
          { prop: 'orderStatusName',label: '问题单状态1-2'},
        ]
      },
      {
        prop: 'orderStatusName',label: '问题单状态2',
        children: [
          { prop: 'orderStatusName',label: '问题单状态2-1'},
          { prop: 'orderStatusName',label: '问题单状态2-2'},
        ]
      },
    ]},
    { prop: 'content', label: '问题内容', comstomProp: {}},
    { prop: 'oper', label: '操作列', width: '145', comstomProp: {} }
  ]
}
  • 封装的组件table.vue
<template>
  <div>
    <el-table
      ref="RefCustomTable"
      v-bind="{...$attrs,...constomTableAttrs}"
      v-on="$listeners"
      :data="data"
      header-cell-class-name="table-header-cell"
      header-row-class-name="table-header-class"
      :row-key="rowKey"
      style="width: 100%;"
    >
      <el-table-column
        v-if="hasIndex"
        :label="label||'序号'"
        type="index"
        :width="80"
      ></el-table-column>
      <!-- TODO:多选点击事件还没绑定 -->
      <el-table-column
          v-if="hasSelect"
          :selectable="selectable"
          type="selection"
          width="55"
      ></el-table-column>
      <!-- 列表字段 -->
      <slot v-for="item in columns" :name="item.prop" v-bind="item.constomProp">
        <el-table-column
          v-if="!item.children"
          :key="item.prop"
          :formatter="item.formatter"
          :label="item.label"
          :prop="item.prop"
          :resizable="false"
          :show-overflow-tooltip="true"
          :sortable="item.sortable"
          :width="item.width"
          :align="item.align ||'center'"
          >
        </el-table-column>
        <MultiColumn v-else :item="item"></MultiColumn>
      </slot>
    </el-table>
  </div>
</template>

<script>
import MultiColumn from "./components/multi-column.vue"
  export default {
    components: {
      MultiColumn
    },
    props: {
      data: {
        type: Array,
        default: ()=>{
          return []
        }
      },
      constomTableAttrs: {
        type: Object,
        default: ()=>{}
      },
      rowKey: {
        type: String,
        default: 'id'
      },
      hasIndex: {
        type: Boolean,
        default: false
      },
      hasSelect: {
        type: Boolean,
        default: false
      },
      columns: {
        type: Array,
        default: ()=>{
          return []
        }
      }
    },
  }
</script>

  • 引入的多列递归组件MultiColumn.vue
<template>
  <el-table-column
      :key="item.prop"
      :class-name="item.columnClass"
      :formatter="item.formatter"
      :label="item.label"
      :min-width="item.minWidth"
      :prop="item.prop"
      :show-overflow-tooltip="true"
      :sortable="item.sortable"
      :width="item.width"
      :align="item.align||'center'"
  >
    <template v-if="item.children">
      <multi-column v-for="(col, index) in item.children" :key="index" :item="col"></multi-column>
    </template>
  </el-table-column>
</template>

<script>
export default {
  name: "MultiColumn",
  props: {
    item: {
      type: Object
    }
  }
};
</script>
  • 实际使用
...代码片段
<custom-table
      :data="tableData"
      :border="true"
      show-summary
      :hasSelect="manualOrderTable.hasSelect"
      :hasIndex="manualOrderTable.hasIndex"
      @row-click="handleRowClick"
      :constomTableAttrs="manualOrderTable.constomTableAttrs"
      :columns="manualOrderTable.columns">
        <template v-slot:oper>
          <el-table-column align="center" label="操作列">
            <template slot-scope="scope">
              <el-button v-if="searchParams.type == 2" :disabled="scope.row.orderStatus != 1" type="text" @click.stop="approvalNode(scope, searchParams.type)">审核</el-button>
              <el-button v-if="searchParams.type == 3" :disabled="scope.row.orderStatus != 2" type="text" @click.stop="approvalNode(scope, searchParams.type)">治理</el-button>
              <el-button v-if="searchParams.type == 4" :disabled="scope.row.orderStatus != 3" type="text" @click.stop="approvalNode(scope, searchParams.type)">确认</el-button>
              <el-button type="text" @click.stop="goDetail(scope.row, scope.row.orderStatus, true)">详情</el-button>
            </template>
          </el-table-column>
        </template>
</custom-table>
...代码片段

<script>
import { manualOrderTable } from './table'
import customTable from '@/components/common/table'
export default {
    name: 'manualOder',
    components: {
      customTable,
      ...代码片段
    },
    ...代码片段
 }
<script>

撒花

在 Vue 2 中使用 Element UI 的 `el-table` 组件时,如果你想要修改表格的边框颜色(包括外边框和单元格边框),由于 Element UI 的样式是基于类名封装的,其默认样式可能被 CSS 作用域或优先级问题限制,因此需要通过 **深度选择器** 来覆盖默认样式。 Element UI 的 `el-table` 边框主要由以下 CSS 控制: - 外边框:`.el-table__body-wrapper, .el-table__header-wrapper` - 单元格边框:`.el-table td, .el-table th` 上的 `border` 属性 - 表格整体边框:`.el-table` --- ### ✅ 修改 el-table 边框颜色的方法 #### 方法一:使用深度选择器(推荐) ```vue <template> <el-table :data="tableData" class="custom-table"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 25, address: '北京' }, { name: '李四', age: 30, address: '上海' }, ] } } } </script> <style scoped> /* 使用深度选择器穿透 scoped 样式 */ .custom-table ::v-deep(.el-table) { border: 2px solid #ff6347 !important; /* 修改外边框颜色为番茄红 */ } .custom-table ::v-deep(.el-table td), .custom-table ::v-deep(.el-table th) { border: 1px solid #ff6347; /* 修改内部单元格边框颜色 */ } /* 可选:去掉表头底部加粗边框的干扰 */ .custom-table ::v-deep(.el-table__header-wrapper th) { border-bottom: 1px solid #ff6347 !important; } </style> ``` --- ### 🔍 解释说明: - `::v-deep()` 是 Vue 单文件组件中用于 **样式穿透** 的深度选择器,允许你在 `scoped` 样式下修改子组件(如 Element UI 的组件)的内部样式。 - `.el-table` 默认有 `border: 1px solid #ebeef5`,我们用 `!important` 覆盖它。 - `td` 和 `th` 元素默认带有边框,需单独设置。 - `class="custom-table"` 是为了提高选择器优先级,避免被其他样式覆盖。 --- ### 💡 其他写法(根据构建工具不同) 如果你使用的是 **Sass/SCSS** 或 **Less**,可以这样写: #### SCSS 写法: ```scss .custom-table { /deep/ .el-table { border: 2px solid #ff6347; } /deep/ .el-table td, /deep/ .el-table th { border-color: #ff6347; } } ``` #### Less 写法: ```less .custom-table { ::v-deep(.el-table) { border: 2px solid #ff6347; td, th { border-color: #ff6347; } } } ``` --- ### ⚠️ 注意事项 1. **不要滥用 `!important`**,但在覆盖第三方库样式时有时不可避免。 2. 如果全局修改,可以直接在全局 CSS 中覆盖 `.el-table` 相关类,无需深度选择器。 3. 某些版本的 Element UI 使用 `.has-gutter` 控制表头边框,如有需要也加上: ```css ::v-deep(.el-table .has-gutter th) { border-color: #ff6347; } ``` --- ### ✅ 效果 最终你会看到一个红色边框的表格,包括外框和每个单元格的边框。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值