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

自定义表格组件

创建可复用的表格组件:

vue实现table

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

标签: vuetable
分享给朋友:

相关文章

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

实现vue cli

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现api

vue实现api

Vue 实现 API 调用 在 Vue 中调用 API 通常涉及使用 axios 或 fetch 发送 HTTP 请求,并结合 Vue 的响应式特性管理数据。以下是常见的实现方式: 安装 axios…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…