当前位置:首页 > 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-model 指令绑定数据,并通过 type="password" 设置输入类型为密码: <template>…

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { -…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容切…

vue实现查询替换

vue实现查询替换

Vue 实现查询替换功能 在 Vue 中实现查询替换功能,可以通过数据绑定和字符串操作方法结合实现。以下是具体实现方式: 基础实现 <template> <div>…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…