vue中编辑和新增数据使用同一个dialog,dialog中使用同一个保存方法

在这里插入图片描述
新增和编辑的字段是一样的,所以展示同一个dialog,唯一的区别就是新增里面的字段都是空的,但是编辑里面的字段是表格中已经存在的,并且都是有数据的。
图1新增的dialog,字段里面的数据都是空的
在这里插入图片描述
图2编辑的dialog里面的字段是有数据的
在这里插入图片描述

//新增操作
<el-button type="primary" size="mini" style="margin-top:5px;" @click="addProgram">新增</el-button>


//编辑操作
<el-table-column label="操作">
  <template slot-scope="scope">
    <el-button type="primary" size="mini" @click="editProgram(scope.row)">编辑</el-button>
  </template>
</el-table-column>


//打开新增的diaglog最后进行保存
<div slot="footer" class="dialog-footer">
   <el-button @click="dialogTableVisible = false">取 消</el-button>
   <el-button type="primary" @click="saveData">保 存</el-button>
</div>


//下面是新增或编辑之后的保存方法
//保存新增数据
<script>
methods:{
//新增数据
  addProgram() {
    this.type = 1//新增数据的type为1
    this.dialogTableVisible = true//当点击新增按钮的时候跳出dialog
    this.clear()//打开的时候清空表格中的数据
  },
//编辑数据
  editProgram(row) {
    this.type = 2//编辑数据的type为2
    this.gridData = row//表格中的数据赋值给列表,从而在dialog中就有数据了
    this.dialogTableVisible = true//当点击编辑按钮的时候跳出dialog
    },
//保存数据
saveData(type) {//给保存方法添加参数,用来区分是新增还是编辑
  if (this.type == 1) {//如果是新增
    this.API.tIndEncPerformanceSave(this.gridData)
      .then((res) => {
        console.log(res)
        if (res.code == 200) {
          this.$message.success('保存成功')
          this.tIndEncPerformancePageByDate()//更新显示整个表格,做一步查询操作
          this.dialogTableVisible = false
        }
      })
      .catch((err) => {
        this.$message.error(err)
        this.dialogTableVisible = false
      })
  } else if (this.type == 2) {//如果是编辑
    this.API.tIndEncPerformanceUpdate(this.gridData).then((res) => {
      console.log(res)
      if (res.code == 200) {
        this.$message.success('保存成功')
        this.tIndEncPerformancePageByDate()
        this.dialogTableVisible = false
      }
    })
  }
},
}
</script>

``

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设你已经拥有一个名为 `DialogForm.vue` 的弹框组件,可以按照以下步骤实现新增编辑的共用: 1. 在 `DialogForm.vue` 定义 `props` 属性,用于接收父组件传递的数据。 ```javascript <template> <el-dialog :visible.sync="visible" title="弹框标题" @close="handleClose"> <!-- 表单内容 --> </el-dialog> </template> <script> export default { props: { visible: { type: Boolean, required: true }, formData: Object // 用于传递表单数据 }, setup(props) { // setup代码 } } </script> ``` 2. 在父组件引入 `DialogForm.vue` 组件,并在父组件定义 `visible` 和 `formData` 数据。 ```javascript <template> <div> <el-button @click="handleAdd">新增</el-button> <el-button @click="handleEdit(row)">编辑</el-button> <dialog-form :visible="visible" :form-data="formData" /> </div> </template> <script> import DialogForm from './DialogForm.vue' export default { components: { DialogForm }, data() { return { visible: false, formData: {} } }, methods: { handleAdd() { this.visible = true // 清空表单数据 this.formData = {} }, handleEdit(row) { this.visible = true // 将当前行数据传递给子组件 this.formData = row } } } </script> ``` 3. 在 `DialogForm.vue` 的 `setup` 方法使用 `watch` 监听 `props` 属性的变化,根据 `formData` 是否为空来判断是新增还是编辑。 ```javascript <script> import { ref, watch } from 'vue' export default { props: { visible: { type: Boolean, required: true }, formData: Object // 用于传递表单数据 }, setup(props) { const formRef = ref(null) watch(() => props.visible, (val) => { if (val) { if (props.formData) { // 编辑 // 将表单数据初始化为props传递的数据 formRef.value.resetFields() formRef.value.setFieldsValue(props.formData) } else { // 新增 // 清空表单数据 formRef.value.resetFields() } } }) const handleClose = () => { // 关闭弹框时重置表单数据 formRef.value.resetFields() } return { formRef, handleClose } } } </script> ``` 这样就可以实现使用同一个弹框组件来进行新增编辑操作了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值