当前位置:首页 > VUE

vue 实现简单表格

2026-01-18 06:09:07VUE

以下是在 Vue 中实现简单表格的几种方法:

使用原生表格标签

通过 Vue 的数据绑定功能渲染表格数据:

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

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      rows: [
        ['张三', 25, '工程师'],
        ['李四', 30, '设计师']
      ]
    }
  }
}
</script>

使用组件化方式

将表格拆分为可复用的组件:

vue 实现简单表格

<template>
  <my-table :headers="headers" :rows="rows"></my-table>
</template>

<script>
import MyTable from './MyTable.vue'

export default {
  components: { MyTable },
  data() {
    return {
      headers: ['姓名', '年龄'],
      rows: [
        { name: '王五', age: 28 },
        { name: '赵六', age: 35 }
      ]
    }
  }
}
</script>

添加交互功能

实现排序和筛选等常见表格功能:

<template>
  <table>
    <thead>
      <tr>
        <th @click="sortBy('name')">姓名</th>
        <th @click="sortBy('age')">年龄</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in sortedItems" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ],
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedItems() {
      return [...this.items].sort((a, b) => {
        return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * this.sortOrder
      })
    }
  },
  methods: {
    sortBy(key) {
      this.sortOrder = this.sortKey === key ? -this.sortOrder : 1
      this.sortKey = key
    }
  }
}
</script>

使用第三方库

对于更复杂的需求,可以考虑使用专门的表格组件库:

vue 实现简单表格

  1. Element UI 的表格组件:

    <template>
    <el-table :data="tableData">
     <el-table-column prop="date" label="日期"></el-table-column>
     <el-table-column prop="name" label="姓名"></el-table-column>
    </el-table>
    </template>
  2. Vuetify 的数据表格:

    <template>
    <v-data-table :headers="headers" :items="items"></v-data-table>
    </template>

这些方法涵盖了从基础到进阶的表格实现方式,可以根据项目需求选择合适的方案。对于简单表格,原生实现足够;对于复杂需求,推荐使用成熟的UI库。

标签: 表格简单
分享给朋友:

相关文章

css怎么制作表格

css怎么制作表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,结合CSS可以自定义样式。以下是基本实现方法: HTML结构 <table> <th…

网页表格制作css代码

网页表格制作css代码

基础表格样式 使用CSS为HTML表格添加基础样式,如边框、间距和背景色: table { width: 100%; border-collapse: collapse; margin…

css3制作简单动画

css3制作简单动画

CSS3 制作简单动画的方法 CSS3 提供了多种方式制作动画,主要通过 transition、transform 和 animation 属性实现。以下是几种常见的实现方法。 使用 transi…

vue实现表格读取数据

vue实现表格读取数据

Vue 实现表格数据读取 在 Vue 中实现表格数据读取通常涉及以下关键步骤: 数据绑定与表格渲染 通过 v-for 指令动态渲染表格行,数据通常存储在组件的 data 或 Vuex 状态中:…

如何实现vue表格联动

如何实现vue表格联动

实现 Vue 表格联动的方法 数据绑定与响应式更新 通过 Vue 的响应式特性,将多个表格的数据绑定到同一个数据源或计算属性。当一个表格的数据发生变化时,另一个表格会自动更新。例如: data()…

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm inst…