当前位置:首页 > 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 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contai…

vue组件实现

vue组件实现

Vue 组件实现基础 Vue 组件是 Vue.js 的核心特性之一,允许将 UI 拆分为独立、可复用的模块。组件的实现包括定义、注册、数据传递和生命周期管理等。 定义组件 组件可以通过单文件组件(.…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现tag

vue实现tag

Vue 实现标签(Tag)功能 在 Vue 中实现标签(Tag)功能可以通过多种方式完成,以下是一些常见的方法和实现步骤: 使用动态组件和 v-for 通过 v-for 指令动态渲染标签列表,结合样…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…