当前位置:首页 > VUE

vue实现table

2026-02-10 11:16:09VUE

Vue 实现 Table 的基本方法

使用原生 HTML 表格

通过 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 in data" :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 {
      columns: [
        { key: 'name', title: '姓名' },
        { key: 'age', title: '年龄' }
      ],
      data: [
        { id: 1, name: '张三', age: 25 },
        { id: 2, name: '李四', age: 30 }
      ]
    }
  }
}
</script>

使用第三方组件库

推荐使用成熟的 UI 库如 Element UI、Ant Design Vue 等,提供丰富的表格功能(排序、分页、筛选等)。

vue实现table

Element UI 示例:

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

<script>
export default {
  data() {
    return {
      tableData: [
        { date: '2023-01-01', name: '王五' },
        { date: '2023-01-02', name: '赵六' }
      ]
    }
  }
}
</script>

高级功能实现

动态列与自定义渲染

通过 scoped slots 实现复杂单元格内容渲染。

vue实现table

<template>
  <table>
    <tr v-for="row in data" :key="row.id">
      <td v-for="col in columns" :key="col.key">
        <slot :name="`cell-${col.key}`" :row="row">
          {{ row[col.key] }}
        </slot>
      </td>
    </tr>
  </table>
</template>

分页控制

结合分页组件实现数据分段加载。

<template>
  <div>
    <table>...</table>
    <el-pagination 
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}
</script>

性能优化建议

对于大数据量场景,建议使用虚拟滚动技术(如 vue-virtual-scroller)避免 DOM 节点过多导致的性能问题。

<template>
  <RecycleScroller
    :items="data"
    :item-size="50"
    key-field="id">
    <template v-slot="{ item }">
      <tr>
        <td>{{ item.name }}</td>
      </tr>
    </template>
  </RecycleScroller>
</template>

标签: vuetable
分享给朋友:

相关文章

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…

vue实现展开

vue实现展开

展开功能的实现方法 在Vue中实现展开功能可以通过多种方式完成,常见的有使用v-show、v-if指令,结合CSS过渡动画或第三方组件库。 使用v-show控制显示隐藏 通过v-show指令可以简单…

vue watch实现

vue watch实现

在 Vue 中实现 watch 监听数据变化 Vue 的 watch 选项用于监听数据变化并执行相应的回调函数。以下是几种常见的实现方式: 基础用法 export default { data(…

vue远离实现

vue远离实现

Vue 原理实现 Vue.js 的核心原理包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是其关键实现原理的详细解析。 响应式系统 Vue 的响应式系统基于 Object.definePro…