当前位置:首页 > VUE

vue实现表格读取数据

2026-02-25 18:48:20VUE

使用 Vue 实现表格数据读取

基础表格数据绑定

通过 v-for 指令遍历数组数据,动态生成表格行。数据通常存储在组件的 data 属性中。

<template>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in tableData" :key="item.id">
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { id: 1, name: 'Alice', age: 25 },
        { id: 2, name: 'Bob', age: 30 }
      ]
    }
  }
}
</script>

从 API 异步加载数据

使用 axiosfetch 从后端接口获取数据,更新表格内容。

<script>
import axios from 'axios';

export default {
  data() {
    return {
      tableData: []
    }
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      axios.get('/api/users')
        .then(response => {
          this.tableData = response.data;
        })
        .catch(error => {
          console.error('Error fetching data:', error);
        });
    }
  }
}
</script>

使用计算属性处理数据

对表格数据进行过滤或排序时,推荐使用计算属性避免直接修改原始数据。

computed: {
  sortedData() {
    return [...this.tableData].sort((a, b) => a.age - b.age);
  }
}

添加分页功能

通过 v-model 绑定分页参数,切片显示数据。

<template>
  <div>
    <table>...</table>
    <div class="pagination">
      <button @click="currentPage--" :disabled="currentPage === 1">Previous</button>
      <span>Page {{ currentPage }} of {{ totalPages }}</span>
      <button @click="currentPage++" :disabled="currentPage === totalPages">Next</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      allData: [] 
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.allData.slice(start, start + this.pageSize);
    },
    totalPages() {
      return Math.ceil(this.allData.length / this.pageSize);
    }
  }
}
</script>

使用第三方表格组件

对于复杂需求,可集成现成组件库如 Element UIVuetify

vue实现表格读取数据

<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="ID" width="180"></el-table-column>
    <el-table-column prop="name" label="Name"></el-table-column>
  </el-table>
</template>

标签: 表格数据
分享给朋友:

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDat…

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格数…

vue日历表格实现

vue日历表格实现

实现Vue日历表格的基本步骤 使用Vue实现日历表格需要处理日期数据生成、渲染和交互逻辑。以下是核心实现方法: 安装依赖(可选) 若需复杂功能可引入date-fns或dayjs等日期库: npm…

vue表格实现复选框

vue表格实现复选框

Vue表格实现复选框的方法 基础实现(使用v-model绑定数组) 在Vue中可以通过v-model绑定数组来实现复选框功能。以下是一个基础示例: <template> <ta…

vue实现导出表格

vue实现导出表格

Vue 实现导出表格的方法 使用 xlsx 库导出 Excel 安装 xlsx 库 npm install xlsx 在 Vue 组件中引入 xlsx import * as XLSX from…

vue动态表格实现

vue动态表格实现

Vue动态表格实现方法 使用v-for指令渲染动态数据 通过v-for指令可以循环遍历数组数据生成表格行,数据变化时表格自动更新。示例代码: <template> <table…