当前位置:首页 > VUE

vue实现添加行

2026-01-18 13:37:29VUE

Vue 实现添加行的基本方法

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

vue实现添加行

基础表格添加行

<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>

表单输入添加行

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

vue实现添加行

<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
  • 大量数据时考虑虚拟滚动优化性能

扩展功能

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

<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 登录功能实现步骤 创建登录表单组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含用户名和密码输入框以及提交按钮。 <template> &…

vue搜索过后实现分页

vue搜索过后实现分页

Vue 实现搜索后分页功能 数据绑定与搜索逻辑 在 Vue 组件中定义必要的数据属性: data() { return { searchQuery: '', currentPa…

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Token…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…