当前位置: 首页 > news >正文

vue 动态增删行,并form表单校验(附v2\v3)

Vue3 组件用的 ant 可以换成你们用,主要是form校验
在这里插入图片描述

前端代码
<a-formref="formRef":model="formData":label-col-props="{ span: 5 }":wrapper-col-props="{ span: 17 }"><a-row><a-form-itemlabel="角色单位":rules="[{required: true,message: '请选择角色',},]"><a-row><a-col v-for="(item, index) in formData.arr" :key="index"><a-space><a-form-itemhide-label:field="`arr.${index}.roleId`":rules="[{required: true,message: '请选择角色',},]"><a-select v-model="item.roleId" placeholder="请选择角色" style="width: 150px"><a-optionv-for="it in roleList":key="it.id":value="it.id">{{ it.roleName }}</a-option></a-select></a-form-item><a-form-itemhide-label:field="`arr.${index}.deptIds`":rules="[{required: true,message: '请选择部门',},]"><a-tree-selectv-model="item.deptIds":multiple="true":allow-clear="true":allow-search="true":data="deptList":field-names="{ key: 'id', title: 'name' }"placeholder="请选择部门"style="width: 300px"></a-tree-select></a-form-item></a-space><a-button @click="xx" type="text">增加</a-button><a-button @click="zz(index)" type="text">删除</a-button></a-col></a-row></a-form-item></a-row></a-form>
js 方法
const formRef = ref(); // 校验
const formData = ref({ // form 对象arr: [{roleId: '',deptIds: []}] // 无限增加的数组
}); // 新增一行
const xx = () => {formData.value.arr.push({roleId: '',deptIds: []})
}
// 删除一行
const zz = (index:any) => {formData.value.arr.splice(index, 1)
}// 点击确定按钮 提交之类的  先在校验一次
const submit11 = () =>{const validateRes = await formRef.value?.validate();if (validateRes) return;// 通过后这里自己的逻辑
}

vue2的

<a-form-model:model="form2"ref="ruleForm"><a-table :columns="columns" :dataSource="form2.outDetailVOList" bordered><template #outNumber='text, record, index'><a-form-model-item:prop="'outDetailVOList.' + index + '.outNumber'":rules="{required: true,message: '请填写出库量',trigger: 'blur',}":disabled="action === 'info' "><a-inputv-model="form2.outDetailVOList[index].outNumber":disabled="action === 'info' "placeholder="请填写出库量"type='number'/></a-form-model-item></template><template #action='text, record, index' ><a v-if="action === 'add' " @click='delCommodity(index)'>删除</a></template></a-table></a-form-model>
const columns = [{title: '领取出库数量',dataIndex: 'outNumber',width: '200px',scopedSlots: { customRender: 'outNumber' }}
]data () {return {form2: {outDetailVOList: [{outNumber: ''}]},}},// 删除商品delCommodity (index) {console.log('', index)// if (this.form2.outDetailVOList.length > 1) {this.form2.outDetailVOList.splice(index, 1)// }},//  在你的提交方法里加 校验
this.$refs.ruleForm.validate(valid => {if (!valid) {console.log('error submit!!没通过校验')return false}})
http://www.lryc.cn/news/416732.html

相关文章:

  • 计算机网络的基本概念
  • Python 爬虫项目实战三:GitHub 用户信息抓取与分析
  • xtrabackup搭建MySQL 8.0 主从复制
  • Java程序员接单分享
  • 【HarmonyOS NEXT星河版开发学习】小型测试案例01-今日头条置顶练习
  • C语言----计算开机时间
  • 批发行业进销存-登录适配 android 横竖屏幕 源码CyberWinApp-SAAS 本地化及未来之窗行业应用跨平台架构
  • js功能(1)
  • 微信小程序乡村医疗系统,源码、部署+讲解
  • 完美解决pip命令版本冲突导致对应版本模块包无法安装的问题
  • 5.1-软件工程基础知识-软件工程概述
  • 极简聊天室-websocket版
  • QT学生管理系统 开发文档
  • 【五大海内外高校支持】2024年数字经济与计算机科学国际学术会议(DECS2024)
  • VS项目打包成lib库并使用
  • 社交巨头与去中心化:解析Facebook在区块链的角色
  • MyBatis(学习记录)
  • QSpinbox
  • 分布式服务架构[原理、设计与实践]学习笔记
  • PDF发票解析并将信息回填到前端(1)后端解析PDF
  • C++练习之插入删除
  • 【LeetCode】238. 除自身以外数组的乘积
  • Excel公式与函数(运算符,计算限制,错误检查)(一)
  • 用AI助手写程序
  • 动手学深度学习7.2 使用块的网络(VGG)-笔记练习(PyTorch)
  • SolidityFoundry ERC4626
  • 大模型时代的操作系统:融合 Rust 和大模型,打造 AI 操作系统
  • 【ML】为什么要做batch normlization,怎么做batch normlization
  • 【C++指南】命名空间
  • RocketMQ Dashboard安装