yiminghe / async-validator

validate form asynchronous
MIT License
8.9k stars 764 forks source link

我想做滚动到未通过的节点,怎么获取全部未通过的节点? #99

Open guohuihot opened 6 years ago

zbxuan commented 6 years ago

同问,滚动到未通过的节点会更人性化.

guohuihot commented 6 years ago

发下我的业务代码,已经实现了,仅供参考

ajaxPost(form, ajxopt, scrollRef) {
            return new Promise((resolve, reject) => {
                // 切换验证规则,用于存草稿
                // this.switchRules()
                this.$refs[form].validate((valid, objs) => {
                    if (valid) {
                        ajxopt.method = 'post'
                        this.ajax(ajxopt)
                            .then((res) => {
                                if (res.code==1) {
                                    this.$message({
                                        message: '提交成功!!!',
                                        type: 'success',
                                        duration: 1000,
                                        onClose: () => {
                                            resolve(res)
                                        }
                                    })
                                }
                            })
                            .catch()
                    } else {
                        reject()
                        this.scrollTop(scrollRef, objs)
                    }
                    // this.switchRules(true)
                })
            })
        },
        getOffsetTop( elem ) {
            var location = 0;
            if (elem.offsetParent) {
                do {
                    location += elem.offsetTop;
                    elem = elem.offsetParent;
                } while (elem);
            }
            return location >= 0 ? location : 0;
        },
        scrollTop(scrollRef = 'body', objs) {
            let scrollEl = scrollRef == 'body' ?
                            document.querySelector('body') :
                            this.$refs.dialog.$el
            let item = document.querySelector(`[for="${Object.keys(objs)[0]}"]`)

            scroll.top(scrollEl, this.getOffsetTop(item))
            // scrollEl.scrollTop = 0
            // scrollEl.scrollTop = this.getOffsetTop(item)
        },

其中scroll是第三方的插件自己安装下 https://github.com/michaelrhodes/scroll