当前位置:首页 > VUE

vue实现简单表格

2026-02-11 06:04:29VUE

Vue 实现简单表格的方法

使用原生 HTML 表格结构

在 Vue 模板中直接使用 HTML 的 <table> 标签结构,结合 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>

使用第三方组件库(如 Element UI)

通过 Element UI 的 el-table 组件快速实现功能丰富的表格。

<template>
  <el-table :data="tableData">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
    <el-table-column prop="city" label="城市"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 25, city: '北京' },
        { name: '李四', age: 30, city: '上海' }
      ]
    }
  }
}
</script>

动态绑定样式

通过 Vue 的 class 和 style 绑定实现表格行的条件样式。

<template>
  <table>
    <tr v-for="(item, index) in items" :key="index" 
        :class="{ 'highlight': item.highlight }">
      <td>{{ item.name }}</td>
      <td>{{ item.value }}</td>
    </tr>
  </table>
</template>

<style>
.highlight {
  background-color: yellow;
}
</style>

添加交互功能

结合 Vue 的事件处理实现表格行点击等交互。

<template>
  <table>
    <tr v-for="(item, index) in items" :key="index" 
        @click="handleClick(item)">
      <td>{{ item.name }}</td>
      <td>{{ item.value }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  methods: {
    handleClick(item) {
      console.log('Clicked:', item);
    }
  }
}
</script>

分页功能实现

通过计算属性实现表格数据的分页显示。

vue实现简单表格

<template>
  <table>
    <tr v-for="(item, index) in paginatedItems" :key="index">
      <td>{{ item.name }}</td>
      <td>{{ item.value }}</td>
    </tr>
  </table>
  <button @click="prevPage">上一页</button>
  <button @click="nextPage">下一页</button>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5
    }
  },
  computed: {
    paginatedItems() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.items.slice(start, start + this.pageSize);
    }
  },
  methods: {
    prevPage() { if (this.currentPage > 1) this.currentPage--; },
    nextPage() { this.currentPage++; }
  }
}
</script>

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

相关文章

css好看的表格制作

css好看的表格制作

CSS表格美化方法 边框与间距优化 使用border-collapse合并边框,避免双线边框。设置padding增加单元格内边距,提升可读性。 table { border-collapse:…

css怎么制作表格

css怎么制作表格

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

css3制作简单动画

css3制作简单动画

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

vue 实现表格单选

vue 实现表格单选

实现表格单选的基本思路 在Vue中实现表格单选功能,通常需要结合表格组件(如el-table)和数据绑定。核心逻辑是通过点击事件更新当前选中行的唯一标识(如id),并通过:class或:style绑定…

vue实现表格输入

vue实现表格输入

Vue实现表格输入的方法 基础表格实现 使用v-for指令动态渲染表格行和列,结合v-model实现双向数据绑定。 <template> <table> <…

vue实现的表格

vue实现的表格

Vue 实现表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 的 <table> 标签创建表格。数据可以通过 v-for 指令动态渲染。 <templ…