uni-app做A-Z排序通讯录、索引列表

上图是效果图,三个问题

  • 访问电话通讯录,拿数据
  • 拿到用户的联系人数组对象,之后根据A-Z排序
  • 根据字母索引快速搜索

首先说数据怎么拿 - 社区有指导
https://ask.dcloud.net.cn/question/64117 uniapp 调取通讯录

// #ifdef APP-PLUS
    plus.contacts.getAddressBook( plus.contacts.ADDRESSBOOK_PHONE, function( addressbook ) {
       // 查找联系人  
        addressbook.find(["displayName","phoneNumbers"],function(contacts){  
          console.log('获取联系人成功')  
           console.log(JSON.stringify(contacts)) ; //拿到的数据
          }, function () {  
               uni.showToast({  
            title: '获取联系人失败',  
            duration: 2000  
        })  
           },{multiple:true}); 
    }, function ( e ) {
        alert( "Get address book failed: " + e.message );
    })
// #endif 

这样就实现了第一步,接下来分析拿到的数据,做处理。

{ 
       "id": 6,
        "rawId": null,
        "target": 0,
        "displayName": "Ann",
        "name": null,
        "nickname": null,
        "phoneNumbers": [{
                "id": 0,
                "pref": false,
                "type": "home",
                "value": "895694582"
            }],
        "emails": null,
        "addresses": null,
        "ims": null,
        "organizations": null,
        "birthday": null,
        "note": null,
        "photos": null,
        "categories": null,
        "urls": null }

从这部分数据看,有用到的是

{displayName:"Ann", "phoneNumbers":[ ... ]}

我们将换成另一种数据结果

 pySort:function(arrList){
    var $this = this;
    if(!String.prototype.localeCompare)
        return null;
    var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#".split('');//ABCDEFGHJKLMNOPQRSTWXYZ
    var zh = "阿八嚓哒妸发旮哈*讥咔垃痳拏噢妑七呥涩它**穵夕丫帀".split('');
    var result = [];
    var curr;

    for(let i=0;i<letters.length;i++){
        curr = {letter: letters[i], data:[]}; //字母对象,data数据
        arrList.forEach((n)=>{
            let initial = n.displayName.charAt(0);//截取第一个字符
            if(initial==letters[i]||initial==letters[i].toLowerCase()){//首字符是英文的
                curr.data.push(n);
            }else if(zh[i]!=='*'&&$this.isChinese(initial)){//判断是否是无汉字,是否是中文
                let chinaName = pinyin.getFullChars(initial).charAt(0); //直接使用pinyin中方法
                if(chinaName==letters[i]||chinaName==letters[i].toLowerCase()){//首字符是英文的
                    curr.data.push(n);
                }
            }
            if(letters[i]=='#'&&!$this.isChar(initial)&&!$this.isChinese(initial)){
                curr.data.push(n);
            }
        })
        result.push(curr)
    }
    this.contactList = result; //contactList 是data中定义的 []
},
isChinese:function(temp){
    var re=/[^\u4E00-\u9FA5]/;
    if (re.test(temp)){return false;}
    return true ;
},
isChar:function(char){
    var reg = /[A-Za-z]/;
    if (!reg.test(char)){return false ;}
    return true ;
},

截取姓名的首字符,英文可以直接比对;数字字符也可以直接比对;中文需要将转成拼音再取首字母

汉字转拼音js下载路径:(如果无效,自行处理)
链接:https://pan.baidu.com/s/1NZ4noIgHv2HSzZW6yBRTxA 密码:2kv1

注意的是,下载的这份js不能直接在vue项目中使用,需要在js文件中加

export{
    pinyin
}
//页面引入
import {pinyin} from '../../common/Convert_Pinyin.js';

这样排序做完。接下来就是索引部分。
其实可以直接使用插件市场的插件,地址附上(https://ext.dcloud.net.cn/plugin?id=375#detail)
但是可以更简单

<scroll-view class="scroll-list" :scroll-into-view="scrollViewId" scroll-y="true" scroll-with-animation  :style="{height:winHeight + 'px'}">
    <view v-for="(item,index) in contactList" :key="index" :id="item.letter == '#' ? 'indexed-list-YZ' :'indexed-list-' + item.letter">
        <view class="letter-title" v-if="item.data&&item.data.length" id="item.letter">{{item.letter}}</view>
        <view> .......</view>
    </view>
</scroll-view>
<view class="right-menu">
    <view v-for="(i,index) in Letters" :key="index" @click="jumper(i,index)" :class="jumperIndex == i?'letter-item active':'letter-item'">{{i}}</view>
</view>

这里的scroll-view是关键,scroll-view


scroll-into-view 与 子元素的id结合使用。

data() {
    return {
        jumperIndex:'A',
        contactList:[],
        scrollViewId:'',
        winHeight: 0,
        itemHeight: 0,
        Letters:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#'],
    }
},
methods:{
       jumper(event,i){
        this.jumperIndex = event;
        let len = this.contactList[i].data.length;
        if(event == '#'){
            this.scrollViewId = 'indexed-list-YZ';
            return
        }
        if(len>0){
            console.log(111);
            this.scrollViewId = 'indexed-list-' + event;
        }
    },
},
onLoad(){       
    let winHeight = uni.getSystemInfoSync().windowHeight;
    this.itemHeight = winHeight / 26;
    this.winHeight = winHeight;
},

主要代码,主要功能完结。

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季夏梧桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值