当前位置:首页 > VUE

vue 实现表格渲染

2026-01-16 20:22:26VUE

基本表格渲染

在 Vue 中渲染表格通常使用 v-for 指令遍历数据数组,动态生成表格行。以下是一个简单的示例:

<template>
  <table>
    <thead>
      <tr>
        <th>姓名</th>
        <th>年龄</th>
        <th>职业</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item, index) in tableData" :key="index">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>{{ item.job }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 28, job: '工程师' },
        { name: '李四', age: 32, job: '设计师' }
      ]
    }
  }
}
</script>

动态表头渲染

若表头需要动态生成,可以结合 v-for 实现:

<template>
  <table>
    <thead>
      <tr>
        <th v-for="(header, index) in headers" :key="index">
          {{ header }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, rowIndex) in tableData" :key="rowIndex">
        <td v-for="(cell, cellIndex) in row" :key="cellIndex">
          {{ cell }}
        </td>
      </tr>
    </tbody>
  </table>
</template>

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

使用计算属性处理数据

对于需要复杂处理的数据,可以使用计算属性:

<template>
  <table>
    <tr v-for="(item, index) in processedData" :key="index">
      <td>{{ item.fullName }}</td>
      <td>{{ item.ageGroup }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      rawData: [
        { firstName: '张', lastName: '三', age: 28 },
        { firstName: '李', lastName: '四', age: 32 }
      ]
    }
  },
  computed: {
    processedData() {
      return this.rawData.map(item => ({
        fullName: `${item.firstName}${item.lastName}`,
        ageGroup: item.age > 30 ? '中年' : '青年'
      }))
    }
  }
}
</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']
}
</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>

第三方表格组件

对于复杂需求,可以使用现成的表格组件库:

  1. Element UIel-table

    <el-table :data="tableData">
    <el-table-column prop="name" label="姓名"></el-table-column>
    <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>
  2. VxeTable

    <vxe-table :data="tableData">
    <vxe-column field="name" title="姓名"></vxe-column>
    <vxe-column field="age" title="年龄"></vxe-column>
    </vxe-table>

性能优化

大数据量渲染时建议使用虚拟滚动:

<template>
  <RecycleScroller
    class="table-body"
    :items="tableData"
    :item-size="50"
    key-field="id"
  >
    <template #default="{ item }">
      <tr>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </template>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller'
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css'

export default {
  components: { RecycleScroller },
  data() {
    return {
      tableData: [] // 大数据量
    }
  }
}
</script>

vue 实现表格渲染

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

相关文章

vue怎么实现动画

vue怎么实现动画

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡系统、第三方库集成以及 CSS 动画。以下是常见实现方法: 使用 Vue 过渡系统 通过 <transition>…

vue实现多级表头

vue实现多级表头

Vue 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-ta…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <t…

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

国外vue实现表格控件

国外vue实现表格控件

Vue 表格控件实现方案 使用开源组件库(推荐) Element UI、Vuetify、Ant Design Vue 等主流库提供成熟的表格组件,支持排序、分页、筛选等功能。 Element U…