Vue3 简单二次封装vxe-table

本文介绍了如何使用VXE-Table组件创建HTML表格,通过`v-for`和`v-if/v-else`实现列的动态配置,同时支持自定义列内容,特别适合处理后台多表格项目的高效开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用背景:使用table更加标准和简洁,同时兼顾自定义需求

HTML区域


  <vxe-table ...props.config>
    <vxe-column fixed="left" type="seq" align="center" title="序号" width="60" v-if="!props.isSeqConfig" />
    <vxe-column fixed="left" align="center" title="序号" width="60" v-else>
      <template v-slot="{ rowIndex, row }">
        <div class="flex items-center justify-center">
          <span class="mr-1">{{ rowIndex + 1 }}</span>
          <span :class="dynamicClass(row)">•</span>
        </div>
      </template>
    </vxe-column>
    <vxe-column v-for="e in props.TableBody" :fixed="e.fixed" :field="e.field" :title="e.title" :min-width="e.minWidth" :width="e.width" :align="e.align">
      <template v-slot="{ row }" v-if="e.custom">
        <slot :name="e.field" :row="row"></slot>
      </template>
    </vxe-column>
    <slot name="operate"> </slot>
  </vxe-table>

TS区域

interface TableItemType { //表格列的数据格式
  field: string
  title: string
  width?: number
  minWidth?: number
  fixed?: 'right' | 'left'
  custom?: boolean
  align?: 'left' | 'center' | 'right'
}

interface Props {
  TableBody: TableItemType[]
  TableData: any
  Config:any
}

const props = defineProps<Props>()

使用场景

<MyTable :TableBody="TableColumns" :TableData="TableData" height="100%">
    <template v-slot:name="{ row }">
        //个性区域
    </template>
    <template v-slot:operate>
          <vxe-column title="操作" width="100" align="center" fixed="right">
              <template v-slot="{ row }">
                //操作区域
              </template>
          </vxe-column>
    </template>
</MyTable>

具体数据格式

const TableColumns= [
    {
        field: 'name',
        title: '文件名',
        custom: true, //该列存在自己的需求,例如样式或者事件
    },  
    {
        field: 'ordStateStr',
        title: '状态',
        width: 80,
    },
    {
        field: 'doDate',
        title: '上传时间',
        width: 150,
    },
]

经过这么一封装,对于后台多表格项目,非常便利

### 封装 Vue 3 中的 vxe-table 组件 #### 创建基础组件结构 为了在 Vue 3封装 `vxe-table` 组件,首先需要安装并引入必要的依赖库。这包括 `xe-utils`, `vxe-table` 及其样式文件。 ```javascript import { createApp } from &#39;vue&#39;; import &#39;xe-utils&#39; import VXETable from &#39;vxe-table&#39; import &#39;vxe-table/lib/style.css&#39; const app = createApp(App); app.use(VXETable); app.config.globalProperties.$XEUtils = window.XEUtils; ``` #### 定义可重用的 TableComponent.vue 接下来构建一个可以被其他部分重复使用的自定义表格组件: ```html <template> <div class="table-container"> <vxe-table :data="dataTable" border stripe resizable show-footer ref="xTableRef"> <!-- 动态生成表头 --> <vxe-column v-for="(column, index) in columnsConfig" :key="index" :field="column.field" :title="column.title"></vxe-column> <!-- 如果有额外需求比如操作按钮等可以在下方添加slot插槽 --> </vxe-table> </div> </template> <script setup lang="ts"> import { defineProps, reactive, toRefs, onMounted } from &#39;vue&#39;; // 接收父级传入的数据和其他配置选项 interface Props { tableData?: any[]; } const props = withDefaults(defineProps<Props>(), { tableData: () => [] }); let state = reactive({ dataTable: [], columnsConfig: [ // 默认列设置可以根据实际情况调整 ] }) onMounted(() => { initializeColumns(); loadData(); }); function initializeColumns() { if (props.tableData.length > 0 && Array.isArray(props.tableData)) { const firstItemKeys = Object.keys(props.tableData[0]); let newColumnConfigs = []; for (let key of firstItemKeys) { newColumnConfigs.push({ field: key, title: key }); } state.columnsConfig = [...newColumnConfigs]; } } async function loadData() { try { // 假设这里是从API获取数据或者直接使用传入的数据 state.dataTable = await Promise.resolve([...props.tableData]); } catch(error){ console.error(&#39;Failed to load data:&#39;, error); } } </script> <style scoped> /* 自定义样式 */ .table-container { width: 100%; height: auto; } </style> ``` 此代码片段展示了如何创建一个简单的、基于属性驱动的 `vxe-table` 封装组件[^3]。通过这种方式,不仅能够轻松管理不同页面间的公共逻辑,还可以简化维护工作量。 #### 使用封装好的组件 最后,在任何地方都可以像下面这样简单地使用这个新创建的组件了: ```html <!-- ParentComponent.vue --> <template> <div> <h2>My Customized Vxe-Table Component Example</h2> <TableComponent :table-data="myCustomData"/> </div> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; import TableComponent from &#39;./components/TableComponent.vue&#39;; const myCustomData = ref([ {"name": "Alice", "age": 24}, {"name": "Bob", "age": 27} ]) </script> ``` 以上就是关于如何在 Vue 3 中实现对 `vxe-table` 进行二次封装的一个基本指南[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值