当前位置:首页 > 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库。

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

相关文章

vue 简单实现

vue 简单实现

Vue 简单实现 Vue 是一个渐进式 JavaScript 框架,适合快速构建用户界面。以下是实现一个简单 Vue 应用的步骤: 安装 Vue 通过 CDN 引入 Vue 是最简单的方式: &…

vue实现表格追加

vue实现表格追加

实现表格数据追加的方法 在Vue中实现表格数据追加可以通过多种方式完成,主要涉及数据绑定和数组操作。以下是几种常见的方法: 数据绑定与v-for指令 通过v-for指令渲染表格数据,利用数组的co…

用vue实现表格

用vue实现表格

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以轻松渲染表格数据。准备一个数据数组,在模板中用 v-for 循环渲染。 <template> <tab…

vue实现表格树

vue实现表格树

Vue 实现表格树的方法 使用递归组件实现树形表格 在 Vue 中可以通过递归组件实现树形表格结构。定义一个组件,该组件可以调用自身来渲染子节点。 <template> <ta…

表格制作css

表格制作css

CSS表格样式设计 基础表格样式 通过border-collapse合并边框,使表格更整洁。width控制整体宽度,text-align设置文字对齐方式。 table { border-coll…

vue实现多级表格

vue实现多级表格

Vue 实现多级表格的方法 使用递归组件实现多级表格 递归组件是处理嵌套数据的理想方式,特别适合展示树形结构的数据。 <template> <table> <…