当前位置:网站首页>Elemetn form control --- automatically locate the position of the field when it is submitted without passing the verification field
Elemetn form control --- automatically locate the position of the field when it is submitted without passing the verification field
2022-04-21 15:14:00 【Mercury_】
Preface
Recently in use element A problem was found in the form component in the framework , When there are many form field items , When the user clicks submit , If the required fields at the top of the form are not filled in , The verification prompt information will only be displayed below this field item , But for users who have scrolled to the bottom Click to submit , The page is quite unresponsive , The user experience is very bad , So when we click Submit , The form does not need to be positioned to the verification position automatically .
Before optimization :

After optimization :

Realize the idea :
1. First, we need to add a parameter to the submission event
object;
2. When the verification fails, collect the prompt information of all failed field items , It throughpushMethod is added to the defined array (str);
3. adoptthis.$refs[Object.keys(object)[0]]Get the prompt that the first field fails the verification ;
4. utilizeObject.prototype.toString.call()Methods to judgethis.$refs[Object.keys(object)[0]]Whether the type of is in the string form of the object ;
5. Finally throughscrollIntoViewMethod to scroll to the first field item that does not pass the verification .
The code is also very simple , as follows :
methods: {
// Event method
submitForm(formName) {
this.$refs[formName].validate((valid, object) => {
if (valid) {
// Verification is done by executing logic
} else {
let str = [];
for (let key in object) {
object[key].map((item) => {
str.push(item.message);
});
let dom = this.$refs[Object.keys(object)[0]];
if (Object.prototype.toString.call(dom) !== "[object Object]") {
dom = dom[0];
break;// End the statement and jump out of the statement , Proceed to the next statement execution
}
// Location code
dom.$el.scrollIntoView({
block: "center",
behavior: "smooth",
});
}
// The page prompt fails to check the field item , And separated by commas
this.$message.error(str.join(","));
}
});
},
},
Be careful : Required fields el-form-item Need to add ref , It is mainly used to locate (ref Recommended values and prop The same name ).

版权声明
本文为[Mercury_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507082881.html
边栏推荐
- <译文>设置Prometheus并将其与Grafana集成以进行监控
- 汉诺塔游戏与递归
- 【二分搜索-简单】LCP 28. 采购方案
- 【二分查找-简单】278. 第一个错误的版本
- Practical application of MySQL database (I)
- Design and implementation of public welfare website based on JSP
- Software testing (III) p51-p104 software test case methods and defects
- SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转试读版
- 数字孪生坦克作战,科技推动战场信息数据化
- lightGBM专题2:基于pyspark在spark平台下lightgbm训练详解
猜你喜欢
随机推荐
登录重构小记
[JS] urlsearchparams object (upload parameters to URL in the form of object)
Cousin node-c language of binary tree
Mysql8.0以上重置初始密码的方法
Tutoriel de développement d'applications SAP ui5 70 - Comment utiliser un contrôle de bouton pour déclencher un saut de routage de page
2的0次方为什么等于1?
【二分查找-简单】69. x 的平方根
JD cloud has launched cloud computing to create an unbounded office experience for the future
数字化时代,企业运维面临现状及挑战分析解读
VMware Horizon 8 2111 部署系列(十三)创建应用程序池
【JS】URLSearchParams 对象(以对象的形式上传参数到url)
关于Signal Catcher线程中对线程的理解
干货 | 移动端App自动化之App控件定位
突然掉电,为啥MySQL也不会丢失数据?(收藏)
lightGBM专题4:pyspark平台下lightgbm模型保存
SQL性能优化以及性能测试
In the digital age, how can SaaS software become a light cavalry replaced by localization?
Lightgbm topic 4: lightgbm model saving under pyspark platform
[C language] C language standard library (super complete)
干货 | 环境问题还是测试的老大难?两个步骤轻松搞定



![[introduction to machine learning] - Lesson 6 - Bayesian classifier](/img/c2/e6e173d4efd903e206eb0a0a677982.png)





