最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解element-ui 組件el-autocomplete使用踩坑記錄

 更新時(shí)間:2022年03月08日 09:43:47   作者:石耳  
最近使用了el-autocomplete組件,本文主要介紹了element-ui 組件el-autocomplete使用踩坑記錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

項(xiàng)目遇到一個(gè)比較麻煩的需求,保存用戶填寫的歷史記錄,項(xiàng)目使用的element-ui,自然就使用了el-autocomplete組件,然后就是各種踩坑,以下記錄以下寫代碼過程中遇到的問題

createFilter(queryString, filed) {
      console.log("createFilter==" + queryString)
      return (item) => {
        switch (filed) {
          case 'cardNum':
            break
          case 'cardPass': case 'userPhone': case 'userName': case 'userCardId':
            return (item.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
            break
          case 'mobile':
            return (item.phone.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
            break
          default:
            break
        }
      };
    },

1、下拉框高度修改:

el-autocomplete組件樣式默認(rèn)有一個(gè)最大高度,超出高度會滾動,如圖顯示的大概有7、8條的數(shù)據(jù),需求要求展示10條所以只能修改組件樣式。

如圖可以看到有一個(gè)max-height屬性,修改這個(gè)樣式就可了,但是寫了樣式代碼不起作用,組件修改了:popper-append-to-body="false"之后樣式起作用了,原理不太清楚,哪位大神知道原因的麻煩告訴我一下,多謝

<style scoped lang="less">

  /deep/ .inline-input {
    /deep/ .el-scrollbar__wrap {
      max-height: 360px; /*no*/
    }
  }
</style>

2、一個(gè)頁面有多個(gè)el-autocomplete組件,所以我需要傳入使用這個(gè)組件的字段名,然后修改fetch-suggestions方法為

:fetch-suggestions="((queryString, cb)=>{queryFun(queryString, cb,'userName')})用閉包的方式多傳入一個(gè)字段的入?yún)?/p>

<el-form-item label="使用人姓名:" prop="userName" :rules="[{ required: true, message: '請?zhí)顚懶畔ⅲ?,trigger: ['blur', 'change'] },
    { pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,20}$/, message: '請?zhí)顚懻_信息!',trigger: ['blur', 'change'] }]">
    <!-- <el-input @input="filterInput($event,'userName')" v-model="form.userName" autocomplete="off" maxlength="20"></el-input> -->
    <el-autocomplete @blur.stop="saveLocal($event.target.value,'userName')"     @input="filterInput($event,'userName')" class="inline-input" v-model="form.userName"     :fetch-suggestions="((queryString, cb)=>{queryFun(queryString, cb,'userName')})"     placeholder="請輸入姓名" @select="((item)=>{handleSelect(item,'userName')})" :debounce=0 :popper-append-to-body="false">
  </el-autocomplete>
?</el-form-item>

3、需要保存的數(shù)據(jù)是校驗(yàn)通過后的數(shù)據(jù),也就是輸入完成blur之后再操作保存的邏輯,但是el-autocomplete組件blur事件不起作用,后查詢資料說是因?yàn)閏lick事件的優(yōu)先級高于blur事件,所以要解決這個(gè)問題就解決事件冒泡問題,只需要使用vue的事件修飾符.stop就可以了,????乛?乛????

@blur.stop="saveLocal($event.target.value,'userName')"

queryFun(queryString, cb, filed) {
      console.log(filed)
      let items = [];
      switch (filed) {
        case 'cardNum':
          break
        case 'cardPass':
          items = JSON.parse(localStorage.getItem("passHistory")) ? JSON.parse(localStorage.getItem("passHistory")) : [];
          break
        case 'userPhone':
          items = JSON.parse(localStorage.getItem("phoneHistory")) ? JSON.parse(localStorage.getItem("phoneHistory")) : [];
          break
        case 'userName':
          items = JSON.parse(localStorage.getItem("userNameHistory")) ? JSON.parse(localStorage.getItem("userNameHistory")) : [];
          break
        case 'mobile':
          break
        case 'userCardId':
          items = JSON.parse(localStorage.getItem("userCardIdHistory")) ? JSON.parse(localStorage.getItem("userCardIdHistory")) : [];
          break
        default:
          break
      }

      let results = queryString ? items.filter(this.createFilter(queryString, filed)) : items;

      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    }
createFilter(queryString, filed) {
      console.log("createFilter==" + queryString)
      return (item) => {
        switch (filed) {
          case 'cardNum':
            break
          case 'cardPass': case 'userPhone': case 'userName': case 'userCardId':
            return (item.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
            break
          default:
            break
        }
      };
    },

4、第一次點(diǎn)擊的時(shí)候會顯示“請?zhí)顚懶畔ⅲ?rdquo;報(bào)錯(cuò)信息,再次點(diǎn)擊選項(xiàng)就正常了,這個(gè)問題也是糾結(jié)了一陣兒,后來想起我的校驗(yàn)規(guī)則trigger 寫的是‘blur’,下拉框應(yīng)該觸發(fā)change事件,修改為

trigger: ['blur', 'change'],解決

handleSelect(item, filed) {
      console.log(item);
      console.log("handleSelect==" + filed);
      let _this = this;
      switch (filed) {
        case 'cardNum':
          break
        case 'cardPass': case 'userPhone': case 'userName':
          _this.$set(_this.form, filed, item.value);
          break
        case 'userCardId':
          this.form.userCardId = item.userCardId;
          this.getAge(item.userCardId);
          break
        default:
          break
      }
    },

5、姓名禁止輸入數(shù)字等使用input事件,將不符合正則的內(nèi)容替換成空,代碼如下

filterInput(e, filed) {
      console.log("filterInput=====")
      console.log(e)
      switch (filed) {
        case 'cardNum':
          this.form[filed] = e.replace(/[^\a-\z\A-\Z0-9]/g, '');
          this.form[filed] = this.form[filed].slice(0, 12);
          break
        case 'cardPass':
          this.form[filed] = e.replace(/[^\a-\z\A-\Z0-9]/g, '');
          this.form[filed] = this.form[filed].slice(0, 6);
          break
        case 'userPhone':
          this.form[filed] = e.replace(/[^0-9]/g, '');
          this.form[filed] = this.form[filed].slice(0, 11);
          break
        case 'userName':
          this.form[filed] = e.replace(/[^\a-\z\A-\Z\u4e00-\u9fa5]/g, '');
          this.form[filed] = this.form[filed].slice(0, 20);
          break
        case 'mobile':
          this.form[filed] = e.replace(/[^0-9]/g, '');
          this.form[filed] = this.form[filed].slice(0, 11);
          break
        case 'userCardId':
          this.form[filed] = e.replace(/[^0-9Xx]/g, '');
          this.form[filed] = this.form[filed].slice(0, 18);
          break
        default:
          this.form[filed] = e.replace(/[\u4e00-\u9fa5]/g, '');
          break
      }
      // this.saveLocal(this.form[filed], filed)
      this.$forceUpdate();
    },

6、校驗(yàn)通過后存儲到localStorage,總覺得代碼有點(diǎn)重復(fù),不過改bug比較著急,大神有更好的寫法,歡迎評論區(qū)留言

// 保存驗(yàn)證通過的信息
    saveLocal(val, filed) {
      var reg = "";
      switch (filed) {
        case 'cardNum':
          reg = /[0-9a-zA-Z]{12}/;
          if (reg.test(val)) {

            // 存儲正確卡號到歷史信息
            this.cardHistory.unshift({ "cardNum": val });
            // 歷史消息去重
            var hash = {};
            this.cardHistory = this.cardHistory.reduce(function (item, next) {
              hash[next.cardNum] ? '' : hash[next.cardNum] = true && item.push(next);
              return item
            }, []);
            if (this.cardHistory.length > 10) {
              this.cardHistory.pop();
            }
            localStorage.setItem("cardList", JSON.stringify(this.cardHistory));
          }
          break
        case 'cardPass':
          reg = /[0-9a-zA-Z]{6}/;
          if (reg.test(val)) {
            // 存儲正確卡號到歷史信息
            this.passHistory.unshift({ "value": val });
            // 歷史消息去重
            var hash = {};
            this.passHistory = this.passHistory.reduce(function (item, next) {
              hash[next.value] ? '' : hash[next.value] = true && item.push(next);
              return item
            }, []);
            if (this.passHistory.length > 10) {
              this.passHistory.pop();
            }
            localStorage.setItem("passHistory", JSON.stringify(this.passHistory));
          }
          break
        case 'userPhone':
          reg = /^1[3-9]\d{9}$/;
          if (reg.test(val)) {
            // 存儲正確卡號到歷史信息
            this.phoneHistory.unshift({ "value": val });
            // 歷史消息去重
            var hash = {};
            this.phoneHistory = this.phoneHistory.reduce(function (item, next) {
              hash[next.value] ? '' : hash[next.value] = true && item.push(next);
              return item
            }, []);
            if (this.phoneHistory.length > 10) {
              this.phoneHistory.pop();
            }
            localStorage.setItem("phoneHistory", JSON.stringify(this.phoneHistory));
          }
          break
        case 'userName':
          reg = /^[a-zA-Z\u4e00-\u9fa5]{2,20}$/;
          if (reg.test(val)) {
            // 存儲正確卡號到歷史信息
            this.userNameHistory.unshift({ "value": val });
            // 歷史消息去重
            var hash = {};
            this.userNameHistory = this.userNameHistory.reduce(function (item, next) {
              hash[next.value] ? '' : hash[next.value] = true && item.push(next);
              return item
            }, []);
            if (this.userNameHistory.length > 10) {
              this.userNameHistory.pop();
            }
            localStorage.setItem("userNameHistory", JSON.stringify(this.userNameHistory));
          }
          break
        case 'mobile':
          break
        case 'userCardId':
          reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
          if (reg.test(val)) {
            // 存儲正確卡號到歷史信息
            this.userCardIdHistory.unshift({ "value": val });
            // 歷史消息去重
            var hash = {};
            this.userCardIdHistory = this.userCardIdHistory.reduce(function (item, next) {
              hash[next.value] ? '' : hash[next.value] = true && item.push(next);
              return item
            }, []);
            if (this.userCardIdHistory.length > 10) {
              this.userCardIdHistory.pop();
            }
            localStorage.setItem("userCardIdHistory", JSON.stringify(this.userCardIdHistory));
          }
          break
        default:
          break
      }
    },

到此這篇關(guān)于詳解element-ui 組件el-autocomplete使用踩坑記錄的文章就介紹到這了,更多相關(guān)element組件el-autocomplete使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue 接口請求地址前綴本地開發(fā)和線上開發(fā)設(shè)置方式

    vue 接口請求地址前綴本地開發(fā)和線上開發(fā)設(shè)置方式

    這篇文章主要介紹了vue 接口請求地址前綴本地開發(fā)和線上開發(fā)設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Vue實(shí)現(xiàn)無限加載瀑布流

    Vue實(shí)現(xiàn)無限加載瀑布流

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)無限加載瀑布流,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Vue3實(shí)現(xiàn)表單輸入的雙向綁定的示例代碼

    Vue3實(shí)現(xiàn)表單輸入的雙向綁定的示例代碼

    Vue.js 是一個(gè)漸進(jìn)式的JavaScript框架,廣泛用于構(gòu)建交互式用戶界面,在 Vue 中,雙向綁定是一項(xiàng)非常核心的概念,尤其是在處理表單輸入時(shí),它使得數(shù)據(jù)和用戶界面之間的互動變得簡單又高效,在本篇博客中,我們將深入探討如何在 Vue 3 中實(shí)現(xiàn)表單輸入的雙向綁定
    2025-01-01
  • vue.js加載新的內(nèi)容(實(shí)例代碼)

    vue.js加載新的內(nèi)容(實(shí)例代碼)

    vue是一種輕巧便捷的框架,那么如何進(jìn)行對于數(shù)據(jù)加載的刷新呢?以下就是我對于vue.js數(shù)據(jù)加載的一點(diǎn)想法
    2017-06-06
  • vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參

    vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參

    這篇文章主要介紹了vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問題解決方案

    v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問題解決方案

    在使用 elementui 表單的過程中,某些表單項(xiàng)需要通過 v-if 來判斷是否展示,但是這些表單項(xiàng)出現(xiàn)了檢驗(yàn)失效的問題,今天小編給大家介紹v-if 導(dǎo)致 elementui 表單校驗(yàn)失效問題解決方案,感興趣的朋友一起看看吧
    2024-01-01
  • 淺析Vue中Virtual?DOM和Diff原理及實(shí)現(xiàn)

    淺析Vue中Virtual?DOM和Diff原理及實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Vue中Virtual?DOM和Diff原理及實(shí)現(xiàn)的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • Vue3中watch無法監(jiān)聽的解決辦法

    Vue3中watch無法監(jiān)聽的解決辦法

    本文主要介紹了Vue3中watch無法監(jiān)聽的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • vue中使用v-if隱藏元素時(shí)會出現(xiàn)閃爍問題的解決

    vue中使用v-if隱藏元素時(shí)會出現(xiàn)閃爍問題的解決

    這篇文章主要介紹了vue中使用v-if隱藏元素時(shí)會出現(xiàn)閃爍問題的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能

    使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能

    這篇文章主要介紹了使用Element實(shí)現(xiàn)表格表頭添加搜索圖標(biāo)和功能,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評論

庆阳市| 凌源市| 肥西县| 广丰县| 高州市| 安岳县| 封丘县| 灵璧县| 合江县| 土默特右旗| 涪陵区| 灌云县| 红原县| 阜新市| 望都县| 安远县| 德清县| 和龙市| 新密市| 千阳县| 绥德县| 商城县| 东辽县| 南投市| 平乡县| 焦作市| 太原市| 金乡县| 德清县| 元朗区| 安新县| 宝清县| 德江县| 石景山区| 瓮安县| 鄂伦春自治旗| 长兴县| 丰顺县| 右玉县| 建湖县| 长葛市|