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
        }
      })
相关推荐
ℋᙚᵐⁱᒻᵉ鲸落38 分钟前
移动端滑动手势冲突:overflow: auto导致外层横向滑动失效
前端·javascript·css·vue.js·html
默_笙2 小时前
🎃 前端学了 Next.js,后端该学啥?NestJS 就是 Node 版的蜜雪冰城
前端·javascript
前端snow3 小时前
ai agent --- 实现 openclaw 定时间效果
前端
码云之上3 小时前
换模型之后,Chatbot 为什么要自己做 compact?
前端·agent·前端工程化
এ慕ོ冬℘゜3 小时前
navigator 导航对象 BOM制作方法
javascript
星栈4 小时前
自定义 UI-UX-Pro-Max 的 CSV 知识库,把 AI 生成的后台拉回行业该有的样子
前端·agent·weui
研☆香4 小时前
简单的图片上传 删除 预览
前端
ITresearchGuest4 小时前
我用 AI 一小时写了一个世界杯数据可视化平台|前端 VibeCoding 初体验
前端·人工智能·信息可视化
慧一居士5 小时前
Vite项目中使用Less步骤,详细使用示例
前端·css
花间相见5 小时前
【LangChain组件03】—— LangChain Agents 执行与状态:工作流程与状态管理实战
java·前端·langchain