当前位置:首页 > VUE

vue实现简单表格

2026-01-15 03:04:16VUE

使用 Vue 实现简单表格

Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法:

基础表格实现

通过 v-for 指令循环渲染表格数据:

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="(value, key) in row" :key="key">
          {{ value }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

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

使用组件化表格

创建可复用的表格组件:

vue实现简单表格

<!-- TableComponent.vue -->
<template>
  <table>
    <slot name="header"></slot>
    <slot name="body"></slot>
  </table>
</template>

<!-- 使用组件 -->
<template>
  <TableComponent>
    <template #header>
      <thead>
        <tr>
          <th v-for="header in headers" :key="header">
            {{ header }}
          </th>
        </tr>
      </thead>
    </template>
    <template #body>
      <tbody>
        <tr v-for="(row, index) in rows" :key="index">
          <td v-for="(value, key) in row" :key="key">
            {{ value }}
          </td>
        </tr>
      </tbody>
    </template>
  </TableComponent>
</template>

添加交互功能

实现排序和筛选功能示例:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" 
            :key="header.key"
            @click="sortBy(header.key)">
          {{ header.text }}
          <span v-if="sortKey === header.key">
            {{ sortOrder > 0 ? '↑' : '↓' }}
          </span>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="row in sortedRows" :key="row.id">
        <td v-for="header in headers" :key="header.key">
          {{ row[header.key] }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1,
      headers: [
        { text: '姓名', key: 'name' },
        { text: '年龄', key: 'age' }
      ],
      rows: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ]
    }
  },
  computed: {
    sortedRows() {
      return [...this.rows].sort((a, b) => {
        return a[this.sortKey] > b[this.sortKey] ? this.sortOrder : -this.sortOrder
      })
    }
  },
  methods: {
    sortBy(key) {
      this.sortOrder = this.sortKey === key ? -this.sortOrder : 1
      this.sortKey = key
    }
  }
}
</script>

样式优化

为表格添加基础样式:

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f2f2f2;
  cursor: pointer;
}
tr:nth-child(even) {
  background-color: #f9f9f9;
}

以上示例展示了 Vue 实现表格的核心方法,可以根据实际需求扩展分页、编辑等功能。

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

相关文章

网页表格制作css代码

网页表格制作css代码

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

vue实现表格读取数据

vue实现表格读取数据

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

css表格制作表格

css表格制作表格

CSS 表格制作方法 基础表格结构 使用HTML的<table>标签创建表格框架,搭配<tr>(行)、<td>(单元格)和<th>(表头)标签: &l…

vue 实现跨行表格

vue 实现跨行表格

实现跨行表格的方法 在Vue中实现跨行表格可以通过多种方式完成,以下是几种常见的方法: 使用HTML原生表格 HTML原生表格支持rowspan属性,可以直接在Vue模板中使用: <tabl…

vue动态表格实现

vue动态表格实现

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

网页表格制作css代码

网页表格制作css代码

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