antd3中Form实现编辑和新增共用Modal时出现的问题

       在做项目时,表单新增和编辑框往往是一样的,只不过一个有数据,而一个没有数据

但是我修改了编辑框保存后,再打开新增对话框,总是有数据,也就是数据没有清空

于是在查找官网文档,我用的是antd3版本,有一个resetField函数:

resetFields重置一组输入控件的值(为 initialValue)与状态,如不传入参数,则重置所有组件Function([names: string[]])

 

编辑函数:

//编辑
    handleEdit = (row) => {
        const { form } = this.formRef.props;   //获取form
        form.resetFields();    //清空form中每条数据

        this.setState({
            editModalVisible:true,
            currentRowData:Object.assign({}, row),
            modalTitle: '编辑'
        })
    }

新增函数:

handleAdd = () => {
        const { form } = this.formRef.props;
        form.resetFields();
        this.setState({
            modalTitle: '新增',
            editModalVisible:true,
            currentRowData:[],
        })
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
antdvue3,要使resetFields生效,需要在modal关闭调用resetFields方法。具体的代码实现如下: ``` <template> <div> <a-button @click="openModal">打开modal</a-button> <a-modal v-model:visible="visible" @ok="handleOk" @cancel="handleCancel"> <a-form ref="form" :model="form"> <a-form-item label="姓名" :rules="{ required: true, message: '请输入姓名' }"> <a-input v-model:value="form.name" /> </a-form-item> <a-form-item label="年龄" :rules="{ required: true, message: '请输入年龄' }"> <a-input-number v-model:value="form.age" /> </a-form-item> </a-form> </a-modal> </div> </template> <script> import { ref } from 'vue'; import { Button, Modal, Form, Input, InputNumber } from 'ant-design-vue'; export default { components: { 'a-button': Button, 'a-modal': Modal, 'a-form': Form, 'a-form-item': Form.Item, 'a-input': Input, 'a-input-number': InputNumber, }, setup() { const visible = ref(false); const form = ref({ name: '', age: null }); const openModal = () => { visible.value = true; }; const handleOk = () => { // 提交表单 form.value = { name: '', age: null }; visible.value = false; }; const handleCancel = () => { // 关闭modal调用resetFields方法 const formRef = this.$refs.form; formRef.resetFields(); form.value = { name: '', age: null }; visible.value = false; }; return { visible, form, openModal, handleOk, handleCancel, }; }, }; </script> ``` 在handleCancel方法,我们首先获取form的ref,然后调用resetFields方法重置表单数据,最后将form的值设置为空对象{},将visible的值设置为false,关闭modal。这样就能够使resetFields生效了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值