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

Vue3 组件用的 ant 可以换成你们用,主要是form校验

html 复制代码
前端代码
<a-form
        ref="formRef"
        :model="formData"
        :label-col-props="{ span: 5 }"
        :wrapper-col-props="{ span: 17 }"
    >
      <a-row>
          <a-form-item
              label="角色单位"
              :rules="[
                    {
                      required: true,
                      message: '请选择角色',
                    },]"
          >
            <a-row>
              <a-col v-for="(item, index) in formData.arr" :key="index">
                <a-space>
                  <a-form-item
                      hide-label
                      :field="`arr.${index}.roleId`"
                      :rules="[
                    {
                      required: true,
                      message: '请选择角色',
                    },
                  ]"
                  >
                    <a-select v-model="item.roleId" placeholder="请选择角色" style="width: 150px">
                      <a-option
                          v-for="it in roleList"
                          :key="it.id"
                          :value="it.id"
                      >
                        {{ it.roleName }}
                      </a-option>
                    </a-select>
                  </a-form-item>
                  <a-form-item
                    hide-label
                    :field="`arr.${index}.deptIds`"
                    :rules="[
                    {
                      required: true,
                      message: '请选择部门',
                    },
                  ]"
                  >
                    <a-tree-select
                        v-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的

html 复制代码
<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-input
                  v-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
        }
      })
相关推荐
里欧跑得慢20 小时前
CSS 模块化架构的演进:BEM、CSS Modules 到 CSS-in-JS 的反思
前端·css·flutter·web·css-in-js
IT_陈寒20 小时前
Vue的computed属性把我坑惨了,原来我一直用错姿势
前端·人工智能·后端
小灰灰搞电子21 小时前
Rust+Slint 实现温度计源码分享
前端·rust·slint
计算机魔术师21 小时前
面壁智能 OpenBMB 推出 MathForm,面向 Lean 4 数学自动形式化的开源框架、数据集与模型
前端
NeilCarmack1 天前
Deepseek-harness增加桌面版端序列:第 2 讲 · spawn Electron:当前进程如何“交棒“
前端·javascript·electron
上海魁鲸科技有限公司1 天前
APS高级排产系统到底有什么用?一文讲清功能、选型与落地建议
前端·microsoft·excel
陈随易1 天前
Bun v1.4 更新总结:把浏览器、图片、定时任务和工程工具都装进一个运行时
前端·后端·程序员
东风破_1 天前
TypeScript 高级类型进阶:keyof、Exclude、Record 与类型组合思想
前端·后端·typescript
DS随心转插件1 天前
Grok生成的html怎么导出——AI导出鸭:大模型结构化输出的“最后一公里”工程化解构
前端·人工智能·ai·html·豆包·deepseek·ai导出鸭
এ慕ོ冬℘゜1 天前
使用 jQuery 动态渲染表格与状态切换
前端·javascript·jquery