当前位置:首页 > 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

vue实现表

<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>

排序功能

vue实现表

通过 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>

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

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

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue框架实现

vue框架实现

Vue 框架实现方法 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。以下是一些核心实现方法,涵盖基本使用、组件化、状态管理和优化技巧。 基本项目搭建 使用 Vue CLI…

vue实现slidedown

vue实现slidedown

Vue 实现 SlideDown 动画效果 在 Vue 中实现 SlideDown 效果可以通过 CSS 过渡或动画结合 Vue 的过渡系统完成。以下是几种常见方法: 使用 Vue Transiti…

vue 实现路径

vue 实现路径

在 Vue 中实现路径管理通常涉及路由配置、动态路由、嵌套路由等场景,以下是具体实现方法: 基础路由配置 安装 Vue Router 并创建路由实例: npm install vue-router…

vue实现搜索查询

vue实现搜索查询

Vue 实现搜索查询的方法 双向绑定实现实时搜索 在Vue中可以通过v-model绑定输入框的值,结合计算属性或watch监听输入变化实现实时搜索。 <template> <i…