如下圖所示,實(shí)現(xiàn)該按鈕toggle功能。百度上很多都是只設(shè)置一個(gè)按鈕的toggle,所以我現(xiàn)在來(lái)稍微總結(jié)下:多個(gè)按鈕如何實(shí)現(xiàn)自身的toggle功能。原理:1,列表展示的時(shí)候,我們會(huì)用wx:for 來(lái)循環(huán)數(shù)據(jù),那么我們就會(huì)得到相 ...
如下圖所示,實(shí)現(xiàn)該按鈕toggle功能。
百度上很多都是只設(shè)置一個(gè)按鈕的toggle,所以我現(xiàn)在來(lái)稍微總結(jié)下:多個(gè)按鈕如何實(shí)現(xiàn)自身的toggle功能。
原理:1,列表展示的時(shí)候,我們會(huì)用wx:for 來(lái)循環(huán)數(shù)據(jù),那么我們就會(huì)得到相應(yīng)的當(dāng)前的第幾個(gè)數(shù)據(jù)(即 wx:key="index")
2.在每一個(gè)數(shù)據(jù)里面添加一個(gè)toggle的屬性,toggle=false 則不展示,
3.設(shè)置一個(gè)點(diǎn)擊方法,獲取當(dāng)前按鈕所在的是第幾個(gè)數(shù)據(jù),然后將相應(yīng)的toggle取反,然后將修改后的數(shù)據(jù)重新寫進(jìn)去 (我出錯(cuò)的地方就在這)
4. 在wxml頁(yè)面判斷toggle的值是true/false ,然后修改相應(yīng)的class名
js代碼:
Page({ data:{ datas:[] }, onLoad:function(options){ var that=this; API.my_ajax('',function(res){ //用mock.js 設(shè)置的模擬數(shù)據(jù)調(diào)用格式 // console.log(res); var listData=res.data; for(var i=0;i<listData.length;i++){ listData[i]['toggle']=false; //添加toggle 屬性 } that.setData({ datas:listData }) // console.log(listData) }) }, showBtn:function(e){ console.log(e); console.log(this); //這兩個(gè)console.log 可以查看當(dāng)前方法里面所有的數(shù)據(jù),可以查找一下我們所需要的內(nèi)容是否有在里面,底下的index 就是這樣找到的 var a=e.currentTarget.dataset.index; var b=this.data.datas[a].toggle; this.data.datas[a].toggle=!b; //設(shè)置之后我們要把數(shù)據(jù)從新添回去 this.setData({ datas:this.data.datas }) } }) wxml代碼:
<!--使用二維碼按鈕--> <label class="icon_qrcode_wrap" data-index="{{index}}" bindtap="showBtn"> <text>使用規(guī)格及二維碼</text> <image class="icon_right" src="../../images/up.png"></image> </label> <!--彈出二維碼樣式--> <view class="qrcode_show_wrap {{item.toggle==true ? '':'none'}}"> <view class="qrcode_container"> <!--<image class="qrcode_big_bg"src="../../images/qrcode_black_bg.png"></image>--> <view class="block_qrcode_wrap"> <image class="tiaoma" src="../../images/tiao_code.png"></image> <image class="rqcode" src="../../images/rq_code_img.png"></image> <text style="display:block;" class="fs12">erwr43545</text> </view> <text class="rq_code_title cfff fs13">使用規(guī)則</text> <text class="cfff code_txt">就掉粉絲活生生的誰(shuí)讓他和人文就掉粉絲活生生的誰(shuí)讓他和人文就掉粉絲活生生的誰(shuí)讓他和人文</text> </view> </view> </view> gyhtr