当前位置:首页 > VUE

纯vue实现表格

2026-01-18 13:10:16VUE

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="(cell, cellIndex) in row" :key="cellIndex">{{ cell }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      headers: ['姓名', '年龄', '职业'],
      rows: [
        ['张三', 25, '工程师'],
        ['李四', 30, '设计师']
      ]
    }
  }
}
</script>

动态数据绑定

表格数据通常来自 API 或动态计算:

纯vue实现表格

<template>
  <table>
    <tbody>
      <tr v-for="item in apiData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.value }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      apiData: []
    }
  },
  async created() {
    const response = await fetch('https://api.example.com/data')
    this.apiData = await response.json()
  }
}
</script>

表格功能扩展

实现排序、筛选等交互功能:

纯vue实现表格

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索...">
    <table>
      <thead>
        <tr>
          <th @click="sortBy('name')">姓名</th>
          <th @click="sortBy('age')">年龄</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in filteredData" :key="item.id">
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      sortKey: '',
      sortOrder: 1,
      items: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ]
    }
  },
  computed: {
    filteredData() {
      let data = this.items
      if (this.searchQuery) {
        data = data.filter(item => 
          item.name.includes(this.searchQuery)
        )
      }
      if (this.sortKey) {
        data = [...data].sort((a, b) => {
          return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * this.sortOrder
        })
      }
      return data
    }
  },
  methods: {
    sortBy(key) {
      this.sortKey = key
      this.sortOrder *= -1
    }
  }
}
</script>

组件化表格

将表格封装为可复用组件:

<!-- TableComponent.vue -->
<template>
  <table>
    <slot name="header"></slot>
    <tbody>
      <tr v-for="(item, index) in data" :key="index">
        <slot :row="item"></slot>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  props: {
    data: {
      type: Array,
      required: true
    }
  }
}
</script>

<!-- 使用组件 -->
<template>
  <TableComponent :data="tableData">
    <template #header>
      <thead>
        <tr>
          <th>姓名</th>
          <th>年龄</th>
        </tr>
      </thead>
    </template>
    <template #default="{ row }">
      <td>{{ row.name }}</td>
      <td>{{ row.age }}</td>
    </template>
  </TableComponent>
</template>

性能优化

对于大型数据集,使用虚拟滚动:

<template>
  <div class="table-container" @scroll="handleScroll">
    <div class="table-content" :style="{ height: totalHeight + 'px' }">
      <div 
        v-for="item in visibleData" 
        :key="item.id" 
        class="table-row"
        :style="{ transform: `translateY(${item.offset}px)` }"
      >
        <div class="table-cell">{{ item.name }}</div>
        <div class="table-cell">{{ item.value }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allData: [], // 所有数据
      visibleData: [], // 可见数据
      itemHeight: 50,
      viewportHeight: 500,
      scrollTop: 0
    }
  },
  computed: {
    totalHeight() {
      return this.allData.length * this.itemHeight
    }
  },
  methods: {
    updateVisibleData() {
      const start = Math.floor(this.scrollTop / this.itemHeight)
      const end = start + Math.ceil(this.viewportHeight / this.itemHeight)

      this.visibleData = this.allData
        .slice(start, end)
        .map((item, index) => ({
          ...item,
          offset: (start + index) * this.itemHeight
        }))
    },
    handleScroll(e) {
      this.scrollTop = e.target.scrollTop
      this.updateVisibleData()
    }
  }
}
</script>

以上实现方式覆盖了 Vue 表格的基础到高级应用场景,可根据实际需求选择适合的方案。

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

相关文章

vue实现点击页面切换

vue实现点击页面切换

实现点击页面切换功能 在Vue中实现点击页面切换功能,可以通过路由跳转或组件动态渲染两种方式完成。以下是具体实现方法: 使用Vue Router实现页面跳转 安装Vue Router后,在项目中配置…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现流程

vue实现流程

Vue 实现流程的基本步骤 安装 Vue.js 可以通过 CDN 引入或使用 npm/yarn 安装。 CDN 方式: <script src="https://cdn.jsdelivr.…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现分栏

vue实现分栏

Vue实现分栏的常见方法 使用CSS Flexbox布局 通过Flexbox可以快速实现分栏效果,适合大多数响应式布局场景 <template> <div class="con…