当前位置:首页 > VUE

vue table组件实现

2026-01-15 04:03:51VUE

Vue 表格组件实现方案

基础表格实现

使用<table>标签结合v-for指令渲染数据是最简单的实现方式:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="col in columns" :key="col.key">{{ col.title }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td v-for="col in columns" :key="col.key">{{ row[col.key] }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    columns: Array,
    data: Array
  }
}
</script>

使用第三方组件库

Element UI的表格组件提供丰富功能:

vue table组件实现

<template>
  <el-table :data="tableData">
    <el-table-column prop="date" label="日期"></el-table-column>
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="address" label="地址"></el-table-column>
  </el-table>
</template>

Ant Design Vue表格组件实现:

<template>
  <a-table :columns="columns" :data-source="data">
    <template #action="{ record }">
      <a @click="handleEdit(record)">编辑</a>
    </template>
  </a-table>
</template>

自定义高级功能

实现可排序表格:

vue table组件实现

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

<script>
export default {
  data() {
    return {
      sortKey: '',
      sortOrder: 1
    }
  },
  computed: {
    sortedData() {
      if (!this.sortKey) return this.data
      return [...this.data].sort((a, b) => {
        return a[this.sortKey] > b[this.sortKey] ? this.sortOrder : -this.sortOrder
      })
    }
  },
  methods: {
    sortBy(key) {
      this.sortKey = key
      this.sortOrder = -this.sortOrder
    }
  }
}
</script>

虚拟滚动优化

处理大数据量时使用虚拟滚动:

<template>
  <div class="virtual-table" @scroll="handleScroll">
    <div class="table-header">
      <!-- 表头内容 -->
    </div>
    <div class="table-body" :style="{ height: totalHeight + 'px' }">
      <div v-for="item in visibleData" 
           :key="item.id"
           :style="{ transform: `translateY(${item.offset}px)` }">
        <!-- 行内容 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rowHeight: 50,
      visibleCount: 20,
      startIndex: 0
    }
  },
  computed: {
    visibleData() {
      return this.data.slice(
        this.startIndex,
        this.startIndex + this.visibleCount
      ).map((item, index) => ({
        ...item,
        offset: (this.startIndex + index) * this.rowHeight
      }))
    },
    totalHeight() {
      return this.data.length * this.rowHeight
    }
  },
  methods: {
    handleScroll(e) {
      const scrollTop = e.target.scrollTop
      this.startIndex = Math.floor(scrollTop / this.rowHeight)
    }
  }
}
</script>

表格分页实现

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

<script>
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.data.slice(start, start + this.pageSize)
    },
    totalPages() {
      return Math.ceil(this.data.length / this.pageSize)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.totalPages) this.currentPage++
    }
  }
}
</script>

响应式表格设计

使用CSS媒体查询实现响应式:

@media screen and (max-width: 600px) {
  table {
    border: 0;
  }
  table thead {
    display: none;
  }
  table tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid #ddd;
  }
  table td {
    display: block;
    text-align: right;
    border-bottom: 1px solid #eee;
  }
  table td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
  }
}

标签: 组件vue
分享给朋友:

相关文章

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressorj…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现发帖

vue实现发帖

使用 Vue 实现发帖功能 前端实现 创建发帖表单组件,包含标题和内容输入框,以及提交按钮。使用 v-model 绑定表单数据。 <template> <div>…

vue底部实现

vue底部实现

Vue 底部实现方法 在 Vue 项目中实现底部布局可以通过多种方式完成,以下是一些常见的方法: 使用固定定位 将底部元素固定在页面底部,适用于单页应用或需要始终显示的底部栏。 <temp…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…