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

分页功能实现

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

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

vue实现简单表格

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

相关文章

css 制作表格

css 制作表格

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

表格tb制作css

表格tb制作css

使用CSS美化HTML表格 通过CSS可以自定义表格样式,使其更美观和符合设计需求。以下是一些常用方法: 基础表格样式 table { width: 100%; border-coll…

制作css表格

制作css表格

基础CSS表格结构 使用HTML的<table>标签创建表格骨架,搭配CSS控制样式。以下是一个基础示例: <table class="styled-table"> &l…

vue 实现简单登陆

vue 实现简单登陆

Vue 实现简单登录功能 创建 Vue 项目 使用 Vue CLI 创建一个新项目: vue create login-demo 选择默认配置或手动配置(推荐包含 Vue Router 和 Vuex…

h5实现表格样式

h5实现表格样式

实现H5表格样式的方法 使用HTML和CSS创建基础表格 在HTML中,表格通过<table>标签定义,配合<tr>(行)、<td>(单元格)和<th&g…

vue实现表格导入

vue实现表格导入

Vue 实现表格导入的方法 使用 Element UI 的 Upload 组件 Element UI 提供了 el-upload 组件,可以方便地实现文件上传功能。结合 xlsx 库解析 Excel…