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

性能优化

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

vue实现表格组件

<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 CLI 或 Vite 初始化项目: npm init vue@latest student-table cd student-table npm install…

表格制作css

表格制作css

基础表格样式 使用CSS可以轻松地为HTML表格添加样式。以下是一个基础表格的HTML和CSS示例: <table class="basic-table"> <thead>…

vue实现组件

vue实现组件

Vue 实现组件的方法 Vue 中实现组件可以通过多种方式,包括全局注册、局部注册、单文件组件(SFC)等。以下是常见的实现方法。 全局注册组件 全局注册的组件可以在任何 Vue 实例或组件中使用。…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue实现广告组件

vue实现广告组件

vue实现广告组件的方法 使用动态组件实现 在Vue中可以通过动态组件的方式加载广告组件,根据不同的条件展示不同的广告内容。动态组件使用<component :is="currentCompon…

css表格制作

css表格制作

CSS表格制作方法 使用CSS创建表格可以通过多种方式实现,以下是常见的几种方法: 使用HTML原生表格标签结合CSS样式 <table class="styled-table">…