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

动态列与行控制

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

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 等库实现异步数据加载,注意处理加载状态。

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>

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

vue表格动态实现

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 的 v-for 指令渲染数组数据到表格中。 <template> <table> <thead> <…

vue实现子表格

vue实现子表格

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

css怎么制作表格

css怎么制作表格

使用HTML和CSS制作表格 在HTML中,表格由<table>标签定义,结合CSS可以灵活控制样式。 基本HTML结构: <table> <tr>…

vue实现数据动态

vue实现数据动态

Vue 实现数据动态绑定的方法 Vue.js 的核心特性之一是数据绑定,通过响应式系统实现数据的动态更新。以下是几种常见的实现方式: 双向数据绑定(v-model) 使用 v-model 指令可以实…

vue实现表格滑动

vue实现表格滑动

Vue实现表格滑动的方法 使用CSS overflow属性实现横向滑动 在表格外层容器设置overflow-x: auto,限制表格宽度超出时出现滚动条。这种方法适合简单场景,无需额外依赖库。 &l…

vue实现拖拽表格

vue实现拖拽表格

Vue 实现拖拽表格的方法 使用第三方库(推荐) 推荐使用 vuedraggable 或 sortablejs 这类成熟的拖拽库,它们对 Vue 有良好支持且功能丰富。 安装 vuedraggabl…