当前位置:首页 > VUE

vue动态实现表

2026-03-30 07:44:03VUE

Vue 动态表格实现方法

数据驱动渲染

通过 v-for 指令绑定数组数据动态生成表格行

<template>
  <table>
    <tr v-for="(item, index) in tableData" :key="index">
      <td>{{ item.name }}</td>
      <td>{{ item.age }}</td>
    </tr>
  </table>
</template>

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

动态表头配置

使用计算属性处理表头数据

computed: {
  headers() {
    return [
      { text: '姓名', value: 'name' },
      { text: '年龄', value: 'age' }
    ]
  }
}

可编辑单元格

结合 v-model 实现双向绑定

vue动态实现表

<td v-for="header in headers" :key="header.value">
  <input v-model="item[header.value]" />
</td>

动态列渲染

根据配置动态决定显示列

methods: {
  toggleColumn(col) {
    this.visibleColumns = this.visibleColumns.includes(col) 
      ? this.visibleColumns.filter(c => c !== col)
      : [...this.visibleColumns, col]
  }
}

性能优化

对于大数据量使用虚拟滚动

vue动态实现表

<virtual-scroller :items="largeData" item-height="50">
  <template v-slot="{ item }">
    <tr>
      <td>{{ item.id }}</td>
      <td>{{ item.content }}</td>
    </tr>
  </template>
</virtual-scroller>

服务端分页

配合 axios 实现分页加载

async fetchData(page) {
  const res = await axios.get(`/api/data?page=${page}`)
  this.tableData = res.data.items
  this.total = res.data.total
}

响应式设计

通过 CSS 媒体查询适配不同屏幕

@media (max-width: 768px) {
  table {
    display: block;
    overflow-x: auto;
  }
}

标签: 动态vue
分享给朋友:

相关文章

vue实现生成二维码

vue实现生成二维码

使用qrcode.vue库生成二维码 安装qrcode.vue库: npm install qrcode.vue --save 在Vue组件中使用: <template> <…

vue 实现直播

vue 实现直播

Vue 实现直播的基本方法 在Vue中实现直播功能,通常需要结合WebRTC、RTMP或HLS等技术。以下是几种常见的实现方式: 使用WebRTC实现实时直播 WebRTC适合低延迟的实时直播场景,…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

甘特图vue实现

甘特图vue实现

甘特图 Vue 实现方法 使用开源库 vue-ganttastic 安装依赖: npm install vue-ganttastic 基础实现代码: <template> <…