当前位置:首页 > VUE

vue实现表格组件

2026-01-18 01:38:20VUE

Vue 表格组件实现方法

基础表格实现

使用 Vue 的模板语法可以快速实现基础表格功能。通过 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>

添加排序功能

为表格添加点击表头排序的功能,通过计算属性处理排序逻辑。

<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>
    <!-- 表格内容同上 -->
  </table>
</template>

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

分页功能实现

结合分页组件实现表格数据的分页展示。

<template>
  <div>
    <!-- 表格组件 -->
    <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: {
    totalPages() {
      return Math.ceil(this.data.length / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      return this.sortedData.slice(start, start + this.pageSize)
    }
  },
  methods: {
    prevPage() {
      this.currentPage = Math.max(1, this.currentPage - 1)
    },
    nextPage() {
      this.currentPage = Math.min(this.totalPages, this.currentPage + 1)
    }
  }
}
</script>

自定义列渲染

支持通过插槽自定义列内容渲染方式。

<template>
  <table>
    <thead><!-- 表头同上 --></thead>
    <tbody>
      <tr v-for="(row, index) in data" :key="index">
        <td v-for="col in columns" :key="col.key">
          <slot v-if="$scopedSlots[col.key]" :name="col.key" :row="row"></slot>
          <span v-else>{{ row[col.key] }}</span>
        </td>
      </tr>
    </tbody>
  </table>
</template>

使用方式:

<data-table :columns="columns" :data="data">
  <template v-slot:action="{ row }">
    <button @click="handleAction(row)">操作</button>
  </template>
</data-table>

性能优化

对于大数据量的表格,可以使用虚拟滚动技术优化性能。

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

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

vue实现表格组件

标签: 组件表格
分享给朋友:

相关文章

vue哪个组件实现动画

vue哪个组件实现动画

在Vue中实现动画效果,可以通过以下组件和方法实现: Vue内置动画组件 Vue提供了<transition>和<transition-group>两个内置组件,用于处理进…

vue实现表格多行修改

vue实现表格多行修改

实现表格多行修改的方法 数据绑定与表格渲染 使用v-model绑定表格数据到Vue实例的data属性。通过v-for循环渲染表格行,并为每行数据添加编辑状态标识。 <template>…

vue实现穿梭框树组件

vue实现穿梭框树组件

Vue 穿梭框树组件实现 穿梭框树组件通常结合了树形结构和穿梭框功能,允许用户在左右两栏之间移动树节点数据。以下是基于 Vue 的实现方案: 核心功能设计 数据结构 树形数据通常采用嵌套结构,例如:…

Vue组件实现方法

Vue组件实现方法

Vue组件的基本实现 Vue组件是Vue.js的核心概念之一,允许将UI拆分为独立可复用的代码片段。组件的实现方式有多种,以下是常见的几种方法。 单文件组件(SFC) 单文件组件以.vue为后缀,…

vue实现表格导入

vue实现表格导入

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

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…