最近在写一个相册选片的小程序,通过通过两层wx:for 实现点击产品,进行记录的功能。应用场景是,第一层wx:for 循环出2个产品,每个产品下面第二层WX:for循环出拍摄的照片。每个照片有一个bindtap select(e)的事件点击。现在的问题是,每一次点击事件,虽然可以可以通过index实现区别出第二层的每个照片。但由于src的统一设置,每个产品下面的此张图片都会被选择上。e.target或e.currenttarget都实现不了。请教如何通过最底层每张照片的选择时,获取最上层的某一个属性值,来区分是第几张照片。或者其他有什么办法?
wxml:
<view wx:for="{{free_products}}" class="itemContainer">
<image mode="widthFix" src="{{item['url_zhu']}}"></image>
<text>横板{{item['horizontal']}}张</text>
<text>竖板{{item['vertical']}}张</text>
<view >
<swiper
indicator-dots="{{indicatorDots}}"
autoplay="{{autoplay}}"
interval="{{interval}}"
duration="{{duration}}"
circular="true">
<swiper-item data-index='{{index}}' wx:for="{{selected_photo}}" bindtap="select">
<image src="{{item['url']}}"/>
<view class="select_icon" >
<image mode="widthFix" src="{{selected_photo[index]['product_selected']?yes_select_path:not_select_path}}" ></image>
</view>
</swiper-item>
</swiper>
</view>
</view>
js:
data: {
products:[],
free_products:[],
charge_products:[],
show_box:[],
indicatorDots: true,
autoplay: true,
interval: 5000,
duration: 1000,
selected_photo:[],
not_select_path: '../../images/not_selected.png',
yes_select_path: '../../images/selected.png',
},
select(e) {
var index = e.currentTarget.dataset.index
var a = this.data.selected_photo
if (a[index]['product_selected'] === false){
a[index]['product_selected'] = true
}else{
a[index]['product_selected'] = false
}
this.setData({
selected_photo:a
});
console.log(e)
},
图片:
<swiper-item data-index='{{index}}' wx:for="{{selected_photo}}" bindtap="select" data-xxx='{{父元素值1}}' data-xxx2='{{父元素值2}}' >
你把想要的属性也一起给照片元素不就可以了