当前位置:首页 > VUE

vue实现table封装

2026-03-29 06:19:45VUE

Vue实现Table封装方法

封装一个可复用的Table组件是Vue项目中常见的需求,以下是一个基于Element UI或Ant Design Vue的封装方案:

基础Table封装

<template>
  <el-table
    :data="tableData"
    :border="border"
    :stripe="stripe"
    @selection-change="handleSelectionChange"
  >
    <template v-for="(col, index) in columns">
      <el-table-column
        v-if="col.type"
        :key="index"
        :type="col.type"
        :width="col.width"
      />
      <el-table-column
        v-else
        :key="index"
        :prop="col.prop"
        :label="col.label"
        :width="col.width"
      >
        <template #default="scope">
          <slot :name="col.prop" :row="scope.row">
            {{ scope.row[col.prop] }}
          </slot>
        </template>
      </el-table-column>
    </template>
  </el-table>
</template>

<script>
export default {
  props: {
    tableData: {
      type: Array,
      default: () => []
    },
    columns: {
      type: Array,
      default: () => []
    },
    border: Boolean,
    stripe: Boolean
  },
  methods: {
    handleSelectionChange(val) {
      this.$emit('selection-change', val)
    }
  }
}
</script>

使用示例

<template>
  <CustomTable
    :table-data="data"
    :columns="columns"
    border
    stripe
    @selection-change="handleSelect"
  >
    <template #status="{ row }">
      <el-tag :type="row.status | statusType">
        {{ row.status }}
      </el-tag>
    </template>
  </CustomTable>
</template>

<script>
export default {
  data() {
    return {
      data: [...],
      columns: [
        { type: 'selection', width: 55 },
        { prop: 'name', label: '姓名' },
        { prop: 'age', label: '年龄' },
        { prop: 'status', label: '状态' }
      ]
    }
  }
}
</script>

高级功能扩展

分页处理

<template>
  <div>
    <el-table ... />
    <el-pagination
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      @current-change="handlePageChange"
    />
  </div>
</template>

动态列配置

props: {
  showColumns: {
    type: Array,
    default: () => [],
    validator: value => value.every(item => typeof item === 'string')
  }
},
computed: {
  filteredColumns() {
    return this.columns.filter(col => 
      this.showColumns.includes(col.prop) || col.type
    )
  }
}

排序和筛选

<el-table-column
  :prop="col.prop"
  :label="col.label"
  sortable
  :filters="col.filters"
  :filter-method="col.filterMethod"
/>

性能优化技巧

使用v-if控制列的显示而非v-forv-show,避免不必要的渲染

大数据量时考虑虚拟滚动方案,可使用vue-virtual-scroller等库

复杂单元格内容使用作用域插槽时,考虑提取为单独组件

vue实现table封装

对于固定列场景,合理设置fixed属性并注意浏览器兼容性

标签: vuetable
分享给朋友:

相关文章

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现评分

vue实现评分

Vue 实现评分功能 使用组件库实现 安装 element-ui 或 ant-design-vue 这类 UI 库,它们已经内置了评分组件。 以 element-ui 为例: <te…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…