liveh5-nuxt/app/pages/countryRegion/index.vue

69 lines
1.8 KiB
Vue
Raw Normal View History

<script setup>
2025-01-10 05:23:11 +00:00
import {ref} from 'vue';
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();
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()
</script>
<template>
<div>
<van-sticky>
2025-01-10 05:23:11 +00:00
<van-search v-model="value" placeholder="请输入国家和地区"/>
</van-sticky>
<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>
</van-index-bar>
2025-01-10 05:23:11 +00:00
<van-back-top right="15vw" bottom="10vh"/>
</div>
</template>
<style scoped>
2025-01-10 05:23:11 +00:00
</style>