Home > Web Front-end > JS Tutorial > body text

Introduction to the method of creating an interval selection component based on element

不言
Release: 2018-09-07 15:01:01
Original
1763 people have browsed it

This article brings you an introduction to the method of creating an interval selection component based on element. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In the company's system, product managers often require that a certain field be set to an interval threshold or used as a filtering condition during design. But because there is no very suitable component in element (the slider component does not support settings on both ends), I built one myself.

Achievements

The final display effect is as follows:

Introduction to the method of creating an interval selection component based on element

Requirements

Taking the project requirements as an example, the basic requirements are as follows:

  1. is divided into left and right values, including left and right values, positive integers

  2. The left side must be greater than or equal to 1, the right side must not be greater than 100000, the right side value must not be less than the left side

  3. Default: 20 on the left side, 100000 on the right side, both are required

  4. Out of focus verification

The page and form verification structures are the same:

<el-form>
    <el-form-item>
        <el-input></el-input>
    </el-form-item>
    ~
    <el-form-item>
        <el-input></el-input>
    </el-form-item>
</el-form>
Copy after login

Main idea

  1. Single form verification: required item verification, positive integer verification, interval verification

  2. Association verification: the right threshold must not be less than the left threshold

According to the above idea, the verification of a single form belongs to the public verification method, and the associated verification needs to be verified separately (because the comparison objects are different and the prompts are different), so in the custom verification The following definitions are included in the verification:

rules: {
    min: [
        { required: true, message: '必填项,请维护', trigger: 'blur' },
        { validator: this.validateCom, trigger: 'blur' },
        { validator: this.validateMin, trigger: 'blur' },
    ],
    max: [
        { required: true, message: '必填项,请维护', trigger: 'blur' },
        { validator: this.validateCom, trigger: 'blur' },
        { validator: this.validateMax, trigger: 'blur' },
    ],
},
Copy after login

Public verification methods: positive integer verification, interval verification

validateCom(rule, value, callback) {
    const one = Number(value);
    if (Number.isInteger(one)) {
        if (one  MAX_NUMBER) {
            return callback(new Error(`输入值必须小于${MAX_NUMBER}`));
        }
        return callback();
    }
    return callback(new Error('输入值必须为正整数'));
},
Copy after login

Note: The input output is always a string type , need to be converted into numbers for comparison

Association verification:

validateMin(rule, value, callback) {
    const one = Number(value);
    const max = Number(this.form.max);
    if (!max || one  min) {
        return callback();
    }
    return callback(new Error('输入值不得小于最小阈值'));
},
Copy after login

Probably, you will think, isn’t this the end! so easy! Now the real pitfalls begin

Filling the pits (key points)

According to the above writing, the basic functions of the component are realized, but there is a pitfall! As follows:

Introduction to the method of creating an interval selection component based on element

Obviously, the value on the left is smaller than the value on the right, but the verification prompt still reports an error. The reason is still a problem of association verification. Since it is an associated verification, when one of them is changed, both should be re-verified. It's very simple. When the input changes, won't it be OK to re-verify the form?

handleChange() {
    this.$refs.form.validate();
}
Copy after login

The actual performance is as we expected, but when we open the console, we will seeUncaught (in promise) false, what the hell is this? As an excellent front-end engineer, you will never allow errors to be reported in your code, even if it does not affect the normal process.

After verification: Promise reports an error, and Uncaught means that there is a reject state that has not been caught. The reason is that when a value is changed and the entire form is verified, the changed input will be verified twice, causing an exception.

Finally make the following modifications:

handleMinChange() {
    this.$refs.form.validateField('max');
},
handleMaxChange() {
    this.$refs.form.validateField('min');
},

// 并对外暴露操作方法
getFormData() {
    const ret = {};
    this.$refs.form.validate((valid) => {
        ret.valid = valid;
        ret.form = this.form;
    });
    return ret;
},
resetForm() {
    this.$refs.form.resetFields();
},
Copy after login

Summary

  1. The input form output value is String type, even if type=number## is set

  2. #The associated items need to be verified during association verification and cannot be verified repeatedly

Related recommendations:

Introduction to the usage of jQuery selector element

Introduction to the use of VUE-region selector (V-Distpicker) component

The above is the detailed content of Introduction to the method of creating an interval selection component based on element. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!