欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vant picker 多級(jí)聯(lián)動(dòng)操作

 更新時(shí)間:2020年11月02日 14:28:44   作者:Posden  
這篇文章主要介紹了Vant picker 多級(jí)聯(lián)動(dòng)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

官網(wǎng)地址:鏈接

官網(wǎng)文檔可能不是很完善,但仔細(xì)看文檔,方法,類(lèi)型其實(shí)都講到的。

度娘也沒(méi)有找到,花了大半天爬坑試錯(cuò)。

搭配彈出層使用

<van-field readonly clickable placeholder="選擇城市" :value="station" @click="showPicker = true" />

<van-popup v-model="showPicker" position="bottom">
<van-picker show-toolbar :columns="columns" @cancel="showPicker = false" @confirm="onConfirm" @change="onChange" />
</van-popup>
const citys = [ // 我們習(xí)慣的格式。 可以后臺(tái)給你出,如果你打不過(guò)的話,你就。。。
 {
  text: "測(cè)試一", // 默認(rèn)識(shí)別text標(biāo)簽
  id: 1,
  children: [
   {
    id: 11,
    text: "測(cè)試1-1",
    children: [
     {
      id: 111,
      text: "測(cè)試1-1-1"
     },
     {
      id: 112,
      text: "測(cè)試1-1-2"
     }
    ]
   },
   {
    id: 12,
    text: "測(cè)試1-2",
    children: [
     {
      id: 122,
      text: "測(cè)試1-2-1"
     },
     {
      id: 122,
      text: "測(cè)試1-2-2"
     }
    ]
   },
   {
    id: 13,
    text: "測(cè)試1-3"
   }
  ]
 },
 {
  text: "測(cè)試二",
  id: 2,
  children: [
   {
    id: 21,
    text: "測(cè)試2",
    children: [
     {
      id: 221,
      text: "測(cè)試2-2-1"
     },
     {
      id: 222,
      text: "測(cè)試2-2-2"
     }
    ]
   },
   {
    id: 22,
    text: "測(cè)試2"
   },
   {
    id: 23,
    text: "測(cè)試2"
   }
  ]
 },
 {
  text: "測(cè)試三",
  id: 3,
  children: [
   {
    id: 31,
    text: "測(cè)試3",
    children: [
     {
      id: 311,
      text: "測(cè)試3-1-1"
     },
     {
      id: 312,
      text: "測(cè)試3-3-2"
     }
    ]
   },
   {
    id: 32,
    text: "測(cè)試3"
   },
   {
    id: 33,
    text: "測(cè)試3"
   }
  ]
 }
];

 data(){
  return {
   station: "",
   showPicker: false,
   columns: [
    {
     values: citys, // 設(shè)置的頁(yè)面初始值
     className: "column1"
    },
    {
     values: citys[0].children,
     className: "column2"
    },
    {
     values: citys[0].children[0].children,
     className: "column3"
    }
   ],
 } 
 }; 

  onConfirm(value) {
   const compact = arr => arr.filter(Boolean); // 三級(jí)聯(lián)動(dòng) 去除沒(méi)值的,比如只有兩級(jí)
   const result = compact(value);
   let str = ""; // 呈現(xiàn)頁(yè)面顯示 /xxx/xxx/xxx
   result.forEach(item => {
    str += "/" + item.text;
   });
   this.station = str;
   this.showPicker = false;
   
  const end = result[result.length - 1]; // 一般都是取最后一個(gè)id給后臺(tái)的
   const id = end.id;
   console.log(id);
  },
  onChange(picker, values, index) { // 實(shí)在不行自己打印測(cè)試
   if (index == 0) {
    picker.setColumnValues(2, []); // 防止突然選中第一個(gè),第二個(gè)是更新的,但第三個(gè)聯(lián)級(jí)不更新,我暫時(shí)強(qiáng)制清空
   }
   let ColumnIndex = picker.getColumnIndex(index);
   console.log("第" + index + "列", "第" + ColumnIndex + "個(gè)");
   picker.setColumnValues(index + 1, values[ColumnIndex ].children || []);//點(diǎn)當(dāng)前列更新下一列數(shù)據(jù),防止undefined 
   
 // 當(dāng)然,如果后臺(tái)數(shù)據(jù)不給你想要的格式,你就按需請(qǐng)求一次了,比如選中第一個(gè),取id請(qǐng)求接口,更新下一列。畢竟連動(dòng)內(nèi)容多的話,頁(yè)面請(qǐng)求也多。但頁(yè)面就不流暢,比如第一列第二列初始值。 
  // 我就是打不過(guò)后臺(tái)。。。
  }

補(bǔ)充知識(shí):【vant】配合 van-popup 使用 van-picker 多級(jí)聯(lián)動(dòng),回顯賦默認(rèn)值 遇到的坑及解決方案

先吐槽一句,van-picker 真心不怎么好用。。。

頁(yè)面大概是這個(gè)樣子:

代碼結(jié)構(gòu)大概是這個(gè)樣子:

<!-- 選擇 收支類(lèi)型彈窗 -->
<van-popup ref = "accountTypePopup" v-model="showPicker" position="bottom">
  <van-picker
    ref = "accountTypePopup2"
    show-toolbar
    :columns="columns"
    @cancel="showPicker = false"
    @confirm="onConfirm"
    @change="onChange"
    
  />
</van-popup>
methods: {
  // ...
  // 修改 columns 方法
  changeColumns(p_name, name) {
    // p_name 一級(jí)分類(lèi)回顯值
    // name 二級(jí)分類(lèi)回顯值
    
    // 類(lèi)型列表
    var typeList =
      this.tabActive === 0
        ? this.expendTypeList
        : this.incomeTypeList;
 
    // 設(shè)置 收支類(lèi)型選項(xiàng) columns 的默認(rèn)值 和 子選項(xiàng)
    this.columns[0]["defaultIndex"] = this.columns[0][
      "values"
    ].findIndex(item => item === p_name);
 
    this.columns[1]["values"] = typeList[p_name];
 
    this.columns[1]["defaultIndex"] = this.columns[1][
      "values"
    ].findIndex(item => item === name);
  }
}

期望是:popup彈出后,picker選中用戶已經(jīng)選中的選項(xiàng)

結(jié)果是:僅第一次popup彈出后,picker選中用戶已經(jīng)選中的選項(xiàng),之后的彈出一直展示第一次的列表

查看文檔,解決方案是用van-picker的方法:

坑就坑中,組件嵌套(popup套picker),用ref獲取不到 picker 實(shí)例

咋整?

用調(diào)試工具調(diào)試了半天發(fā)現(xiàn),picker實(shí)例化成DOM元素后,即使隱藏,也僅僅是display:none,不會(huì)重新實(shí)例化

那就好辦了。。。

<!-- 選擇 收支類(lèi)型彈窗 -->
<van-popup ref = "accountTypePopup" v-model="showPicker" position="bottom">
  <van-picker
    ref = "accountTypePopup2"
    show-toolbar
    :columns="columns"
    @cancel="showPicker = false"
    @confirm="onConfirm"
    @change="onChange"
 
    // 這里是新加的 //
    :key = "account_type_value"
    
  />
</van-popup>

添加一個(gè)key屬性,值為【一級(jí)項(xiàng)+二級(jí)項(xiàng)】,問(wèn)題圓滿解決!!!

以上這篇Vant picker 多級(jí)聯(lián)動(dòng)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論