当前位置:首页 > VUE

用vue实现表格

2026-03-29 00:03:49VUE

使用 Vue 实现表格

基础表格实现

在 Vue 中可以通过 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>

添加交互功能

可以通过 Vue 的事件绑定为表格添加排序、筛选等功能:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="header in headers" :key="header" @click="sortBy(header)">
          {{ header }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in sortedRows" :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, 城市: '上海' }
      ],
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedRows() {
      if (!this.sortKey) return this.rows
      return [...this.rows].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="rows">
     <el-table-column prop="姓名" label="姓名"></el-table-column>
     <el-table-column prop="年龄" label="年龄"></el-table-column>
     <el-table-column prop="城市" label="城市"></el-table-column>
    </el-table>
    </template>
  2. Vuetify:

    <template>
    <v-data-table :headers="headers" :items="rows"></v-data-table>
    </template>
  3. Ant Design Vue:

    <template>
    <a-table :columns="headers" :data-source="rows"></a-table>
    </template>

分页功能实现

可以通过计算属性实现客户端分页:

<template>
  <table>
    <!-- 表格内容 -->
  </table>
  <div>
    <button @click="currentPage--" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="currentPage++" :disabled="currentPage === totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      // 其他数据
    }
  },
  computed: {
    paginatedRows() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.sortedRows.slice(start, start + this.pageSize)
    },
    totalPages() {
      return Math.ceil(this.rows.length / this.pageSize)
    }
  }
}
</script>

响应式设计

可以通过 CSS 使表格在不同屏幕尺寸下保持良好显示:

用vue实现表格

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 8px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}
@media screen and (max-width: 600px) {
  table {
    display: block;
    overflow-x: auto;
  }
}

标签: 表格vue
分享给朋友:

相关文章

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…