当前位置:首页 > VUE

vue 实现table

2026-03-07 15:15:26VUE

Vue 实现 Table 的基本方法

使用原生 HTML 表格

Vue 可以直接结合原生 HTML 的 <table> 标签实现表格功能,通过 v-for 动态渲染数据。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in data" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['Name', 'Age', 'Email'],
      data: [
        { Name: 'Alice', Age: 25, Email: 'alice@example.com' },
        { Name: 'Bob', Age: 30, Email: 'bob@example.com' }
      ]
    };
  }
};
</script>

使用第三方组件库

常见的 Vue UI 库(如 Element UI、Ant Design Vue、Vuetify)提供了功能丰富的 Table 组件。

vue 实现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: 'Alice', age: 25, email: 'alice@example.com' },
        { name: 'Bob', age: 30, email: 'bob@example.com' }
      ]
    };
  }
};
</script>

自定义可复用 Table 组件

通过封装一个可复用的 Table 组件,支持动态列配置、排序、分页等功能。

vue 实现table

<!-- TableComponent.vue -->
<template>
  <table>
    <thead>
      <tr>
        <th 
          v-for="column in columns" 
          :key="column.key"
          @click="sortTable(column.key)"
        >
          {{ column.label }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedData" :key="row.id">
        <td v-for="column in columns" :key="column.key">
          {{ row[column.key] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    data: Array,
    columns: Array
  },
  data() {
    return {
      sortKey: '',
      sortOrder: 'asc'
    };
  },
  computed: {
    sortedData() {
      if (!this.sortKey) return this.data;
      const order = this.sortOrder === 'asc' ? 1 : -1;
      return [...this.data].sort((a, b) => {
        return a[this.sortKey] > b[this.sortKey] ? order : -order;
      });
    }
  },
  methods: {
    sortTable(key) {
      if (this.sortKey === key) {
        this.sortOrder = this.sortOrder === 'asc' ? 'desc' : 'asc';
      } else {
        this.sortKey = key;
        this.sortOrder = 'asc';
      }
    }
  }
};
</script>

高级功能实现

表格常需支持分页、筛选、编辑等交互功能。

分页实现
<template>
  <div>
    <table>...</table>
    <div class="pagination">
      <button 
        v-for="page in totalPages" 
        :key="page"
        @click="currentPage = page"
      >
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.data.slice(start, start + this.pageSize);
    },
    totalPages() {
      return Math.ceil(this.data.length / this.pageSize);
    }
  }
};
</script>
单元格编辑功能
<template>
  <table>
    <tr v-for="row in data" :key="row.id">
      <td v-for="col in columns" :key="col.key">
        <span v-if="!row.editing">{{ row[col.key] }}</span>
        <input 
          v-else 
          v-model="row[col.key]" 
          @blur="row.editing = false"
        >
        <button @click="row.editing = !row.editing">Edit</button>
      </td>
    </tr>
  </table>
</template>

性能优化建议

对于大数据量的表格,可采用虚拟滚动技术(如 vue-virtual-scroller)避免渲染全部行。

<template>
  <RecycleScroller
    :items="data"
    :item-size="50"
    key-field="id"
  >
    <template v-slot="{ item }">
      <tr>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </template>
  </RecycleScroller>
</template>

标签: vuetable
分享给朋友:

相关文章

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

javascript实现vue

javascript实现vue

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

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…

vue实现按钮

vue实现按钮

Vue 实现按钮的方法 使用原生 HTML 按钮 在 Vue 模板中可以直接使用 HTML 的 <button> 元素,通过 v-on 或 @ 绑定点击事件。 <template&…