2025-01-10 05:06:36 +00:00
|
|
|
<script setup>
|
2025-01-10 05:23:11 +00:00
|
|
|
import {ref} from 'vue';
|
2025-01-10 05:06:36 +00:00
|
|
|
import pinyin from 'pinyin';
|
|
|
|
import countryCode from './data/index.js';
|
|
|
|
|
|
|
|
const value = ref('');
|
|
|
|
const alphabet = [
|
|
|
|
'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'
|
|
|
|
];
|
|
|
|
|
|
|
|
function groupByPinyinInitial(data) {
|
|
|
|
const grouped = {};
|
|
|
|
data.forEach(country => {
|
2025-01-10 05:23:11 +00:00
|
|
|
const initial = pinyin(country.cn, {style: pinyin.STYLE_FIRST_LETTER})[0][0].toUpperCase();
|
2025-01-10 05:06:36 +00:00
|
|
|
if (!grouped[initial]) {
|
|
|
|
grouped[initial] = [];
|
|
|
|
}
|
|
|
|
grouped[initial].push(country);
|
|
|
|
});
|
|
|
|
|
|
|
|
return grouped;
|
|
|
|
}
|
|
|
|
|
2025-01-10 05:23:11 +00:00
|
|
|
const groupedCountries = ref([])
|
|
|
|
const initData=()=>{
|
|
|
|
groupedCountries.value = groupByPinyinInitial(countryCode);
|
|
|
|
}
|
|
|
|
const searchCountry = computed(() => {
|
|
|
|
if (!value.value) {
|
|
|
|
return groupedCountries.value;
|
|
|
|
}
|
|
|
|
return Object.keys(groupedCountries.value).reduce((filtered, initial) => {
|
|
|
|
const countries = groupedCountries.value[initial].filter(country =>
|
|
|
|
country.cn.includes(value.value)
|
|
|
|
);
|
|
|
|
if (countries.length > 0) {
|
|
|
|
filtered[initial] = countries;
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}, {});
|
|
|
|
});
|
|
|
|
initData()
|
2025-01-10 05:06:36 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<van-sticky>
|
2025-01-10 05:23:11 +00:00
|
|
|
<van-search v-model="value" placeholder="请输入国家和地区"/>
|
2025-01-10 05:06:36 +00:00
|
|
|
</van-sticky>
|
2025-01-13 03:33:15 +00:00
|
|
|
<van-index-bar sticky :sticky-offset-top="55" :index-list="alphabet">
|
2025-01-10 05:23:11 +00:00
|
|
|
<template v-for="(countries, index) in searchCountry" :key="index">
|
|
|
|
<van-index-anchor
|
|
|
|
:index="index"
|
|
|
|
></van-index-anchor>
|
|
|
|
<van-cell v-for="country in countries" :key="country.code" :title="country.cn">
|
|
|
|
<div class="pr-[25px]"> +{{ country.zone }}</div>
|
|
|
|
</van-cell>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
2025-01-10 05:06:36 +00:00
|
|
|
</van-index-bar>
|
2025-01-10 05:23:11 +00:00
|
|
|
<van-back-top right="15vw" bottom="10vh"/>
|
2025-01-10 05:06:36 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style scoped>
|
2025-01-10 05:23:11 +00:00
|
|
|
|
2025-01-10 05:06:36 +00:00
|
|
|
</style>
|