当前位置:首页 > VUE

vue实现添加行

2026-01-18 13:37:29VUE

Vue 实现添加行的基本方法

在 Vue 中实现表格或列表的动态添加行功能,可以通过操作数据数组结合 v-for 指令完成。以下是两种常见场景的实现方式:

基础表格添加行

<template>
  <div>
    <table>
      <tr v-for="(item, index) in items" :key="index">
        <td>{{ item.name }}</td>
        <td>{{ item.value }}</td>
      </tr>
    </table>
    <button @click="addRow">添加行</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { name: '默认行', value: 0 }
      ]
    }
  },
  methods: {
    addRow() {
      this.items.push({
        name: '新行',
        value: this.items.length
      })
    }
  }
}
</script>

表单输入添加行

对于需要用户输入后添加的场景:

<template>
  <div>
    <input v-model="newItem.name" placeholder="名称">
    <input v-model="newItem.value" placeholder="值" type="number">
    <button @click="addRow">添加</button>

    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }}: {{ item.value }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newItem: { name: '', value: 0 },
      items: []
    }
  },
  methods: {
    addRow() {
      if (this.newItem.name.trim()) {
        this.items.push({...this.newItem})
        this.newItem = { name: '', value: 0 } // 重置表单
      }
    }
  }
}
</script>

使用 Vue 3 Composition API

Vue 3 的响应式系统可以更简洁地实现:

<template>
  <button @click="addRow">添加随机数据行</button>
  <div v-for="(row, index) in rows" :key="index">
    行 {{ index }}: {{ row.data }}
  </div>
</template>

<script setup>
import { ref } from 'vue'

const rows = ref([])

const addRow = () => {
  rows.value.push({
    data: Math.random().toString(36).substring(7)
  })
}
</script>

注意事项

  • 为动态生成的元素添加唯一 key 属性,通常使用索引或唯一ID
  • 复杂表格建议使用专门的表格组件库如 Element UIVuetify
  • 大量数据时考虑虚拟滚动优化性能

扩展功能

实现可删除行的完整示例:

vue实现添加行

<template>
  <div>
    <button @click="addRow">+ 添加行</button>
    <table>
      <tr v-for="(row, index) in rows" :key="row.id">
        <td><input v-model="row.content"></td>
        <td><button @click="removeRow(index)">删除</button></td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rows: [],
      nextId: 1
    }
  },
  methods: {
    addRow() {
      this.rows.push({
        id: this.nextId++,
        content: ''
      })
    },
    removeRow(index) {
      this.rows.splice(index, 1)
    }
  }
}
</script>

标签: vue
分享给朋友:

相关文章

vue 实现滚动

vue 实现滚动

实现滚动的基本方法 在Vue中实现滚动效果可以通过多种方式完成,包括使用原生JavaScript、CSS或第三方库。以下是一些常见的方法: 使用window.scrollTo方法实现页面滚动 可以通…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…