当前位置:首页 > VUE

vue实现简单表格

2026-01-15 03:04:16VUE

使用 Vue 实现简单表格

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

基础表格实现

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

<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>

使用组件化表格

创建可复用的表格组件:

<!-- 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 实现表格的核心方法,可以根据实际需求扩展分页、编辑等功能。

vue实现简单表格

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

相关文章

css表格制作表格

css表格制作表格

CSS 表格制作方法 使用 CSS 创建表格可以通过多种方式实现,包括原生 HTML 表格元素结合 CSS 样式,或者使用纯 CSS 布局技术(如 Flexbox 或 Grid)模拟表格效果。以下是常…

怎么制作css表格

怎么制作css表格

使用HTML和CSS创建表格 HTML提供<table>标签用于创建表格结构,CSS用于控制样式。以下是一个基础示例: <table class="styled-table"&g…

实现简单vue

实现简单vue

实现一个简单的 Vue 框架 数据响应式系统 通过 Object.defineProperty 实现数据劫持,监听数据变化: function defineReactive(obj, key, v…

vue实现子表格

vue实现子表格

实现子表格的基本思路 在Vue中实现子表格通常需要结合组件化思想和动态渲染技术。常见的场景是在主表格的行内嵌套子表格,展开主表行时显示对应的子表数据。 使用el-table实现子表格(Elemen…

uniapp 表格导入

uniapp 表格导入

uniapp 表格导入的实现方法 使用 uni.chooseFile 选择文件 在 uniapp 中,可以通过 uni.chooseFile API 让用户选择本地文件。该 API 支持多平台,包括…

vue动态表格实现

vue动态表格实现

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