当前位置:首页 > VUE

vue实现table

2026-01-13 03:54:24VUE

Vue 实现 Table 的基本方法

使用 Vue 实现表格功能可以通过多种方式完成,包括原生 HTML 表格、第三方组件库或自定义组件。以下是几种常见的方法:

原生 HTML 表格 在 Vue 模板中直接使用 <table> 标签,结合 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="cell in row.cells" :key="cell.id">{{ cell.value }}</td>
      </tr>
    </tbody>
  </table>
</template>
<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      rows: [
        { id: 1, cells: [{ id: 1, value: 'John' }, { id: 2, value: 25 }, { id: 3, value: 'john@example.com' }] },
        { id: 2, cells: [{ id: 4, value: 'Jane' }, { id: 5, value: 30 }, { id: 6, value: 'jane@example.com' }] }
      ]
    }
  }
}
</script>

使用 Element UI 的 Table 组件 Element UI 提供了功能丰富的表格组件:

<template>
  <el-table :data="tableData">
    <el-table-column prop="name" label="Name"></el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
    <el-table-column prop="email" label="Email"></el-table-column>
  </el-table>
</template>
<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25, email: 'john@example.com' },
        { name: 'Jane', age: 30, email: 'jane@example.com' }
      ]
    }
  }
}
</script>

实现高级表格功能

分页功能 结合分页组件实现数据分页:

<template>
  <div>
    <el-table :data="currentPageData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="totalItems">
    </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [...],
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    currentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.tableData.slice(start, end)
    },
    totalItems() {
      return this.tableData.length
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

排序功能 添加排序功能可以通过自定义排序方法或使用组件内置排序:

<template>
  <el-table :data="tableData" @sort-change="handleSortChange">
    <el-table-column prop="name" label="Name" sortable></el-table-column>
    <el-table-column prop="age" label="Age" sortable></el-table-column>
  </el-table>
</template>
<script>
export default {
  methods: {
    handleSortChange({ prop, order }) {
      this.tableData.sort((a, b) => {
        if (order === 'ascending') {
          return a[prop] > b[prop] ? 1 : -1
        } else {
          return a[prop] < b[prop] ? 1 : -1
        }
      })
    }
  }
}
</script>

性能优化建议

对于大数据量表格,考虑使用虚拟滚动技术避免渲染所有行:

<template>
  <virtual-list :size="50" :remain="10" :data="largeData">
    <table>
      <tr v-for="item in largeData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
  </virtual-list>
</template>

表格数据更新时使用 key 属性帮助 Vue 高效更新 DOM:

<tr v-for="item in items" :key="item.id">
  <!-- 单元格内容 -->
</tr>

自定义表格组件

创建可复用的表格组件:

<!-- TableComponent.vue -->
<template>
  <table>
    <thead>
      <slot name="header"></slot>
    </thead>
    <tbody>
      <slot name="body" v-for="item in data" :item="item"></slot>
    </tbody>
  </table>
</template>
<script>
export default {
  props: ['data']
}
</script>

<!-- 使用 -->
<table-component :data="tableData">
  <template #header>
    <tr>
      <th>Name</th>
    </tr>
  </template>
  <template #body="{ item }">
    <tr>
      <td>{{ item.name }}</td>
    </tr>
  </template>
</table-component>

vue实现table

标签: vuetable
分享给朋友:

相关文章

vue自己实现下拉导航

vue自己实现下拉导航

实现下拉导航的基本思路 在Vue中实现下拉导航,可以通过结合Vue的响应式数据和事件绑定特性来完成。主要思路是利用v-show或v-if控制下拉菜单的显示与隐藏,并通过鼠标事件或点击事件触发状态变化。…

vue 实现tabs

vue 实现tabs

Vue 实现 Tabs 组件的方法 使用动态组件和 v-for 在 Vue 中实现 Tabs 可以通过动态组件和 v-for 指令结合完成。定义一个 tabs 数组,包含每个标签的标题和对应的组件。…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…