当前位置:首页 > 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实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现表格复选

vue实现表格复选

实现表格复选的基本思路 在Vue中实现表格复选功能,通常需要结合v-model绑定选中状态,监听复选框变化事件,以及维护一个选中项的数组。以下是具体实现方法: 基础实现步骤 数据准备 定义表格数据和…

vue 实现动态路由

vue 实现动态路由

动态路由的实现方法 Vue中实现动态路由通常涉及以下关键步骤,结合Vue Router的API和异步加载逻辑完成。 基础配置 安装Vue Router并初始化路由实例。在router/index.j…

vue动态实现select

vue动态实现select

Vue 动态实现 Select 组件 在 Vue 中动态实现 Select 组件可以通过多种方式完成,以下介绍几种常见的方法: 使用 v-for 动态渲染选项 通过 v-for 指令可以动态渲染 s…

vue表格实现导出

vue表格实现导出

使用 vue-json-excel 插件 安装插件: npm install vue-json-excel --save 在组件中引入并注册: import JsonExcel from 'vue…

怎么制作css表格

怎么制作css表格

制作CSS表格的方法 使用HTML表格标签结合CSS样式 通过HTML的<table>、<tr>、<td>等标签创建表格结构,再使用CSS美化样式。例如: <…