当前位置:首页 > VUE

vue实现表格读取数据

2026-01-07 01:54:31VUE

Vue 实现表格读取数据的方法

使用静态数据

静态数据可以直接在 Vue 组件的 data 中定义,并通过 v-for 指令渲染到表格中。

<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 },
        { id: 3, name: 'Charlie', age: 35 }
      ]
    }
  }
}
</script>

从 API 获取动态数据

通过 axiosfetch 从后端 API 获取数据,并在 createdmounted 钩子中调用。

<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>
import axios from 'axios';

export default {
  data() {
    return {
      tableData: []
    }
  },
  async created() {
    try {
      const response = await axios.get('https://api.example.com/users');
      this.tableData = response.data;
    } catch (error) {
      console.error('Error fetching data:', error);
    }
  }
}
</script>

使用计算属性处理数据

如果需要处理数据后再显示,可以使用计算属性。

<template>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in filteredData" :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 },
        { id: 3, name: 'Charlie', age: 35 }
      ]
    }
  },
  computed: {
    filteredData() {
      return this.tableData.filter(item => item.age > 28);
    }
  }
}
</script>

使用第三方表格组件

如果需要更复杂的表格功能(如排序、分页),可以使用第三方库如 Element UIVuetify

<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" width="180"></el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
  </el-table>
</template>

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

以上方法涵盖了从静态数据到动态 API 数据的表格实现,以及使用计算属性和第三方库的进阶用法。根据需求选择合适的方式即可。

vue实现表格读取数据

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

相关文章

vue表格实现单选框

vue表格实现单选框

实现单选框的基本思路 在Vue中实现表格单选框通常结合<input type="radio">或第三方组件库(如Element UI、Ant Design Vue等)。核心逻辑是通过v-m…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…

Vue数据依赖实现

Vue数据依赖实现

Vue数据依赖实现原理 Vue的数据依赖实现基于响应式系统,核心是通过Object.defineProperty或Proxy拦截数据读写操作,结合发布-订阅模式自动追踪依赖和触发更新。 核心机制…

vue 实现表格单选

vue 实现表格单选

实现表格单选的方法 在Vue中实现表格单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-model绑定选中项 通过v-model绑定一个变量来记录当前选中的行数据。当用户点击某一行…

vue实现数据删除

vue实现数据删除

vue实现数据删除的方法 使用v-for和v-on指令 在Vue中可以通过v-for指令渲染列表数据,结合v-on指令监听删除事件。定义一个方法处理删除逻辑,通常使用splice方法从数组中移除指定项…

vue实现子表格

vue实现子表格

Vue 实现子表格的方法 在 Vue 中实现子表格可以通过多种方式完成,以下是几种常见的方法: 使用嵌套组件 创建一个父表格组件和一个子表格组件,通过 props 将数据传递给子表格组件。父表格的每…