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


    效果图:

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

    npm i pinyin
    import pinyin from "pinyin"

    完整算法:

    1. function getListByPinyinFirstLetter(data) {
    2. const newList = {};
    3. for (const item of data) {
    4. let firstLetter;
    5. if (/[a-zA-Z]/.test(item.name.charAt(0))) {
    6. // 如果是英文字母开头的直接使用大写首字母
    7. firstLetter = item.name.charAt(0).toUpperCase();
    8. } else {
    9. const pinyinArray = pinyin(item.name, {
    10. style: pinyin.STYLE_FIRST_LETTER, // 提取拼音首字母
    11. });
    12. if (pinyinArray.length > 0) {
    13. firstLetter = /[a-zA-Z]/.test(pinyinArray[0][0].toUpperCase()) ? pinyinArray[0][0].toUpperCase() :
    14. "#"; // 获取拼音首字母并转换为大写
    15. } else {
    16. // 如果没有拼音首字母,则归类为#
    17. firstLetter = "#";
    18. }
    19. }
    20. if (!newList[firstLetter]) {
    21. newList[firstLetter] = [];
    22. }
    23. newList[firstLetter].push(item);
    24. }
    25. // 将键按字母顺序排序
    26. const sortedKeys = Object.keys(newList).sort((a, b) => {
    27. if (a === '#') return 1;
    28. if (b === '#') return -1;
    29. return a.localeCompare(b);
    30. });
    31. const sortedNewList = {};
    32. for (const key of sortedKeys) {
    33. sortedNewList[key] = newList[key];
    34. }
    35. console.log(sortedNewList, sortedKeys);
    36. indexList.value = sortedKeys
    37. list.value = sortedNewList;
    38. }

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

    1. <script setup>
    2. import {
    3. ref,
    4. } from "vue";
    5. import pinyin from "pinyin"
    6. import {
    7. onLoad,
    8. onPageScroll
    9. } from "@dcloudio/uni-app"
    10. onLoad(() => {
    11. getListByPinyinFirstLetter(testList.value)
    12. })
    13. const indexList = ref([])
    14. const testList = ref([{
    15. name: "张三"
    16. },
    17. {
    18. name: "张学友"
    19. },
    20. {
    21. name: "asasd"
    22. },
    23. {
    24. name: "大师"
    25. },
    26. {
    27. name: "(字符"
    28. },
    29. ])
    30. const list = ref([])
    31. function getListByPinyinFirstLetter(data) {
    32. const newList = {};
    33. for (const item of data) {
    34. let firstLetter;
    35. if (/[a-zA-Z]/.test(item.name.charAt(0))) {
    36. // 如果是英文字母开头的直接使用大写首字母
    37. firstLetter = item.name.charAt(0).toUpperCase();
    38. } else {
    39. const pinyinArray = pinyin(item.name, {
    40. style: pinyin.STYLE_FIRST_LETTER, // 提取拼音首字母
    41. });
    42. if (pinyinArray.length > 0) {
    43. firstLetter = /[a-zA-Z]/.test(pinyinArray[0][0].toUpperCase()) ? pinyinArray[0][0].toUpperCase() :
    44. "#"; // 获取拼音首字母并转换为大写
    45. } else {
    46. // 如果没有拼音首字母,则归类为#
    47. firstLetter = "#";
    48. }
    49. }
    50. if (!newList[firstLetter]) {
    51. newList[firstLetter] = [];
    52. }
    53. newList[firstLetter].push(item);
    54. }
    55. // 将键按字母顺序排序
    56. const sortedKeys = Object.keys(newList).sort((a, b) => {
    57. if (a === '#') return 1;
    58. if (b === '#') return -1;
    59. return a.localeCompare(b);
    60. });
    61. const sortedNewList = {};
    62. for (const key of sortedKeys) {
    63. sortedNewList[key] = newList[key];
    64. }
    65. console.log(sortedNewList, sortedKeys);
    66. indexList.value = sortedKeys
    67. list.value = sortedNewList;
    68. }
    69. onPageScroll(e => {
    70. this.scrollTop = e.scrollTop;
    71. })
    72. script>
    73. <style lang="scss" scoped>
    74. .list-cell {
    75. display: flex;
    76. box-sizing: border-box;
    77. width: 100%;
    78. padding: 10px 24rpx;
    79. overflow: hidden;
    80. color: #323233;
    81. font-size: 14px;
    82. line-height: 24px;
    83. background-color: #fff;
    84. }
    85. style>

  • 相关阅读:
    Centos7 安装 Mysql
    R语言layout () 函数
    使用 Python Timedelta 月份计算日期
    2022牛客蔚来杯第二场 G J K L D C (H)
    SQL教程之每个分析工程师都需要知道的 5 个 SQL 函数
    Java调用方法,键盘录入数据,分别求出长方形的面积和长方体的体积。
    CTFHub | MySQL结构
    WPF数据绑定详解(Binding基础)(一)
    93. 对 Promise 的理解?
    高通DSP架构和HVX指令介绍
  • 原文地址:https://blog.csdn.net/m0_49083276/article/details/134161392