ant a-form/a-form-model用法(组件使用、表单验证、数据收集、数据重置、数据提交)

一、a-form

组件使用

html 复制代码
    <a-form
      :form="form"
      layout="vertical"
      :label-col="{span: 6}"
      :wrapper-col="{span: 17,offset:1}"
    ></a-form>

绑定数据

html 复制代码
<a-form-item :label="$t('m.equipment') + 'ID'">
            <!-- v-decorator="[
                  'deviceId',
                  {
                    rules: [{ required: false, message: $t('m.PleaseEnter') + $t('m.deviceId') }],
                  },
            ]"-->
            <a-input
              v-model="queryParam.deviceId"
              :placeholder=" $t('m.PleaseEnter') + $t('m.deviceId')"
            />
          </a-form-item>

定义form

javascript 复制代码
form: this.$form.createForm(this),

提交数据,表单验证

javascript 复制代码
      this.form.validateFields((err, values) => {
        if (!err) {
  
        }
      })

打开表单时,内容重置 / 单个设置表单内容

javascript 复制代码
        this.form.resetFields()
          this.form.setFieldsValue()
          this.form.setFieldsValue({
            name: this.data.name,
            age: this.data.age
          })
         

获取表单内容

javascript 复制代码
this.form.getFieldsValue();
this.form.getFieldValue('name')

打开表单后,将整条数据对应填写到表单

javascript 复制代码
 this.form.resetFields()
          this.model = Object.assign({}, this.record)
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model, ',name', 'age'))
          })

有的时候可能会报错:

报错的话加一下this.$nextTick(()=>{})


二、a-form-model

组件使用

javascript 复制代码
          <a-form-model ref="ruleForm" :model="form" :rules="rules" :layout="form.layout">
            <a-form-model-item :label="$t('m.electronicFence') + $t('m.name')" prop="name">
              <a-input v-model="form.name" :placeholder="$t('m.electronicFence') + $t('m.name')" />
            </a-form-model-item>
            <a-form-model-item :label="$t('m.yaddress') + $t('m.search')" prop="address">
              <input
                style="height:15px"
                v-model="form.address"
                id="tipinput"
                :placeholder="$t('m.electronicFence') + $t('m.yaddress')"
              />
            </a-form-model-item>
            <a-form-model-item>
              <a-button @click="openPloy()" style="margin-bottom: 5px;margin-left:20px">编辑当前区域</a-button>
              <a-button style="margin-left:20px" @click="delArea()">删除当前区域</a-button>
            </a-form-model-item>
          </a-form-model>

data:

javascript 复制代码
      form: {
        name: '',
        address: undefined,
        layout: 'inline'
      },
      rules: {
        name: [{ required: true, message: `请输入电子围栏名称`, trigger: 'blur' }],
        address: [{ required: true, message: `请选择地址`, trigger: 'blur' }]
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },

提交

javascript 复制代码
onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

重置

javascript 复制代码
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },

至于修改、获取form中绑定的值,直接this.form就好了


发表一下个人看法

本人认为第二种方法比第一种简单的多,使用也很方便。

相关推荐
一 乐12 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
C_心欲无痕12 小时前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
清沫12 小时前
Claude Skills:Agent 能力扩展的新范式
前端·ai编程
yinuo13 小时前
前端跨页面通信终极指南:方案拆解、对比分析
前端
北辰alk13 小时前
Vue 模板引擎深度解析:基于 HTML 的声明式渲染
vue.js
北辰alk13 小时前
Vue 自定义指令完全指南:定义与应用场景详解
vue.js
yinuo13 小时前
前端跨页面通讯终极指南⑨:IndexedDB 用法全解析
前端
北辰alk14 小时前
Vue 动态路由完全指南:定义与参数获取详解
vue.js
北辰alk14 小时前
Vue Router 完全指南:作用与组件详解
vue.js
北辰alk14 小时前
Vue 中使用 this 的完整指南与注意事项
vue.js