vue之按钮新增删除

<el-col :span="1.5">
  <el-button
    type="primary"
    plain
    icon="el-icon-plus"
    size="mini"
    @click="handleAdd"
    v-hasPermi="['pay:configBank:add']"
  >新增推广码
  </el-button>
</el-col>
<el-col :span="1.5">
  <el-button
    type="danger"
    plain
    icon="el-icon-delete"
    size="mini"
    @click="handleDelete"
    v-hasPermi="['pay:configBank:remove']"
  >删除推广码
  </el-button>
</el-col>

 

 

<!-- 新增推广码弹框 -->
<el-dialog
  v-dialogDrag
  :close-on-click-modal="false"
  title="新增推广码"
  :visible.sync="addPromotionCode"
  width="500px"
  append-to-body
>
  <el-form ref="formaddPromotionCode" :model="formaddPromotionCode" :rules="rules" label-width="70px">
    <el-form-item label="推广码" prop="code">
      <el-input placeholder="请输入推广码" v-model="formaddPromotionCode.code"
      />
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitaddPromotionCode">立即提交</el-button>
  </div>
</el-dialog>

<!-- 删除推广码弹框 -->
<el-dialog
  v-dialogDrag
  :close-on-click-modal="false"
  title="删除推广码"
  :visible.sync="delPromotionCode"
  width="500px"
  append-to-body
>
  <el-form ref="formdelPromotionCode" :model="formdelPromotionCode" :rules="rules" label-width="70px">
    <el-form-item label="推广码" prop="code">
      <el-input placeholder="请输入推广码" v-model="formdelPromotionCode.code"
      />
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="submitdelPromotionCode">立即提交</el-button>
  </div>
</el-dialog>

 

 

//新增推广码弹框
addPromotionCode: false,
//删除推广码弹框
delPromotionCode: false,
//新增推广码参数
formaddPromotionCode: {},
//删除推广码参数
formdelPromotionCode: {},

 

 

// 表单校验
rules: {
  code: [
    {required: true, message: "推广码不能为空", trigger: "blur"}
  ]
}

 

 

//新增推广码
handleAdd() {
  this.resetformaddPromotionCode()
  this.addPromotionCode = true
},
//删除推广码
handleDelete() {
  this.resetformdelPromotionCode()
  this.delPromotionCode = true
},
// 新增推广码表单重置
resetformaddPromotionCode() {
  this.formaddPromotionCode = {
    code: null
  };
  this.resetForm("formaddPromotionCode");
},
// 删除推广码表单重置
resetformdelPromotionCode() {
  this.formdelPromotionCode = {
    code: null
  };
  this.resetForm("formdelPromotionCode");
},

 

 

/** 新增推广码提交按钮 */
submitaddPromotionCode() {
  this.$refs["formaddPromotionCode"].validate(valid => {
    if ((/^[0-9]+$/).test(this.formaddPromotionCode.code)) {
      addPromotionCode(this.formaddPromotionCode).then(response => {
        if (response.code == 0) {
          this.$message.error(response.msg);
        } else {
          this.msgSuccess("新增成功");
          this.addPromotionCode = false;
          this.getList();
        }
      });
    } else {
      this.$message.error("推广码只能为纯数字");
    }
  });
},
/** 删除推广码提交按钮 */
submitdelPromotionCode() {
  this.$refs["formdelPromotionCode"].validate(valid => {
    if ((/^[0-9]+$/).test(this.formdelPromotionCode.code)) {
      delPromotionCode(this.formdelPromotionCode).then(response => {
        if (response.code == 0) {
          this.$message.error(response.msg);
        } else {
          this.msgSuccess("删除成功");
          this.delPromotionCode = false;
          this.getList();
        }
      });
    } else {
      this.$message.error("推广码只能为纯数字");
    }
  });
},

 

 

<think>我们正在讨论Vue中实现表格新增行和删除行功能的方法。根据用户的需求,他们希望了解如何实现这两个功能,并且之前有提到过Element UI(因为引用中出现了element表格)。因此,我将基于Element UI组件库来给出实现方案,因为它是Vue生态中非常流行的UI框架之一。 主要思路: 1. 数据驱动:Vue的核心是数据驱动视图,因此表格的数据通常是一个数组,数组中的每个元素代表一行数据。 2. 新增行:向这个数组添加一个新的空数据对象(或默认值对象)即可。 3. 删除行:从数组中移除指定索引或指定ID的行数据。 步骤: 1. 定义表格数据(数组) 2. 在模板中使用Element UI的表格组件(el-table)来展示数据 3. 为每一行添加操作列,包含删除按钮 4. 添加一个“新增行”按钮,点击时触发新增行的方法 以下是一个简单的实现示例: 模板部分(使用Element UI): ```html <template> <div> <el-button @click="addRow">新增行</el-button> <el-table :data="tableData" style="width: 100%"> <!-- 假设表格有两列:姓名和年龄 --> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <el-button size="mini" type="danger" @click="deleteRow(scope.$index)">删除</el-button> </template> </el-table-column> </el-table> </div> </template> ``` 脚本部分: ```javascript <script> export default { data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 22 } ] } }, methods: { // 新增行 addRow() { // 向数组中添加一个新的对象,这里使用空数据(可根据实际情况设置默认值) this.tableData.push({ name: '', age: '' }); }, // 删除行 deleteRow(index) { this.tableData.splice(index, 1); } } } </script> ``` 说明: 1. 在模板中,我们使用了Element UI的el-table组件,并将tableData作为数据源。 2. 新增行:通过addRow方法,在tableData数组末尾添加一个新的对象(这里是一个空对象,也可以设置默认值)。 3. 删除行:通过deleteRow方法,传入当前行的索引,使用splice方法从数组中移除该索引对应的元素。 注意: - 删除操作时,通常需要确认,可以添加一个确认框(Element UI提供了el-popconfirm或使用$confirm)。 - 新增行时,如果需要给每一行一个唯一的标识(例如id),以便在删除时更准确(特别是在有分页、排序等情况下,索引可能不可靠),那么可以在数据模型中增加id字段,并使用唯一值(如时间戳、UUID等)来标识。 如果需要使用唯一标识来删除,可以修改为: 数据: ```javascript data() { return { tableData: [ { id: 1, name: '张三', age: 20 }, { id: 2, name: '李四', age: 22 } ] } } ``` 方法: ```javascript deleteRow(id) { this.tableData = this.tableData.filter(item => item.id !== id); } ``` 但是,在模板中需要传递id而不是索引: ```html <el-button @click="deleteRow(scope.row.id)">删除</el-button> ``` 另外,如果表格内容需要编辑,那么可以在每列中使用可编辑的单元格,例如使用输入框(el-input)或者使用一个可编辑的组件(如el-input在编辑状态下显示)。 以上是一个基本的实现。根据用户之前的引用,他们可能还涉及到一些字段的配置(如fieldEnglishName等),因此在实际项目中,表格的列可能是动态的,但核心的数据操作(增删)方法是一致的。 注意事项: - 新增行时,如果是复杂对象(嵌套对象),需要注意使用深拷贝还是浅拷贝,以避免引用问题。 - 删除行时,如果使用了唯一标识,则更加安全可靠。 如果用户没有使用Element UI,而是使用原生的HTML表格,思路也是一样的,只是模板部分使用原生表格标签。 下面给出一个原生HTML表格的实现示例: 模板: ```html <template> <div> <button @click="addRow">新增行</button> <table border="1"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(row, index) in tableData" :key="index"> <td><input type="text" v-model="row.name"></td> <td><input type="number" v-model="row.age"></td> <td><button @click="deleteRow(index)">删除</button></td> </tr> </tbody> </table> </div> </template> ``` 脚本部分与上述Element UI示例中的脚本部分相同。 在原生表格中,我们使用v-for循环生成表格行,并绑定输入框的v-model来实现数据的双向绑定。删除新增操作同样是通过操作数组来完成。 综上所述,无论使用UI框架还是原生HTML,核心都是操作数据数组。用户可以根据自己的项目情况选择合适的方式。 相关问题: 1. 在使用Vue和Element UI实现表格行编辑时,如何实现保存所有修改? 2. Vue中如何实现动态表头(即列的数量和字段由后台返回)? 3. 在新增行时,如何避免使用索引作为key值?有什么更好的方式? </think>在 Vue 中实现表格的新增行和删除行功能,核心思路是利用响应式数据绑定和数组操作方法。以下是基于 Element UI 的实现方案(也适用于原生 HTML 表格): ### 一、完整实现示例 ```vue <template> <div> <!-- 新增按钮 --> <el-button @click="addNewRow" type="primary">新增行</el-button> <!-- 数据表格 --> <el-table :data="tableData" border style="width: 100%"> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="age" label="年龄" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column label="操作"> <template #default="scope"> <el-button size="mini" type="danger" @click="deleteRow(scope.$index)" >删除</el-button> </template> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { // 初始表格数据 tableData: [ { name: '张三', age: 25, address: '北京市朝阳区' }, { name: '李四', age: 30, address: '上海市浦东新区' } ] } }, methods: { // 新增行方法 addNewRow() { this.tableData.push({ name: '', age: null, address: '' }) }, // 删除行方法 deleteRow(index) { this.tableData.splice(index, 1) } } } </script> ``` ### 二、核心实现原理 1. **响应式数据绑定** - 表格数据存储在 `tableData` 数组中 - Vue 会自动监测数组变化并更新视图 - 修改数组即可触发界面重渲染 2. **新增行实现** ```javascript addNewRow() { this.tableData.push({...}) // 添加空数据对象 } ``` - 使用 `push()` 方法添加新对象到数组末尾 - 新对象包含表格所需字段的初始值 3. **删除行实现** ```javascript deleteRow(index) { this.tableData.splice(index, 1) // 删除指定索引行 } ``` - `splice(index, 1)` 删除数组中指定位置的元素 - `scope.$index` 获取当前行的索引位置 ### 三、关键注意事项 1. **唯一标识问题** - 建议为每行数据添加唯一 `id` 字段 - 用 `id` 替代 `index` 作为删除依据更可靠 ```javascript deleteRow(id) { this.tableData = this.tableData.filter(item => item.id !== id) } ``` 2. **深拷贝问题** - 当行数据包含嵌套对象时,使用深拷贝避免引用问题: ```javascript import { cloneDeep } from 'lodash' addNewRow() { const newRow = cloneDeep(this.emptyTemplate) // 深拷贝模板 this.tableData.push(newRow) } ``` 3. **表单验证(编辑场景)** ```vue <el-table-column prop="name"> <template #default="{ row }"> <el-input v-model="row.name" :rules="[{ required: true }]" /> </template> </el-table-column> ``` ### 四、原生 HTML 实现方案 ```vue <template> <div> <button @click="addRow">新增行</button> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(row, index) in rows" :key="index"> <td><input v-model="row.name"></td> <td><input type="number" v-model="row.age"></td> <td><button @click="removeRow(index)">删除</button></td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { rows: [{ name: '', age: null }] } }, methods: { addRow() { this.rows.push({ name: '', age: null }) }, removeRow(index) { this.rows.splice(index, 1) } } } </script> ``` ### 五、最佳实践建议 1. **状态管理** - 复杂场景使用 Vuex/Pinia 管理表格数据状态 - 集中处理数据验证和业务逻辑 2. **性能优化** - 大数据量使用虚拟滚动 (`<el-table-v2>`) - 合理使用 `key` 属性标识行数据 ```vue <tr v-for="row in tableData" :key="row.id"> ``` 3. **批量操作** - 添加全选功能和批量删除 ```javascript deleteSelected() { this.tableData = this.tableData.filter(row => !row.checked) } ``` 完整示例可参考 Element UI 官方文档: [Element Table 组件](https://element.eleme.cn/#/zh-CN/component/table)[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值