el-table组件封装

组件

<template>
  <div class="qzone">
    <slot name="searchBox"></slot>
    <slot name="btnBox"></slot>
    <el-table
      :data="tableData"
      ref="multipleTable"
      style="width: 100%"
      border
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }"
      @selection-change="handleSelectionChange"
      @cell-click="cellClick"
      v-loading="loading"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column
        label="序号"
        type="index"
        width="60"
        :index="indexMethod"
      >
      </el-table-column>
      <el-table-column
      :prop="titleObj.key"
        :label="titleObj.label"
        :width="titleObj.width"
        :min-width="titleObj.minWidth"
        :align="titleObj.align"
        :formatter="titleObj.formatter"
        v-if="showtitleColumn"
      >
        <template slot-scope="scope">
          <slot name="title" :row="scope.row" :index="scope.$index"></slot>
        </template>
      </el-table-column>
      <el-table-column
        :prop="item.key"
        :label="item.title"
        :width="item.width"
        :align="item.align"
        :min-width="item.minWidth"
        :show-overflow-tooltip="item.tooltip"
        v-for="(item, index) in column"
        :key="index"
        :formatter="item.formatter"
      />
      <el-table-column
        fixed="right"
        label="操作"
        :width="actionWidth"
        align="center"
      >
        <template slot-scope="scope">
          <slot name="action" :row="scope.row" :index="scope.$index"></slot>
        </template>
### 封装 `el-table` 组件的 Render 函数 为了实现更灵活的数据展示方式,在 Element UI 的基础上可以对 `el-table` 进行二次封装,使其支持通过配置项来定义每一列的具体显示逻辑。这不仅提高了代码可读性和维护性,还增强了组件的功能扩展能力。 #### 创建自定义 Table 组件 首先创建一个新的 Vue 组件用于包裹原始的 `el-table`,并引入必要的依赖: ```javascript import { defineComponent } from 'vue'; import { ElTable, ElTableColumn } from 'element-plus'; export default defineComponent({ name: 'CustomElTable', props: { columns: Array, data: Array }, }); ``` 在此基础上,利用 `columns` 属性传递给子组件的信息来动态生成表格列结构,并处理其中可能存在的 `render` 方法: ```html <template> <el-table :data="data"> <!-- 遍历传入的列配置 --> <el-table-column v-for="(column, index) in columns" :key="index" :prop="column.prop" :label="column.label" :width="column.width || ''"> <!-- 如果存在 render 函数,则调用它来自定义渲染单元格内容 --> <template #default="{ row }" v-if="typeof column.render === 'function'"> {{ column.render(row[column.prop], row) }} </template> <!-- 否则按照默认方式呈现数据 --> <template #default="{ row }" v-else> {{ row[column.prop] }} </template> </el-table-column> </el-table> </template> ``` 上述模板中,当某列提供了 `render` 函数时就会执行该函数并将结果作为当前单元格的内容;如果没有提供,则简单地取出对应字段值进行显示[^1]。 #### 使用示例 现在可以在其他地方轻松使用这个增强版的 table 组件了,只需要准备好合适的配置即可: ```javascript const app = createApp({}); app.component('custom-el-table', CustomElTable); new app.mount('#app'); ``` 配合如下 JSON 格式的列描述符数组一起工作: ```json [ {"label": "姓名", "prop": "name"}, {"label": "年龄", "prop": "age", "render": (value, row) => `${value}岁`, } ] ``` 这样就实现了基于配置文件驱动的方式来自定义每列表现的行为模式,同时也保留了原生 API 的易用特性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值