当前位置:首页 > 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中实现表格可以通过多种方式完成,包括使用原生HTML表格、第三方组件库或自定义组件。以下是几种常见的方法: 使用原生HTML表格 在Vue模板中直接使用HTML的&l…

vue日历表格实现

vue日历表格实现

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

简单实现vue

简单实现vue

实现一个简易 Vue 框架 以下是一个基于 JavaScript 的简易 Vue 实现,核心功能包括数据响应式、模板编译和依赖收集。 数据响应式 通过 Object.defineProperty 实…

js多线程如何简单实现

js多线程如何简单实现

JavaScript 多线程的简单实现方法 JavaScript 本身是单线程的,但可以通过 Web Workers 实现多线程操作。Web Workers 允许在后台运行脚本,独立于主线程执行任务。…

css怎么制作表格

css怎么制作表格

使用HTML和CSS制作表格 在HTML中,表格由<table>标签定义,结合CSS可以灵活控制样式。 基本HTML结构: <table> <tr>…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…