当前位置: 首页 > news >正文

基于uniapp与uview做一个按拼音首字母排序的通讯录页面

效果图:

第一步导入pinyin库并应用,用于区分汉字的拼音首字母

npm i pinyin
import pinyin from "pinyin"

完整算法:

function getListByPinyinFirstLetter(data) {const newList = {};for (const item of data) {let firstLetter;if (/[a-zA-Z]/.test(item.name.charAt(0))) {// 如果是英文字母开头的直接使用大写首字母firstLetter = item.name.charAt(0).toUpperCase();} else {const pinyinArray = pinyin(item.name, {style: pinyin.STYLE_FIRST_LETTER, // 提取拼音首字母});if (pinyinArray.length > 0) {firstLetter = /[a-zA-Z]/.test(pinyinArray[0][0].toUpperCase()) ? pinyinArray[0][0].toUpperCase() :"#"; // 获取拼音首字母并转换为大写} else {// 如果没有拼音首字母,则归类为#firstLetter = "#";}}if (!newList[firstLetter]) {newList[firstLetter] = [];}newList[firstLetter].push(item);}// 将键按字母顺序排序const sortedKeys = Object.keys(newList).sort((a, b) => {if (a === '#') return 1;if (b === '#') return -1;return a.localeCompare(b);});const sortedNewList = {};for (const key of sortedKeys) {sortedNewList[key] = newList[key];}console.log(sortedNewList, sortedKeys);indexList.value = sortedKeyslist.value = sortedNewList;}

完整代码(样式自己定义):

<template>{{newList}}<u-index-list :scrollTop="scrollTop" v-if="indexList.length"><view v-for="(item, index) in indexList" :key="index"><u-index-anchor :index="item" /><view class="list-cell" v-for="(item,index) in list[item]">{{item.name}}</view></view></u-index-list>
</template><script setup>import {ref,} from "vue";import pinyin from "pinyin"import {onLoad,onPageScroll} from "@dcloudio/uni-app"onLoad(() => {getListByPinyinFirstLetter(testList.value)})const indexList = ref([])const testList = ref([{name: "张三"},{name: "张学友"},{name: "asasd"},{name: "大师"},{name: "(字符"},])const list = ref([])function getListByPinyinFirstLetter(data) {const newList = {};for (const item of data) {let firstLetter;if (/[a-zA-Z]/.test(item.name.charAt(0))) {// 如果是英文字母开头的直接使用大写首字母firstLetter = item.name.charAt(0).toUpperCase();} else {const pinyinArray = pinyin(item.name, {style: pinyin.STYLE_FIRST_LETTER, // 提取拼音首字母});if (pinyinArray.length > 0) {firstLetter = /[a-zA-Z]/.test(pinyinArray[0][0].toUpperCase()) ? pinyinArray[0][0].toUpperCase() :"#"; // 获取拼音首字母并转换为大写} else {// 如果没有拼音首字母,则归类为#firstLetter = "#";}}if (!newList[firstLetter]) {newList[firstLetter] = [];}newList[firstLetter].push(item);}// 将键按字母顺序排序const sortedKeys = Object.keys(newList).sort((a, b) => {if (a === '#') return 1;if (b === '#') return -1;return a.localeCompare(b);});const sortedNewList = {};for (const key of sortedKeys) {sortedNewList[key] = newList[key];}console.log(sortedNewList, sortedKeys);indexList.value = sortedKeyslist.value = sortedNewList;}onPageScroll(e => {this.scrollTop = e.scrollTop;})
</script><style lang="scss" scoped>.list-cell {display: flex;box-sizing: border-box;width: 100%;padding: 10px 24rpx;overflow: hidden;color: #323233;font-size: 14px;line-height: 24px;background-color: #fff;}
</style>

http://www.lryc.cn/news/215145.html

相关文章:

  • 网络工程师-入门基础课:华为HCIA认证课程介绍
  • 玻色量子成功研制光量子计算专用光纤恒温控制设备——“量晷”
  • 力扣:147. 对链表进行插入排序(Python3)
  • OpenCV4(C++)——形态学(腐蚀、膨胀)
  • C++设计模式_24_Visitor 访问器
  • el-tabel表格加个多选框
  • Go语言集成开发环境(IDE):GoLand 2023中文
  • opencv c++ canny 实现 以及与halcon canny的对比
  • 阿里云无影升级2.0 云电脑解决方案时代到来
  • 【案例展示】多物理场仿真软件介绍
  • k8s的RBAC中,clusterrole, rolebinding 是什么关系谁先谁后
  • myabtis流式查询
  • K8S的pod创建过程
  • java修仙传之海岛奇遇
  • 电子商务平台对接电商供应链,不得不说的开放平台电商API接口
  • 【JAVA学习笔记】 57 - 本章作业
  • 【题解】[GenshinOI Round 3] P9816 少项式复合幂
  • 手写数字识别--神经网络实验
  • 双11消费遇冷?如何让消费回归心智原点
  • 一分钟了解:什么是Image Matting?
  • 微信小程序 跳转客服页面
  • 10个简单增删改查的免费Spring Boot源代码项目
  • mysql数据表设计
  • pytorch复现4_Resnet
  • 【数据库】形式化关系查询语言(一):关系代数Relational Algebra:基本运算、附加关系代数、扩展的关系代数
  • 【计算机网络】计算机网络和因特网
  • JAVA面经整理(9)
  • IPD(集成产品开发)模式下的产品研发流程
  • Flutter GetX的使用
  • 【Amazon】AWS实战 | 快速发布安全传输的静态页面