你的理解还是有问题是按照26字母顺序比如说姓苏或者宋等,都是S字母开头,按姓名拼音开头第一个字母排序,然后依次按拼音顺序排
时间: 2024-04-21 10:23:24 浏览: 208
非常抱歉,我之前的回答还是理解有误。以下是按照姓氏拼音进行排序的修改后的代码示例:
```vue
<template>
<view class="container">
<view class="title">通讯录</view>
<view class="index-bar">
<view
class="index-item"
v-for="(group, index) in contactGroups"
:key="index"
@click="scrollToGroup(group)"
>
{{ group }}
</view>
</view>
<scroll-view class="contact-list" scroll-y :scroll-into-view="scrollIntoView">
<view class="contact-group" v-for="(group, index) in contactGroups" :key="index">
<view class="group-title">{{ group }}</view>
<view
class="contact-item"
v-for="(contact, idx) in groupContacts[group]"
:key="idx"
@click="goToChatPage(contact)"
>
<view class="contact-info">
<view class="contact-name">{{ contact.name }}</view>
<view class="contact-phone">{{ contact.phone }}</view>
</view>
<button class="contact-delete" @click.stop="deleteContact(contact)">删除</button>
</view>
</view>
</scroll-view>
<button class="add-contact" @click="addContact">添加联系人</button>
</view>
</template>
<script>
export default {
data() {
return {
contacts: [
{ name: '苏三', phone: '13888888888' },
{ name: '宋家', phone: '13999999999' },
{ name: '王五', phone: '13666666666' },
],
contactGroups: [],
groupContacts: {},
scrollIntoView: '',
};
},
created() {
this.groupContacts = this.groupContactsByAlphabet(this.contacts);
this.contactGroups = Object.keys(this.groupContacts).sort();
},
methods: {
groupContactsByAlphabet(contacts) {
const groups = {};
contacts.forEach((contact) => {
const firstLetter = this.getFirstLetter(contact.name);
if (!groups[firstLetter]) {
groups[firstLetter] = [];
}
groups[firstLetter].push(contact);
});
return groups;
},
getFirstLetter(name) {
const pinyin = require('pinyin');
const pinyinArr = pinyin(name, { style: pinyin.STYLE_FIRST_LETTER });
return pinyinArr[0][0].toUpperCase();
},
addContact() {
uni.navigateTo({
url: '/pages/addContact/addContact',
});
},
deleteContact(contact) {
const group = this.getFirstLetter(contact.name);
const index = this.groupContacts[group].findIndex((c) => c.name === contact.name);
if (index > -1) {
this.groupContacts[group].splice(index, 1);
if (this.groupContacts[group].length === 0) {
delete this.groupContacts[group];
this.contactGroups.splice(this.contactGroups.indexOf(group), 1);
}
}
},
goToChatPage(contact) {
// 跳转到聊天页面的逻辑
},
scrollToGroup(group) {
this.scrollIntoView = `group-${group}`;
},
},
};
</script>
<style scoped>
.container {
padding: 20rpx;
}
.title {
font-size: 32rpx;
text-align: center;
}
.index-bar {
display: flex;
justify-content: center;
margin-bottom: 10rpx;
}
.index-item {
margin: 0 10rpx;
font-size: 28rpx;
color: #007aff;
}
.contact-list {
height: calc(100vh - 250rpx);
}
.contact-group {
margin-bottom: 20rpx;
}
.group-title {
padding-left: 20rpx;
font-size: 28rpx;
color: #999999;
}
.contact-item {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10rpx;
}
.contact-info {
flex: 1;
}
.contact-name {
font-size: 28rpx;
margin-bottom: 6rpx;
}
.contact-phone {
font-size: 24rpx;
color: #999999;
}
.contact-delete {
font-size: 24rpx;
color: #ff0000;
}
.add-contact {
margin-top: 20rpx;
width: 100%;
height: 60rpx;
line-height: 60rpx;
text-align: center;
font-size: 28rpx;
background-color: #007aff;
color: #ffffff;
}
</style>
```
上述代码使用了一个名为 `pinyin` 的拼音库,通过引入并调用该库的 `pinyin()` 方法,可以将中文姓名转换为拼音,并取首字母进行分组。
请在代码中添加 `pinyin` 拼音库的依赖,具体的引入方式取决于你的项目和构建工具。
希望这次的修改满足了你的需求。如果还有其他问题,请随时提问!
阅读全文