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

vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式

 更新時(shí)間:2024年07月02日 08:36:59   作者:Fighting寧  
這篇文章主要介紹了vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用vant的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)

需求

因?yàn)槿珖?guó)地區(qū)數(shù)據(jù)太多,如果要一次加載出來,再顯示頁面會(huì)比較慢。所以通過接口點(diǎn)擊獲取當(dāng)前的數(shù)據(jù)

需要注意的點(diǎn)

  • 后臺(tái)接口在返回?cái)?shù)據(jù)時(shí),如果有下一級(jí)的數(shù)據(jù),要讓后臺(tái)返回children,如果不返回,控件就會(huì)出現(xiàn)關(guān)閉彈框無法點(diǎn)擊下一級(jí)的bug(控件是根據(jù)是否有children來判斷是否要繼續(xù)點(diǎn)擊的,前端是無法知道是否存在下一級(jí)數(shù)據(jù)的)
  • 添加數(shù)據(jù)的3種方案任選一種就可以,推薦第一種,無論多少層級(jí)都可以添加上。第二種只判斷了4級(jí)的添加,5級(jí)數(shù)據(jù)添加不上,第三種是遞歸的方式添加數(shù)據(jù)
<van-cascader v-model="cascaderValue" title="請(qǐng)選擇所在地區(qū)" :options="options" @close="areaShow = false" @finish="onFinish" :field-names="fieldNames" @change="onChangeArea" />

需要用到的data中的變量

export default {
  data() {
    return {
      areaShow: false,
      cascaderValue: '',
      fieldNames: {
        text: 'name',
        value: 'id',
        children: 'children'
      },
      // 選項(xiàng)列表,children 代表子選項(xiàng),支持多級(jí)嵌套
      options: [],
      divisionIds: '', // 地區(qū)的id
      divisionNames: '', // 地區(qū)的名字
    }
  }, 

第一種方案

比較簡(jiǎn)單,vant中觸發(fā)本身的change事件,可以拿到當(dāng)前點(diǎn)擊的元素,以及它的上層元素,我們只需要把請(qǐng)求到的最新數(shù)據(jù),加在最里面的數(shù)據(jù)結(jié)構(gòu)中即可

methods: {
    // 級(jí)聯(lián)數(shù)據(jù)全部選項(xiàng)選擇完畢后,會(huì)觸發(fā) finish 事件
    onFinish({ selectedOptions }) {
      this.divisionNames = selectedOptions.map(option => option.name).join('/')
      this.divisionIds = selectedOptions.map(option => option.id).join(',')
      this.areaShow = false
    },
    // 從接口請(qǐng)求獲取第一層的數(shù)據(jù),---比如北京
    async getAreaList() {
      let id = ''
      let res = await getAreaList(id)
      res.data.forEach(item => {
        this.options.push({
          name: item.name,
          id: item.id,
          children: item.children || null// 這個(gè)很關(guān)鍵
        })
      })
    },
    onChangeArea({ value, selectedOptions, tabIndex }) {
      // 需要后臺(tái)接口返回children數(shù)據(jù)
      // 拿到數(shù)據(jù)后,動(dòng)態(tài)添加
      getAreaList(value).then(res => {
      	// 第一種方案
        this.addTree(selectedOptions, res.data, value)
      })
    },
    addTree(selectedOptions, children, id) {
      selectedOptions.forEach(item => {
        if (item.id === id) {
          item.children = children
        }
      })
    }
  }
}

第二種方案:不推薦

methods: {
    // 級(jí)聯(lián)數(shù)據(jù)全部選項(xiàng)選擇完畢后,會(huì)觸發(fā) finish 事件
    onFinish({ selectedOptions }) {
      this.divisionNames = selectedOptions.map(option => option.name).join('/')
      this.divisionIds = selectedOptions.map(option => option.id).join(',')
      this.areaShow = false
    },
    // 從接口請(qǐng)求獲取第一層的數(shù)據(jù),---比如北京
    async getAreaList() {
      let id = ''
      let res = await getAreaList(id)
      res.data.forEach(item => {
        this.options.push({
          name: item.name,
          id: item.id,
          children: item.children || null// 這個(gè)很關(guān)鍵
        })
      })
    },
    onChangeArea({ value, selectedOptions, tabIndex }) {
      // 需要后臺(tái)接口返回children數(shù)據(jù)
      // 拿到數(shù)據(jù)后,動(dòng)態(tài)添加
      getAreaList(value).then(res => {
        // 第二種方案
          if (tabIndex === 0) {
          let index = this.options.findIndex(item => item.id === value)
          this.options[index].children = res.data
          // this.$set(this.options[index], 'children', res.data)
        } else if (tabIndex === 1) {
          let firstIndex = this.options.findIndex(item => item.id === selectedOptions[0].id) // 省級(jí) index
          let cities = this.options[firstIndex].children // 所有城市
          let index = cities.findIndex(item => item.id === value) // 市級(jí) index
          cities[index].children = res.data
          // this.$set(this.options[firstIndex].children[index], 'children', res.data)
        } else if (tabIndex === 2) {
          let firstIndex = this.options.findIndex(item => item.id === selectedOptions[0].id) // 省級(jí) index
          let cities = this.options[firstIndex].children // 所有城市
          let secondIndex = cities.findIndex(item => item.id === selectedOptions[1].id) // 市級(jí) index
          let areas = cities[secondIndex].children // 城市下的城區(qū)
          let index = areas.findIndex(item => item.id === value) // 城區(qū) index
          areas[index].children = res.data
          // this.$set(this.options[firstIndex].children[secondIndex].children[index], 'children', res.data)
          }
      })
    },
}

第三種方案

// 級(jí)聯(lián)數(shù)據(jù)全部選項(xiàng)選擇完畢后,會(huì)觸發(fā) finish 事件
    onFinish({ selectedOptions }) {
      this.divisionNames = selectedOptions.map(option => option.name).join('/')
      this.divisionIds = selectedOptions.map(option => option.id).join(',')
      this.areaShow = false
    },
    // 從接口請(qǐng)求獲取第一層的數(shù)據(jù),---比如北京
    async getAreaList() {
      let id = ''
      let res = await getAreaList(id)
      res.data.forEach(item => {
        this.options.push({
          name: item.name,
          id: item.id,
          children: item.children || null// 這個(gè)很關(guān)鍵
        })
      })
    },
    onChangeArea({ value, selectedOptions, tabIndex }) {
      // 需要后臺(tái)接口返回children數(shù)據(jù)
      // 拿到數(shù)據(jù)后,動(dòng)態(tài)添加
      getAreaList(value).then(res => {
      	// 第三種方案
         this.addTree1(res.data, value)
      })
    },
    // 遞歸寫法
    addTree1(list, value) {
      function addTree2(json, id) {
        const index = json.findIndex(ev => ev.id == id)
        if (index > -1) {
          json[index].children = list
          return
        } else {
          json.map(item => {
            if (item.children) {
              addTree2(item.children || [], value)
            }
          })
        }
      }
      addTree2(this.options, value)
    }
}
  • 第一次獲取到的數(shù)據(jù)

  • 點(diǎn)擊山東省后獲取的數(shù)據(jù)

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue實(shí)現(xiàn)牌面翻轉(zhuǎn)效果

    Vue實(shí)現(xiàn)牌面翻轉(zhuǎn)效果

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)牌面翻轉(zhuǎn)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果

    vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果

    這篇文章主要介紹了vue中實(shí)現(xiàn)左右聯(lián)動(dòng)的效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-06-06
  • Vue 3中的異步操作管理示例分析

    Vue 3中的異步操作管理示例分析

    在現(xiàn)代Web應(yīng)用開發(fā)中,異步操作是常見的挑戰(zhàn),本文介紹了Vue3中管理多個(gè)異步操作的方法,幫助開發(fā)者提高編碼效率和應(yīng)用性能,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • vscode vue3中jsconfig與tsconfig的區(qū)別詳細(xì)講解

    vscode vue3中jsconfig與tsconfig的區(qū)別詳細(xì)講解

    這篇文章主要介紹了vscode vue3中jsconfig與tsconfig區(qū)別的相關(guān)資料,jsconfig.json用于JavaScript項(xiàng)目,允許處理JS文件,tsconfig.json用于TypeScript項(xiàng)目,控制編譯選項(xiàng),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-05-05
  • Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)

    Vue?通過this.$emit()方法子組件向父組件傳值(步驟分享)

    這篇文章主要介紹了Vue?this.$emit()方法通過子組件向父組件傳值,第一步在父組件中引入子組件,第二步子組件向父組件傳值,本文通過需要的朋友可以參考下
    2022-11-11
  • 前端實(shí)現(xiàn)不同角色登入展示不同頁面效果實(shí)例

    前端實(shí)現(xiàn)不同角色登入展示不同頁面效果實(shí)例

    要實(shí)現(xiàn)不同角色登錄跳轉(zhuǎn)不同的前端頁面,可以在登錄成功后,根據(jù)用戶的角色信息,使用路由跳轉(zhuǎn)到不同的頁面,這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)不同角色登入展示不同頁面效果的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • vue滑動(dòng)吸頂及錨點(diǎn)定位的示例代碼

    vue滑動(dòng)吸頂及錨點(diǎn)定位的示例代碼

    這篇文章主要介紹了vue滑動(dòng)吸頂及錨點(diǎn)定位的示例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Vue Element前端應(yīng)用開發(fā)之組織機(jī)構(gòu)和角色管理

    Vue Element前端應(yīng)用開發(fā)之組織機(jī)構(gòu)和角色管理

    本篇文章繼續(xù)深化Vue Element權(quán)限管理模塊管理的內(nèi)容,介紹組織機(jī)構(gòu)和角色管理模塊的處理,使得我們了解界面組件化模塊的開發(fā)思路和做法,提高我們界面設(shè)計(jì)的技巧,并減少代碼的復(fù)雜性,提高界面代碼的可讀性,同時(shí)也是利用組件的復(fù)用管理。
    2021-05-05
  • Vue中@click.stop與@click.prevent、@click.native使用

    Vue中@click.stop與@click.prevent、@click.native使用

    這篇文章主要介紹了Vue中@click.stop與@click.prevent、@click.native使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue兩個(gè)通信方式與動(dòng)畫過度及混入使用介紹

    Vue兩個(gè)通信方式與動(dòng)畫過度及混入使用介紹

    最近在寫vue的一個(gè)項(xiàng)目要實(shí)現(xiàn)過渡的效果,雖然vue動(dòng)畫不是強(qiáng)項(xiàng),庫也多,但是基本的坑還是得踩扎實(shí),下面這篇文章主要給大家介紹了關(guān)于Vue中實(shí)現(xiàn)過渡動(dòng)畫效果的相關(guān)資料,需要的朋友可以參考下
    2023-03-03

最新評(píng)論

津市市| 高雄县| 手游| 曲沃县| 临邑县| 渝北区| 辽阳县| 南陵县| 中牟县| 连平县| 莱州市| 芦溪县| 上思县| 稷山县| 长治县| 兴化市| 富民县| 泉州市| 河源市| 双桥区| 广饶县| 沅江市| 雷山县| 罗定市| 阳山县| 蒙阴县| 海南省| 上高县| 北辰区| 房产| 龙陵县| 祥云县| 买车| 信宜市| 沙河市| 阳春市| 禹州市| 永济市| 桃园市| 柘城县| 新和县|