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

添加排序功能

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

vue实现表格组件

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

自定义列渲染

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

vue实现表格组件

<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 的 data 和 methods 定义倒计时逻辑,通过 setInterval 更新剩余时间。 <template> <div>{{…

vue表格实现复选框

vue表格实现复选框

vue表格实现复选框 在Vue中实现表格复选框功能,可以通过以下方法完成: 方法一:使用el-table(Element UI) 安装Element UI后,在表格列中直接添加type="sele…

实现vue组件

实现vue组件

Vue 组件的基本实现 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种方式: 单文件组件 (SFC) 使用 .vue 文件格式…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

Vue 拖拽实现组件嵌套 使用 Vue 实现拖拽组件嵌套功能可以通过原生 HTML5 拖拽 API 或第三方库如 vuedraggable 实现。以下是两种方法的详细说明: 使用 HTML5 拖拽…

css好看的表格制作

css好看的表格制作

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

css表格制作

css表格制作

CSS 表格制作基础 使用 CSS 可以创建美观且响应式的表格。以下是一些基本方法: table 元素用于定义表格,tr 定义行,td 定义单元格,th 定义表头单元格。 <table&…