当前位置:首页 > VUE

vue表格动态实现

2026-03-29 05:26:47VUE

Vue 动态表格实现方法

基于 v-for 的静态数据渲染

动态表格的核心是通过 v-for 循环渲染数据。定义表格数据为响应式数组,通过方法动态修改数据即可实现更新。

<template>
  <table>
    <thead>
      <tr>
        <th v-for="col in columns" :key="col">{{ col }}</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(row, index) in tableData" :key="index">
        <td v-for="col in columns" :key="col">{{ row[col] }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      columns: ['name', 'age', 'gender'],
      tableData: [
        { name: 'Alice', age: 25, gender: 'Female' },
        { name: 'Bob', age: 30, gender: 'Male' }
      ]
    }
  }
}
</script>

动态列与行控制

通过计算属性或方法动态生成列信息,支持字段的增删改查。

vue表格动态实现

methods: {
  addColumn(colName) {
    if (!this.columns.includes(colName)) {
      this.columns.push(colName)
      this.tableData.forEach(row => {
        row[colName] = '' // 初始化新列
      })
    }
  },
  removeColumn(colName) {
    const index = this.columns.indexOf(colName)
    if (index > -1) {
      this.columns.splice(index, 1)
      this.tableData.forEach(row => {
        delete row[colName]
      })
    }
  }
}

服务端数据加载

结合 axios 等库实现异步数据加载,注意处理加载状态。

vue表格动态实现

data() {
  return {
    loading: false,
    tableData: []
  }
},
async created() {
  this.loading = true
  try {
    const res = await axios.get('/api/data')
    this.tableData = res.data
  } finally {
    this.loading = false
  }
}

高级功能实现

可编辑单元格:通过动态切换 input 和文本显示

<td @dblclick="editCell(row, col)">
  <input v-if="row.editing === col" 
         v-model="row[col]"
         @blur="saveEdit(row)">
  <span v-else>{{ row[col] }}</span>
</td>

动态排序:使用计算属性实现

computed: {
  sortedData() {
    return [...this.tableData].sort((a, b) => {
      return a[this.sortBy] > b[this.sortBy] ? 1 : -1
    })
  }
}

性能优化建议

  • 大数据量使用虚拟滚动(如 vue-virtual-scroller)
  • 复杂表格考虑专业组件库(如 Element UI 的 el-table)
  • 使用 :key 时避免索引,采用唯一 ID
  • 分页加载减少初始渲染压力

完整示例

<template>
  <div>
    <button @click="addRandomRow">Add Row</button>
    <table>
      <thead>
        <tr>
          <th v-for="col in columns" 
              :key="col"
              @click="sortBy(col)">
            {{ col }} {{ sortBy === col ? (sortDesc ? '↓' : '↑') : '' }}
          </th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="row in sortedData" :key="row.id">
          <td v-for="col in columns" :key="col">
            <EditableCell :value="row[col]" @update="val => updateCell(row, col, val)"/>
          </td>
          <td><button @click="removeRow(row.id)">Delete</button></td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import EditableCell from './EditableCell.vue'

export default {
  components: { EditableCell },
  data() {
    return {
      columns: ['id', 'name', 'value'],
      tableData: [],
      sortBy: 'id',
      sortDesc: false,
      nextId: 1
    }
  },
  computed: {
    sortedData() {
      return [...this.tableData].sort((a, b) => {
        const valA = a[this.sortBy]
        const valB = b[this.sortBy]
        return this.sortDesc ? valB - valA : valA - valB
      })
    }
  },
  methods: {
    addRandomRow() {
      this.tableData.push({
        id: this.nextId++,
        name: `Item ${this.nextId}`,
        value: Math.floor(Math.random() * 100)
      })
    },
    removeRow(id) {
      this.tableData = this.tableData.filter(row => row.id !== id)
    },
    sortBy(col) {
      if (this.sortBy === col) {
        this.sortDesc = !this.sortDesc
      } else {
        this.sortBy = col
        this.sortDesc = false
      }
    },
    updateCell(row, col, val) {
      row[col] = val
    }
  }
}
</script>

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

相关文章

vue实现子表格

vue实现子表格

Vue 实现子表格的方法 在 Vue 中实现子表格可以通过多种方式完成,以下是几种常见的方法: 使用嵌套组件 创建一个父表格组件和一个子表格组件,通过 props 将数据传递给子表格组件。父表格的每…

vue实现表格拖动列宽

vue实现表格拖动列宽

实现表格列宽拖动的核心思路 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽逻辑,动态修改列宽。需要处理表头与表体的联动,确保拖动时视觉一致。 基础HTML结构 &…

vue实现子表格

vue实现子表格

实现子表格的基本思路 在Vue中实现子表格通常需要结合组件化思想和动态渲染技术。常见的场景是在主表格的行内嵌套子表格,展开主表行时显示对应的子表数据。 使用el-table实现子表格(Element…

vue实现简单表格

vue实现简单表格

使用 Vue 实现简单表格 Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法: 基础表格实现 通过 v-for 指令循环渲染表格数据: <template&…

vue表格组件实现

vue表格组件实现

Vue表格组件实现 基础表格实现 使用el-table组件实现基础表格功能。安装Element UI后,引入el-table和el-table-column组件。 <template>…

vue实现分组表格

vue实现分组表格

Vue 实现分组表格的方法 使用 v-for 嵌套循环实现分组 通过嵌套的 v-for 循环可以轻松实现分组表格。外层循环遍历分组数据,内层循环遍历每组中的具体项。 <template>…