当前位置:首页 > 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中实现动态禁用功能通常通过v-bind:disabled(或简写为:disabled)绑定一个响应式变量实现。当变量值为true时,元素被禁用;为false时启用。…

elementui动态

elementui动态

ElementUI 动态组件与数据绑定 ElementUI 支持动态渲染组件和数据绑定,常用于表格、表单等场景的动态生成。通过 v-for 和动态属性绑定实现组件灵活配置。 动态生成表格列 <…

vue实现表格树

vue实现表格树

Vue 实现表格树的方法 使用递归组件实现树形表格 在 Vue 中可以通过递归组件实现树形表格结构。定义一个组件,该组件可以调用自身来渲染子节点。 <template> &l…

vue实现动态弧线

vue实现动态弧线

Vue 实现动态弧线的方法 动态弧线可以通过 SVG 或 Canvas 实现,以下是两种常见的方法: 使用 SVG 实现动态弧线 SVG 的 <path> 元素可以绘制弧线,结合…

vue实现表格筛选

vue实现表格筛选

实现表格筛选的基本思路 在Vue中实现表格筛选通常涉及以下几个核心步骤:数据绑定、筛选逻辑处理、动态渲染。以下是具体实现方法。 基础数据绑定与表格渲染 定义表格数据和表头结构,使用v-for循环渲染…

vue实现的表格

vue实现的表格

Vue 实现表格的方法 使用原生 HTML 表格 在 Vue 模板中可以直接使用 HTML 的 <table> 标签创建表格。数据可以通过 v-for 指令动态渲染。 <templ…