Commit 94bf65bd by yancanhua

fix: 代码优化

1 parent 8aa75363
<template> <template>
<view> <view>
<uni-popup ref="popupRef" type="center" background-color="#fff" border-radius="24px" z-index="10000" <uni-popup ref="popupRef" type="center" background-color="#fff" border-radius="24px" z-index="10000"
:show-close="false"> :show-close="false" :mask-click="false">
<view class="wrapper"> <view class="wrapper">
<view class="top-bg"> <view class="top-bg">
<view class="top-icon"> <view class="top-icon">
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<van-index-anchor :index="letter">{{ letter }}</van-index-anchor> <van-index-anchor :index="letter">{{ letter }}</van-index-anchor>
<van-cell v-for="(brand, index) in getBrandsByLetter(letter)" :key="brand.id"> <van-cell v-for="(brand, index) in getBrandsByLetter(letter)" :key="brand.id">
<template slot="title"> <template slot="title">
<div class="brand-cell" @click="bindClick(brand, index)"> <div class="brand-cell" @click="bindClick(brand)">
<image class="brand-image" :src="brand.logoUrl" mode="aspectFill" /> <image class="brand-image" :src="brand.logoUrl" mode="aspectFill" />
{{ brand.name }} {{ brand.name }}
<div class="check-icon"> <div class="check-icon">
...@@ -84,6 +84,7 @@ export default class selectVehicleBrand extends Vue { ...@@ -84,6 +84,7 @@ export default class selectVehicleBrand extends Vue {
uni.setStorageSync('cacheBrands', this.brands); uni.setStorageSync('cacheBrands', this.brands);
}); });
} }
//默认回显已经选中的品牌 //默认回显已经选中的品牌
const storagVehicleBrandList = JSON.parse(uni.getStorageSync('storagVehicleBrandList') || '[]'); const storagVehicleBrandList = JSON.parse(uni.getStorageSync('storagVehicleBrandList') || '[]');
this.brands = this.brands.map(item => { this.brands = this.brands.map(item => {
...@@ -97,15 +98,24 @@ export default class selectVehicleBrand extends Vue { ...@@ -97,15 +98,24 @@ export default class selectVehicleBrand extends Vue {
return this.brands.filter(brand => brand.letter === letter); return this.brands.filter(brand => brand.letter === letter);
} }
bindClick(brand: any, index: number) { bindClick(brand: any) {
if(this.selectedBrand.length >= 10) { // 找到品牌在原始数组中的索引
uni.showToast({ const brandIndex = this.brands.findIndex(item => item.id === brand.id);
title: '最多可选10个品牌', // 如果品牌已选中,则直接取消选择
icon: 'none' if (this.brands[brandIndex].selected) {
}); this.brands[brandIndex].selected = false;
return; } else {
// 检查是否超过最大选择数量
if(this.selectedBrand.length >= 10) {
uni.showToast({
title: '最多可选10个品牌',
icon: 'none'
});
return;
}
this.brands[brandIndex].selected = true;
} }
this.brands[index].selected = !this.brands[index].selected; // 更新选中品牌列表
this.selectedBrand = this.brands.filter(brand => brand.selected); this.selectedBrand = this.brands.filter(brand => brand.selected);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!