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

仿iPhone通訊錄制作小程序自定義選擇組件的實現(xiàn)

 更新時間:2019年05月23日 14:40:16   作者:大雄呀  
這篇文章主要介紹了仿iPhone通訊錄制作小程序自定義選擇組件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

近期閑來無事,想著閑著也是閑著,不如給自己搞點事情做!敢想敢做,于是選擇了給微信小程序做個 仿iPhone通訊錄 效果的自定義組件。

先來整理一下,瞧瞧需要實現(xiàn)的核心功能。

  1. 按照第一個字的首字母排序;
  2. 實現(xiàn)輸入搜索功能;
  3. 側邊欄字母導航;

基本上分為3塊:

  1. 頂部的搜索區(qū)域;
  2. 內容的展示區(qū)域;
  3. 側邊字母導航欄區(qū)域;
// index.wxml
<view class="main">
 <!-- 頂部搜索區(qū)域 -->
 <view class="header">
 </view>
 <!-- 內容區(qū)域 -->
 <scroll-view class="scroll">
 </scroll-view>
 <!-- 側邊導航 -->
 <view class="sub_nav">
 </view>
</view>

【頂部的搜索區(qū)域】

一目了然就直接貼代碼了。

<view class="header">
 // 這里或許有人要問,為啥不用小程序的label組件呢。?_?
 // 原因就是...我就不用,你還能咬我?!^(oo)^
 // 哈哈哈哈~開個玩笑,其實是小程序的label組件還沒支持input!
 <view class="label"> 
  <icon></icon>
  <input type="text" placeholder="搜索" />
 </view>
</view>

【內容的展示區(qū)域】

再說一目了然會不會被打呢?:joy:

根據圖片就可以看出來,存在2個區(qū)域。

  1. 紅框包圍的外框,負責圈定展示的范圍;
  2. 綠框包圍的范圍,包含有字母標題和對應的子項。

代碼如下:

<scroll-view class="scroll">
 <view class="dl">
  <view class="dt">這里是字母標題。</view>
  <view class="dd">
  <span>這里當然是展示的內容啦。</span>
  </view>
 </view>
 </scroll-view>

【側邊字母導航欄區(qū)域】

為了節(jié)省一下文章的篇幅,這里就不貼圖了,很簡單,就是并排下來就好了。

<view class="sub_nav">
 <view class="option">這里是輸出字母。</view>
</view>

接下來是wxss的樣式了。

考慮到wxss的樣式較多,我就直接貼 代碼鏈接 吧,有興趣的童鞋可以瞧瞧。

完成之后,是時候貼個效果圖了。(不許吐槽丑,寶寶會不開心的!:pensive:)

結構樣式弄完了,也貼一下自定組件的基礎文件

// index.json
{
 "component": true
}
// index.js
Component({
 properties: {}, // 組件的對外屬性
 data: {},  // 組件的內部數(shù)據
 lifetimes: {}, // 生命周期
 methods: {}  // 事件
});

現(xiàn)在開始實現(xiàn)功能了?。?!

按照第一個字的首字母排序

說實話,實現(xiàn)這塊功能呢,我是沒啥頭緒的,所以這個時候就要求助偉大的“度娘/Google”了。

經過樓主“遍尋網絡”,查找到如下頁面的源碼參考:

因樓主問題,遺忘了該網址,如有知道的童鞋,貼個鏈接告訴下樓主,樓主立馬麻溜的加上。 源碼的原理大概描述下:

收錄 20902 個漢字和 375 個多音字的 Unicode 編碼,然后用JS切割首字母并轉換成 Unicode 進行對比,最后返回對應首字母的拼音。

// 漢字對應的Unicode編碼文件
// oMultiDiff = 多音字 | firstLetterMap = 漢字
import firstStore from './firstChineseLetter'; 

// 獲取首字母拼音
function getFirstLetter (val) {
 const firstVal = val.charAt(0);
 if (/.*[\u4e00-\u9fa5]+.*/.test(firstVal)) {
 // 處理中文字符
 // 轉換成Unicode編碼,與firstStore里面的數(shù)據進行對比,然后返回對應的參數(shù)
 const code = firstVal.charCodeAt(0); // 轉換成Unicode編碼
 return code in firstStore.oMultiDiff ? firstStore.oMultiDiff[code] : firstStore.firstLetterMap.charAt(code - 19968);
 } else {
 // 這里處理非中文
 // 檢測是否字母,如果是就直接返回大寫的字母
 // 不是的話,返回“#”
 return /^[a-zA-Z]+$/.test(firstVal) ? firstVal.toUpperCase() : '#';
 }
}

getFirstLetter('東城區(qū)');
// 輸出結果:D

firstChineseLetter.js地址

獲取首字母的方法有了之后,就該對數(shù)據進行處理了。

首先定義一下組件所需要的參數(shù)。

Component({
 // 組件的對外屬性
 properties: {
 data: { type: Array, value: [], }, // 組件外傳遞進來的數(shù)據
 attr: { type: String, value: 'label' }, // 需要進行首字母處理的屬性,默認是"label"
 },
 ...
})

然后,針對組件外傳遞進來的數(shù)據,做一次轉換。

// 靜態(tài)數(shù)據的存儲
const Static = {
 list: []
}

Component({
 ...
 methods: {
 // 初始/重置數(shù)據
 init () {
  const { data, attr } = this.properties;

  let changeData = [], // 轉換后的數(shù)據
   inChangeData = {}; // 存儲轉換后的數(shù)據對應字母的索引值
   
  data.map(v => {
  // 獲取首字母拼音
  let firstLetter = this.getFirstLetter(v[attr]); 
  
  // 循環(huán)對比檢測
  firstLetter.split('').map(str => {
   if (str in inChangeData) {
   // 有首字母相同的項,
   // 則添加入已有的項里面
   changeData[inChangeData[str]].list.push(v);
   } else {
   // 沒有首字母相同的項,
   // 則在尾部追加一條新的數(shù)據,
   // 儲存對應的字母值(firstLetter),
   // 同時存儲該字母對應的索引
   changeData.push({ firstLetter: str, list: [v] });
   inChangeData[str] = changeData.length - 1;
   }
  });
  });
  
  // 此時轉換后的數(shù)組屬于亂序,
  // 需要對亂序的數(shù)組進行排序
  changeData.sort((pre, next) => pre.firstLetter < next.firstLetter ? -1 : 1);
  
  // 若存在“#”項,將位置位移至底部
  if (changeData[0].firstLetter === '#') {
  const firstArr = changeData.splice(0, 1);
  changeData = [...changeData, ...firstArr];
  }

  // 存儲轉換后的數(shù)據,
  // this.data.list的數(shù)據對應頁面的展示數(shù)據,因為有搜索功能,數(shù)據可能會變更,
  // 在靜態(tài)的數(shù)據里面,也存儲1份數(shù)據,方便后續(xù)的搜索等功能。
  this.setData({ list: changeData });
  Static.list = changeData;
 },
 }
 ...
});

初始化函數(shù)有了之后呢,當然是調用它啦。

Component({
 lifetimes: {
 // 在組件實例進入頁面節(jié)點樹時執(zhí)行初始化數(shù)據
 attached () {
  this.init();
 }
 },
 observers: {
 // 考慮到組件傳遞的數(shù)據存在變更的可能,
 // 在數(shù)據變更的時候,也要做一次初始化
 'data, attr, icon' (data, attr) {
  this.init();
 }
 },
})

接下來是搜索功能啦~

先給頁面搜索框加個監(jiān)聽事件(input)

<view class="main">
 ...
 <view class="header">
 <view class="label">
  <icon></icon>
  <input type="text" placeholder="搜索" value="{{ search }}" bindinput="searchData" />
 </view>
 </view>
 ...
</view>

接著是JS的事件

const Static = {
 list: []
}

Component({
 ...
 methods: {
 searchData (e) {
  const { value } = e.detail; // 用戶輸入的值
  const { list } = Static; // init存儲的靜態(tài)數(shù)據,用來做數(shù)據對比
  const { attr } = this.properties; // 要對比的屬性值
  let result = [], tem = {};
  
  // 沒有搜索內容,返回全部內容
  if (value.length === 0) { this.setData({ list: Static.list }); return; }

  // 檢索搜索內容
  list.map(v => {
  // 獲取所有跟value匹配上的數(shù)據
  const searchList = v.list.filter(v => v[attr].indexOf(value) !== -1);
  
  if (searchList.length > 0) {
   // 此處原理類似樓上init的對比,此處不細說,
   // 反正我懶我有理(0.0)
   if (v.firstLetter in tem) {
   const _list = result[tem[v.firstLetter]].lish;
   result[tem[v.firstLetter]].lish = [..._list, ...searchList];
   } else {
   result.push({ firstLetter: v.firstLetter, list: [...searchList] });
   tem[v.firstLetter] = result.length - 1;
   }
  }
  });
  
  // 存儲數(shù)據
  this.setData({ list: result, search: value });
 }
 },
 ...
});

側邊欄字母導航

(突然覺得,寫文好累?。。。。?/p>

寫這塊的時候呢,樓主發(fā)現(xiàn)了iPhone通訊錄側邊導航欄有個問題, 手指在字母導航欄上滑動的時候,有時候很難確認自己滑到了哪個區(qū)域?!

然鵝這個問題呢,樓主發(fā)現(xiàn)了微信的通訊錄,針對這塊添加了手指滑動的時候,添加了個結構來幫助用戶確認目前所處的區(qū)域。

樓主本著學習的精神,借(chao)鑒(xi)了這個效果,來個效果圖。

貼一下新的wxml結構

<!-- 側邊導航 -->
 <view class="sub_nav" id="subNav" catchtouchstart="subTouchStart" catchtouchmove="subTouchMove" catchtouchend="subTouchEnd">
 <view class="option" wx:for="{{ list }}" data-firstLetter="{{ item.firstLetter }}" wx:key="firstLetter">
  {{ item.firstLetter }}
  <!-- 以下這塊就是新增的結構啦 S -->
  <view 
  class="max {{ item.firstLetter === scrollIntoView && subNavHint ? 'show' : '' }}" 
  data-desc="{{ item.firstLetter }}"
  ></view>
  <!-- 以上這塊就是新增的結構啦 E -->
 </view>
 </view>
const Static = {
 list: [],
 timer: null
}

Component({
 ...
 data: {
 scrollIntoView: '', // 標記當前處于哪個字母
 subNavHint: false, // 控制借(chao)鑒(xi)微信效果的元素
 },
 methods: {
 subTouchStart () {
  this.setData({ subNavHint: true, scrollIntoView: '' });
 },
 subTouchEnd () {
  this.setData({ subNavHint: false });
 },
 subTouchMove (e) {
  // 獲取字母導航欄元素對應的值
  const query = this.createSelectorQuery();
  query.select('#subNav').boundingClientRect();
  query.selectViewport().scrollOffset();
  query.exec(res => {
  const { clientY } = e.touches[0]; // Y軸的位置
  const DomTop = res[0].top; // 導航元素距離頂部的位置
  const { list } = this.data;
  
  // 計算索引,
  // 或許看到這里有人會疑問,為什么是除以20?
  // 因為樣式里面,我寫的高度是20px,所以每個字母的區(qū)域是20px。
  let index = Math.round((clientY - DomTop) / 20); 
  index = index >= list.length ? list.length - 1 : index; // 限制索引大于0
  index = index < 0 ? 0 : index; // 限制索引小于0
  // 限制結果重復賦值
  if (list[index].firstLetter !== this.data.scrollIntoView) {
   this.setData({ scrollIntoView: list[index].firstLetter });
   // 加個抖動效果
   wx.vibrateShort(); 
  }
  });
  }
 },
 }
 ...
});

結語

文章寫到這呢,基本上核心的功能都已經實現(xiàn)啦~ :stuck_out_tongue_closed_eyes:(終于寫完了...)

通過自己封裝組件,樓主還是有挺大收獲的!

當然,這個組件還有很多可以繼續(xù)完善的地方,有興趣的童鞋呢,可以提出你的優(yōu)化建議,樓主有時(xing)間(qu)的話,會繼續(xù)完善下去。

最后,還是推一下這個組件啦,希望它能幫到有需要的童鞋。

github地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論