当前位置:首页 > VUE

实现vue table

2026-02-10 06:10:14VUE

使用 Vue 实现表格组件

Vue 提供了多种方式实现表格功能,以下介绍几种常见方法,涵盖基础表格、动态渲染、分页和排序等功能。

基础表格实现

通过 v-for 循环渲染表格数据,适用于静态或少量数据展示。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header">{{ header }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in data" :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'],
      data: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ]
    };
  }
};
</script>

动态数据加载

结合 API 请求动态加载表格数据,通常使用 axiosfetch

实现vue table

<template>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="user in users" :key="user.id">
        <td>{{ user.id }}</td>
        <td>{{ user.name }}</td>
        <td>{{ user.email }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      users: []
    };
  },
  async created() {
    const response = await axios.get('https://jsonplaceholder.typicode.com/users');
    this.users = response.data;
  }
};
</script>

分页功能

通过计算属性实现分页逻辑,控制每页显示的数据量。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>Name</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in paginatedData" :key="item.id">
          <td>{{ item.id }}</td>
          <td>{{ item.name }}</td>
        </tr>
      </tbody>
    </table>
    <button @click="prevPage" :disabled="currentPage === 1">Previous</button>
    <span>Page {{ currentPage }} of {{ totalPages }}</span>
    <button @click="nextPage" :disabled="currentPage === totalPages">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [
        { id: 1, name: 'Alice' },
        { id: 2, name: 'Bob' },
        { id: 3, name: 'Charlie' },
        { id: 4, name: 'David' }
      ],
      currentPage: 1,
      itemsPerPage: 2
    };
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.data.slice(start, end);
    },
    totalPages() {
      return Math.ceil(this.data.length / this.itemsPerPage);
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    }
  }
};
</script>

排序功能

通过点击表头实现升序或降序排列。

实现vue table

<template>
  <table>
    <thead>
      <tr>
        <th @click="sort('id')">ID</th>
        <th @click="sort('name')">Name</th>
        <th @click="sort('age')">Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedData" :key="row.id">
        <td>{{ row.id }}</td>
        <td>{{ row.name }}</td>
        <td>{{ row.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      data: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 },
        { id: 3, name: 'Charlie', age: 20 }
      ],
      sortKey: 'id',
      sortOrder: 1 // 1 for ascending, -1 for descending
    };
  },
  computed: {
    sortedData() {
      return [...this.data].sort((a, b) => {
        if (a[this.sortKey] < b[this.sortKey]) return -1 * this.sortOrder;
        if (a[this.sortKey] > b[this.sortKey]) return 1 * this.sortOrder;
        return 0;
      });
    }
  },
  methods: {
    sort(key) {
      if (this.sortKey === key) {
        this.sortOrder *= -1;
      } else {
        this.sortKey = key;
        this.sortOrder = 1;
      }
    }
  }
};
</script>

使用第三方库

对于复杂表格需求,可以使用成熟的第三方库如 Element UIVuetifyag-Grid

Element UI 为例:

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="date" label="Date" width="180"></el-table-column>
    <el-table-column prop="name" label="Name" width="180"></el-table-column>
    <el-table-column prop="address" label="Address"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: 'Alice', address: 'New York' },
        { date: '2023-01-02', name: 'Bob', address: 'London' }
      ]
    };
  }
};
</script>

总结

  • 基础表格使用 v-for 循环渲染数据。
  • 动态数据通过 API 请求加载。
  • 分页通过计算属性和按钮控制实现。
  • 排序通过点击表头切换升序或降序。
  • 复杂场景推荐使用 Element UIag-Grid 等库。

以上方法可根据需求灵活组合或扩展。

标签: vuetable
分享给朋友:

相关文章

vue项目实现

vue项目实现

Vue 项目实现指南 环境准备 确保已安装 Node.js(建议版本 14+)和 npm/yarn。通过以下命令检查版本: node -v npm -v 安装 Vue CLI(脚手架工具): np…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout() {…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue实现RTMP

vue实现RTMP

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

vue首页实现

vue首页实现

实现Vue首页的基本步骤 创建一个Vue首页通常涉及项目初始化、页面结构设计、路由配置和组件开发。以下是具体实现方法: 初始化Vue项目 使用Vue CLI或Vite快速搭建项目结构: npm i…

mui实现vue

mui实现vue

mui 实现 Vue 的步骤 mui 是一个轻量级的前端框架,常用于移动端开发。结合 Vue 可以实现高效的开发体验。以下是具体实现方法。 安装 mui 和 Vue 通过 npm 或 yarn 安装…