ant-design-vue之中<a-form>表单清空

该文章展示了如何在Vue应用中重置<a-form>组件的表单数据。通过结合使用ref绑定的formRef调用resetFields方法以及将formState设置为null,可以确保表单的所有字段被完全清空。

如果我们给<a-form>绑定的ref为formRef ,绑定的数据值为formState。

<template>
   <div>
     <a-form  ref="formRef" :model="formState">
       <a-form-item  label="名字">
           <a-input v-model:value="formState.name" />
       </a-form-item>
       <a-form-item label="地址">
           <a-input v-model:value="formState.adress" />
       </a-form-item>
     </a-form>

      <a-button type="primary" @click="reset">重置<a-button>
   </div>
</template>

清空表单的数据值要从两个地方清空 

<script setup>

import {ref,reactive} from 'vue' 

const formState = reactive({
   name:"",
   adress:''

})

//点击重置按钮清空,要分别从绑定的ref调用resetRields()函数以及绑定的数据值formState清空
//这样才能达到真正清空的目的
const reset = () => {
 formRef.value.resetFields();//resetFields()函数是组件库提供的
 formState.value = null;
}

</script>

<template> <a-card :bordered="false"> <!-- 查询区域 --> <div class="table-page-search-wrapper"> <a-form layout="inline" @keyup.enter.native="searchQuery"> <a-row :gutter="24"> </a-row> </a-form> </div> <!-- 查询区域-END --> <!-- 操作按钮区域 --> <div class="table-operator"> <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> <a-button type="primary" icon="download" @click="handleExportXls(&#39;流程实例关联表单&#39;)">导出</a-button> <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel"> <a-button type="primary" icon="import">导入</a-button> </a-upload> <!-- 高级查询区域 --> <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query> <a-dropdown v-if="selectedRowKeys.length > 0"> <a-menu slot="overlay"> <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item> </a-menu> <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button> </a-dropdown> </div> <!-- table区域-begin --> <div> <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;"> <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项 <a style="margin-left: 24px" @click="onClearSelected">清空</a> </div> <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" class="j-table-force-nowrap" @change="handleTableChange"> <template slot="htmlSlot" slot-scope="text"> <div v-html="text"></div> </template> <template slot="imgSlot" slot-scope="text"> <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span> <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/> </template> <template slot="fileSlot" slot-scope="text"> <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span> <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)"> 下载 </a-button> </template> <span slot="action" slot-scope="text, record"> <a @click="handleEdit(record)">编辑</a> <a-divider type="vertical" /> <a-dropdown> <a class="ant-dropdown-link">更多 <a-icon type="down" /></a> <a-menu slot="overlay"> <a-menu-item> <a @click="handleDetail(record)">详情</a> </a-menu-item> <a-menu-item> <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)"> <a>删除</a> </a-popconfirm> </a-menu-item> </a-menu> </a-dropdown> </span> </a-table> </div> <sys-deploy-form-modal ref="modalForm" @ok="modalFormOk"></sys-deploy-form-modal> </a-card> </template> <script> import &#39;@/assets/less/TableExpand.less&#39; import { mixinDevice } from &#39;@/utils/mixin&#39; import { JeecgListMixin } from &#39;@/mixins/JeecgListMixin&#39; import SysDeployFormModal from &#39;./modules/SysDeployFormModal&#39; export default { name: &#39;SysDeployFormList&#39;, mixins:[JeecgListMixin, mixinDevice], components: { SysDeployFormModal }, data () { return { description: &#39;流程实例关联表单管理页面&#39;, // 表头 columns: [ { title: &#39;#&#39;, dataIndex: &#39;&#39;, key:&#39;rowIndex&#39;, width:60, align:"center", customRender:function (t,r,index) { return parseInt(index)+1; } }, { title:&#39;表单主键&#39;, align:"center", dataIndex: &#39;formId&#39; }, { title:&#39;流程实例主键&#39;, align:"center", dataIndex: &#39;deployId&#39; }, { title:&#39;创建人&#39;, align:"center", dataIndex: &#39;createBy&#39; }, { title:&#39;创建日期&#39;, align:"center", dataIndex: &#39;createTime&#39; }, { title: &#39;操作&#39;, dataIndex: &#39;action&#39;, align:"center", fixed:"right", width:147, scopedSlots: { customRender: &#39;action&#39; } } ], url: { list: "/flowable/sysDeployForm/list", delete: "/flowable/sysDeployForm/delete", deleteBatch: "/flowable/sysDeployForm/deleteBatch", exportXlsUrl: "/flowable/sysDeployForm/exportXls", importExcelUrl: "flowable/sysDeployForm/importExcel", }, dictOptions:{}, superFieldList:[], } }, created() { this.getSuperFieldList(); }, computed: { importExcelUrl: function(){ return `${window._CONFIG[&#39;domianURL&#39;]}/${this.url.importExcelUrl}`; }, }, methods: { /** 挂载流程定义 */ submitDefinitionForm(row) { this.customformParam.formId = row.id; updateCustomForm(this.customformParam).then(res => { this.$message.success(res.message); this.definitionOpen = false; this.getList(); }) }, handleCurrentChange(data) { if (data) { this.currentRow = JSON.parse(data.formContent); } }, getCategoryName(category) { let find = this.categorys.find(o => o.id == category); if (find) { return find.name } return &#39;&#39; }, initDictConfig(){ }, getSuperFieldList(){ let fieldList=[]; fieldList.push({type:&#39;string&#39;,value:&#39;formId&#39;,text:&#39;表单主键&#39;}) fieldList.push({type:&#39;string&#39;,value:&#39;deployId&#39;,text:&#39;流程实例主键&#39;}) fieldList.push({type:&#39;string&#39;,value:&#39;createBy&#39;,text:&#39;创建人&#39;}) fieldList.push({type:&#39;datetime&#39;,value:&#39;createTime&#39;,text:&#39;创建日期&#39;}) this.superFieldList = fieldList } } } </script> <style scoped> @import &#39;~@assets/less/common.less&#39;; </style>上传文件为vue文件,vue版本为vue2版本,脚本为JavaScript编写;现在将该vue文件进行转化:转化vue版本为vue3版本,脚本为TypeScript编写
最新发布
08-29
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值