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

微信小程序自定義多列選擇器使用詳解

 更新時(shí)間:2019年06月21日 09:47:45   作者:alexsaurora  
這篇文章主要為大家詳細(xì)介紹了微信小程序自定義多列選擇器使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、預(yù)覽

微信小程序在自帶的表單組件中加入了選擇器picker,并給出了常用的時(shí)間和省市區(qū)三級(jí)聯(lián)動(dòng)選擇器,但日常開發(fā)中不可能僅僅使用這些選擇器,所以我們?cè)趯W(xué)習(xí)時(shí)先寫一個(gè)常見的自定義選擇器,用于滿足項(xiàng)目中的日常需要。
先給出效果圖:(先聲明選擇器中數(shù)據(jù)為測(cè)試使用,與真實(shí)情況無關(guān))

二、picker屬性

一個(gè)簡(jiǎn)單地多列選擇器只要給picker組件加屬性mode="multiSelector"即可,綁定數(shù)據(jù)時(shí)使用range來綁定一個(gè)數(shù)組作為顯示內(nèi)容,下面是官方給出的屬性解釋。

三、創(chuàng)建組件

我們可以先在.wxml建一個(gè)自定義picker組件:

<picker 
 mode="multiSelector" 
 bindchange="bindCustomPickerChange" 
 bindcolumnchange="bindCustomPickerColumnChange" 
 value="{{customIndex}}" 
 range="{{onlyArray}}"
 >
 <view>
  多列自創(chuàng)選擇器:{{onlyArray[0][customIndex[0]]}},{{onlyArray[1][customIndex[1]]}},{{onlyArray[2][customIndex[2]]}}
 </view>
</picker>

要注意的是,此處的onlyArray數(shù)組只是當(dāng)前顯示內(nèi)容的數(shù)組,并不是我們?nèi)繑?shù)據(jù)的數(shù)組。

四、自定義函數(shù)

寫好組件,我們就來寫js文件,思路如下:

1.先創(chuàng)建頁面所需數(shù)據(jù)

Page({

 /**
 * 頁面的初始數(shù)據(jù)
 */
 data: {
 //當(dāng)前選中數(shù)組的下標(biāo)值
 customIndex: [0, 0, 0],
 //當(dāng)前選中數(shù)組
 onlyArray: [
  [],
  [],
  []
 ],
 //customArray假設(shè)為我們從后臺(tái)獲取到的json數(shù)據(jù)
 customArray: [{
  name: '百度',
  dept: [{
   name: '搜索',
   product: [{
    name: '百度搜索'
    },
    {
    name: '百度一下'
    },
   ]
   },
   {
   name: '團(tuán)購',
   product: [{
    name: '百度糯米'
   }, {
    name: '餓了么'
   }]
   },
   {
   name: '音樂',
   product: [{
    name: '百度音樂'
   }]
   },
   {
   name: '問答社區(qū)',
   product: [{
    name: '百度貼吧'
   }]
   }
  ]
  },

  {
  name: '騰訊',
  dept: [{
   name: '社交',
   product: [{
    name: 'QQ'
    },
    {
    name: '微信'
    },
   ]
   },
   {
   name: '視頻',
   product: [{
    name: '騰訊視頻'
    },
    {
    name: '搜狐視頻'
    },
   ]
   },

   {
   name: '短視頻',
   product: [{
    name: '微視'
   }]
   }
  ]
  },
 ],
 },

2.加載頁面時(shí)給出賦值函數(shù)。

可以看到,當(dāng)前選中數(shù)組onlyArray是空的,在小程序顯示時(shí)會(huì)直接顯示成空,所以需要在頁面創(chuàng)建時(shí)給一個(gè)初始值,這個(gè)初始值使用customIndex數(shù)組來給出,也可以用于頁面數(shù)據(jù)回填。代碼如下:

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面加載
 */
 onLoad: function(options) {
 var data = {
  customArray: this.data.customArray,
  customIndex: this.data.customIndex,
  onlyArray: this.data.onlyArray,
 };
 for (var i = 0; i < data.customArray.length; i++) {
  data.onlyArray[0].push(data.customArray[i].name);
 }
 for (var j = 0; j < data.customArray[data.customIndex[0]].dept.length; j++) {
  data.onlyArray[1].push(data.customArray[data.customIndex[0]].dept[j].name);
 }
 for (var k = 0; k < data.customArray[data.customIndex[0]].dept[data.customIndex[1]].product.length; k++) {
  data.onlyArray[2].push(data.customArray[data.customIndex[0]].dept[data.customIndex[1]].product[k].name);
 }
 this.setData(data);
 },

3.創(chuàng)建組件監(jiān)聽函數(shù)。

這里需要兩個(gè)函數(shù),分別是bindchange(打開組件后點(diǎn)擊確定觸發(fā))和bindcolumnchange(打開組件后滑動(dòng)列觸發(fā))。

//多列自定義選擇器改變value的方法
 bindCustomPickerChange: function(e) {
 var customArray = this.data.customArray,
  customIndex = this.data.customIndex,
  onlyArray = this.data.onlyArray;

 console.log('picker發(fā)送選擇改變,攜帶值為', e.detail.value);
 //此處e.detail.value為當(dāng)前選擇的列的下標(biāo)值數(shù)組,如[0,1,0]
 
 console.log('picker最終選擇值為:', onlyArray[0][customIndex[0]], onlyArray[1][customIndex[1]], onlyArray[2][customIndex[2]]);
 this.setData({
  customIndex: e.detail.value
 })
 },

 //多列自創(chuàng)選擇器換列方法
 bindCustomPickerColumnChange: function(e) {
 var customArray = this.data.customArray,
  customIndex = this.data.customIndex,
  onlyArray = this.data.onlyArray;

 customIndex[e.detail.column] = e.detail.value;
 // console.log(onlyArray);

 var searchColumn = () => {
  for (var i = 0; i < customArray.length; i++) {
  var arr1 = [];
  var arr2 = [];
  if (i == customIndex[0]) {
   for (var j = 0; j < customArray[i].dept.length; j++) {
   arr1.push(customArray[i].dept[j].name);
   if (j == customIndex[1]) {
    for (var k = 0; k < customArray[i].dept[j].product.length; k++) {
    arr2.push(customArray[i].dept[j].product[k].name);
    }
    onlyArray[2] = arr2;
   }
   }
   onlyArray[1] = arr1;
  }
  };
 }

 switch (e.detail.column) {
  case 0:
  customIndex[1] = 0;
  customIndex[2] = 0;
  searchColumn();
  break;
  case 1:
  customIndex[2] = 0;
  searchColumn();
  break;
 }
 this.setData({
  onlyArray: onlyArray,
  customIndex: customIndex
 });
 },

需要說明的是:

1).bindchange和bindcolumnchange兩個(gè)函數(shù)都是eventhandle類型的,但他們綁定的數(shù)據(jù)不同。

  • bindchange函數(shù)的e.detail.value為當(dāng)前選擇的所有列的下標(biāo)值數(shù)組,如[0,1,0]代表當(dāng)前選擇器的三列數(shù)據(jù)下標(biāo);
  • bindcolumnchange函數(shù)的e.detail.column代表當(dāng)前選擇的是第幾列,e.detail.value為具體的當(dāng)前選擇的第幾列的數(shù)據(jù)的下標(biāo),是一個(gè)數(shù)字。

2).在bindcolumnchange函數(shù)中,進(jìn)行e.detail.column的判斷,

  • 如果e.detail.column == 0,則代表改變的是第一列的數(shù)據(jù),此時(shí)要將第二列和第三列的數(shù)據(jù)下標(biāo)全部置為0,即置為缺省數(shù)據(jù),并將onlyArray數(shù)組進(jìn)行聯(lián)動(dòng)變化;
  • 如果e.detail.column ==1,則代表改變的是第二列的數(shù)據(jù),此時(shí)將第三列的數(shù)據(jù)下標(biāo)置為0,將onlyArray數(shù)組進(jìn)行聯(lián)動(dòng)變化;
  • 如果e.detail.column ==2,則代表改變的是第三列的數(shù)據(jù),而前兩列不需要進(jìn)行變化,故不需要判斷這種情況的數(shù)組變化。

3).點(diǎn)擊確定后,調(diào)用bindchange方法得到選擇結(jié)果。我們可以得到的數(shù)據(jù)包括兩部分,

  • 一是當(dāng)前選擇的所有列的下標(biāo)值數(shù)組,即customIndex,數(shù)據(jù)回填時(shí)即使用這部分?jǐn)?shù)據(jù);
  • 二是當(dāng)前選擇的內(nèi)容,我們用customIndex中的下標(biāo)查找onlyArray數(shù)組中的具體內(nèi)容得到具體值,例如 " 百度,搜索,百度一下 "。

五、遇到的問題

在這里遇到一個(gè)bug,如果在switch語句中將customIndex[0]或customIndex[1]置為0的語句放在searchColumn()后邊時(shí),數(shù)組顯示會(huì)混亂,如圖所示:

即第一列換列時(shí),如果第二列數(shù)據(jù)下標(biāo)非0,則第三列數(shù)據(jù)無法對(duì)應(yīng)上。

根據(jù)上述bug描述,我猜想可能是在switch判斷中將后列數(shù)據(jù)下標(biāo)置為0的操作晚于數(shù)組變化的的方法才會(huì)導(dǎo)致此問題。

所以后來將customIndex[0]或customIndex[1]置為0的語句放在searchColumn()前才解決了此問題。

六、總結(jié)

此時(shí)我們的自定義多列選擇器就建好了,使用json數(shù)據(jù)作為總數(shù)據(jù),可以自定義選項(xiàng),可以得到想要的兩部分?jǐn)?shù)據(jù),可以數(shù)據(jù)回填,基本滿足了項(xiàng)目中的實(shí)際需要。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

肇州县| 阳谷县| 云南省| 连云港市| 孟州市| 舒兰市| 宁强县| 蓝山县| 平山县| 晋城| 樟树市| 临颍县| 万年县| 房产| 如皋市| 革吉县| 镇远县| 湘阴县| 恩施市| 陆良县| 武城县| 商丘市| 巫溪县| 乐昌市| 高碑店市| 安多县| 资溪县| 财经| 枣阳市| 宁阳县| 江都市| 云林县| 漠河县| 利津县| 西畴县| 珲春市| 资源县| 教育| 东乡| 育儿| 海原县|