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

性能优化

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

vue 实现表格渲染

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

实现vue cli

安装 Vue CLI 确保系统中已安装 Node.js(版本建议 12.x 或更高)。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 安装完成后验证版本:…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue轮播组件实现

vue轮播组件实现

Vue 轮播组件实现方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template…

vue实现吸附效果

vue实现吸附效果

Vue实现吸附效果的方法 吸附效果通常指页面滚动时,某个元素固定在特定位置(如顶部或底部)。以下是几种实现方式: 使用CSS的position: sticky 通过CSS的sticky定位实现吸附效…

vue实现增量更新

vue实现增量更新

Vue 实现增量更新的方法 使用动态组件和 v-if 控制渲染 通过 v-if 或 v-show 控制组件的渲染,避免不必要的 DOM 操作。动态组件结合 keep-alive 可以缓存组件状态,减少…