Ant Design Vue 下拉框(带查询和验证)

<template>
  <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">   
    <a-form-model-item label="爱好" prop="title">
      <!-- <a-input v-model.number="ruleForm.age" /> -->
      <a-select
        show-search
        placeholder="请填写"
        option-filter-prop="children"
        style="width: 200px"
        v-model="ruleForm.title"
      >
        <a-select-option
          v-for="item in selectList"
          :key="item.id"
          :value="item.title"
        >
          {{ item.title }}
        </a-select-option>
      </a-select>
    </a-form-model-item>

    <a-form-model-item label="Age" prop="age">
      <!-- <a-input v-model.number="ruleForm.age" /> -->
      <a-select
        show-search
        placeholder="Select a person"
        option-filter-prop="children"
        style="width: 200px"
        v-model="ruleForm.age"
      >
        <a-select-option
          v-for="item in dataList"
          :key="item.id"
          :value="item.title"
        >
          {{ item.title }}
        </a-select-option>
      </a-select>
    </a-form-model-item>

    <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
      <a-button type="primary" @click="submitForm('ruleForm')">
        Submit
      </a-button>
      <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">
        Reset
      </a-button>
    </a-form-model-item>
  </a-form-model>
</template>
<script>
export default {
  data() {
    let checkAge = (rule, value, callback) => {
       if (value === '') {
        callback(new Error('不能为空'));
      } else {
        callback();
      }
    };
// :rules="rules"  表单验证规则(model)   validator 自定义校验  被校验的表单项 prop 值,校验是否通过,错误消息(如果存在)
    // prop="title"  放在每一项中的
     // show-search   设置可以查询下拉列表的数据
     
    return {
      dataList: [
        {
          title: "张三",
          id: "1",
        },
        {
          title: "李四",
          id: "2",
        },
        {
          title: "王五",
          id: "3",
        },
        {
          title: "赵六",
          id: "4",
        },
      ],
      selectList: [
        {
          title: "篮球",
          id: "1",
        },
        {
          title: "足球",
          id: "2",
        },
        {
          title: "橄榄球",
          id: "3",
        },
      ],
      ruleForm: {
        age: "",
        title: "",
      },
      rules: {
        age:[{ validator: checkAge, trigger: "change" }],
        title:[{ validator: checkAge, trigger: "change" }],
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 },
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log(this.ruleForm,'ruleForm')
      this.$refs[formName].validate((valid) => {
        if (valid) {
           return false;
        } 
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

效果图

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值