当前位置:首页 > VUE

vue实现表

2026-03-27 11:58:38VUE

Vue 实现表格的基本方法

使用 Vue 实现表格可以通过多种方式,包括原生 HTML 表格、第三方组件库(如 Element UI、Ant Design Vue)或自定义组件。以下是几种常见实现方式:

原生 HTML 表格

通过 v-for 指令循环渲染表格数据:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in rows" :key="row.id">
        <td v-for="(value, key) in row" :key="key">{{ value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['ID', 'Name', 'Age'],
      rows: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ]
    }
  }
}
</script>

使用 Element UI 表格组件

安装 Element UI 后直接使用 el-table

<template>
  <el-table :data="tableData">
    <el-table-column prop="id" label="ID"></el-table-column>
    <el-table-column prop="name" label="Name"></el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ]
    }
  }
}
</script>

表格功能扩展

分页功能

结合 el-pagination 实现分页:

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

<script>
export default {
  computed: {
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.tableData.slice(start, start + this.pageSize)
    }
  }
}
</script>

排序功能

通过 sortable 属性启用列排序:

<el-table-column prop="age" label="Age" sortable></el-table-column>

自定义表格组件

创建可复用的表格组件:

<!-- TableComponent.vue -->
<template>
  <table>
    <slot name="header"></slot>
    <slot name="body" :rows="rows"></slot>
  </table>
</template>

<script>
export default {
  props: ['rows']
}
</script>

使用时通过插槽自定义内容:

vue实现表

<TableComponent :rows="tableData">
  <template #header>
    <tr>
      <th>ID</th>
      <th>Name</th>
    </tr>
  </template>
  <template #body="{ rows }">
    <tr v-for="row in rows" :key="row.id">
      <td>{{ row.id }}</td>
      <td>{{ row.name }}</td>
    </tr>
  </template>
</TableComponent>

标签: vue
分享给朋友:

相关文章

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现付款

vue实现付款

Vue 实现付款功能 在 Vue 中实现付款功能通常需要集成第三方支付网关(如支付宝、微信支付、Stripe 等)。以下是常见的实现方法: 集成支付宝/微信支付 安装必要的依赖(如 axios 用于…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…