当前位置:首页 > VUE

vue 实现动态表格

2026-02-20 09:58:51VUE

Vue 动态表格实现方法

基础表格结构

使用 v-for 指令动态渲染表格行和列,数据通过 data 属性绑定:

<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: [
        ['张三', 25, '北京'],
        ['李四', 30, '上海']
      ]
    }
  }
}
</script>

动态列控制

通过计算属性实现动态列显示:

vue 实现动态表格

<template>
  <div>
    <label v-for="header in allHeaders" :key="header">
      <input type="checkbox" v-model="visibleHeaders" :value="header"> 
      {{ header }}
    </label>
    <table>
      <thead><!-- 仅显示选中列 --></thead>
      <tbody><!-- 对应数据渲染 --></tbody>
    </table>
  </div>
</template>

<script>
export default {
  computed: {
    filteredHeaders() {
      return this.allHeaders.filter(header => 
        this.visibleHeaders.includes(header)
      )
    }
  }
}
</script>

可编辑单元格

结合 v-model 实现单元格编辑:

vue 实现动态表格

<template>
  <td v-for="(cell, cellIndex) in row" :key="cellIndex">
    <input v-if="editable[rowIndex][cellIndex]" 
           v-model="tableData[rowIndex][cellIndex]">
    <span v-else @click="startEdit(rowIndex, cellIndex)">{{ cell }}</span>
  </td>
</template>

<script>
methods: {
  startEdit(row, col) {
    this.$set(this.editable[row], col, true)
  }
}
</script>

服务端数据加载

使用 axios 异步加载数据:

<script>
import axios from 'axios'

export default {
  mounted() {
    axios.get('/api/table-data').then(response => {
      this.tableData = response.data
    })
  }
}
</script>

性能优化

对于大数据量场景建议使用虚拟滚动:

<template>
  <div class="table-container" @scroll="handleScroll">
    <div class="table-body" :style="{ height: totalHeight + 'px' }">
      <div v-for="(row, index) in visibleRows" :key="index" class="row">
        <!-- 行内容渲染 -->
      </div>
    </div>
  </div>
</template>

完整组件示例

<template>
  <div>
    <table class="dynamic-table">
      <thead>
        <tr>
          <th v-for="header in filteredHeaders" :key="header">
            {{ header }}
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(row, rowIndex) in visibleRows" :key="rowIndex">
          <td v-for="(header, colIndex) in filteredHeaders" :key="colIndex">
            <editable-cell 
              :value="row[header]"
              @update="val => updateCell(rowIndex, header, val)"
            />
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  components: {
    EditableCell: {
      props: ['value'],
      template: `
        <div @click="edit = true" v-if="!edit">
          {{ value }}
        </div>
        <input v-else v-model="localValue" @blur="save"/>
      `,
      data: () => ({ edit: false, localValue: '' }),
      methods: {
        save() {
          this.$emit('update', this.localValue)
          this.edit = false
        }
      }
    }
  }
}
</script>

标签: 表格动态
分享给朋友:

相关文章

vue 表格实现

vue 表格实现

Vue 表格实现方法 基础表格实现 使用 el-table 组件(Element UI)实现基础表格: <template> <el-table :data="tableDat…

vue实现表格数据修改

vue实现表格数据修改

实现表格数据修改的基本思路 在Vue中实现表格数据修改通常涉及以下核心步骤:数据绑定、编辑状态切换、表单输入处理和数据提交。以下是一个典型实现方案: 数据绑定与渲染 使用v-for指令循环渲染表格数…

国外vue实现表格控件

国外vue实现表格控件

以下是国外流行的 Vue 表格控件实现方案及特点,供参考: vue-good-table 基于 Vue 2/3 的轻量级表格组件,支持排序、分页、筛选等功能。 安装方式: npm instal…

vue实现表格导入

vue实现表格导入

Vue 实现表格导入的方法 使用 Element UI 的 Upload 组件 Element UI 提供了 el-upload 组件,可以方便地实现文件上传功能。结合 xlsx 库解析 Excel…

vue实现导出表格

vue实现导出表格

Vue 实现导出表格的方法 使用 xlsx 库导出 Excel 安装 xlsx 库 npm install xlsx 在 Vue 组件中引入 xlsx import * as XLSX from…

vue实现表格追加

vue实现表格追加

实现表格数据追加的方法 在Vue中实现表格数据追加可以通过多种方式完成,主要涉及数据绑定和数组操作。以下是几种常见的方法: 数据绑定与v-for指令 通过v-for指令渲染表格数据,利用数组的…