小程序模板網(wǎng)

微信小程序--實(shí)現(xiàn)多個(gè)按鈕toggle功能

發(fā)布時(shí)間:2017-12-19 10:21 所屬欄目:小程序開(kāi)發(fā)教程

如下圖所示,實(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ù)重新寫(xiě)進(jìn)去 (我出錯(cuò)的地方就在這) 
4. 在wxml頁(yè)面判斷toggle的值是true/false ,然后修改相應(yīng)的class名 
js代碼:

 

		
  1. Page({
  2. data:{
  3. datas:[]
  4. },
  5. onLoad:function(options){
  6. var that=this;
  7. API.my_ajax('',function(res){ //用mock.js 設(shè)置的模擬數(shù)據(jù)調(diào)用格式
  8. // console.log(res);
  9. var listData=res.data;
  10. for(var i=0;i<listData.length;i++){
  11. listData[i]['toggle']=false; //添加toggle 屬性
  12. }
  13. that.setData({
  14. datas:listData
  15. })
  16. // console.log(listData)
  17. })
  18. },
  19. showBtn:function(e){
  20. console.log(e);
  21. console.log(this);
  22. //這兩個(gè)console.log 可以查看當(dāng)前方法里面所有的數(shù)據(jù),可以查找一下我們所需要的內(nèi)容是否有在里面,底下的index 就是這樣找到的
  23.  
  24. var a=e.currentTarget.dataset.index;
  25. var b=this.data.datas[a].toggle;
  26. this.data.datas[a].toggle=!b;
  27.  
  28. //設(shè)置之后我們要把數(shù)據(jù)從新添回去
  29. this.setData({
  30. datas:this.data.datas
  31. })
  32. }
  33. })

wxml代碼:

 

		
  1. <!--使用二維碼按鈕-->
  2. <label class="icon_qrcode_wrap" data-index="{{index}}" bindtap="showBtn">
  3. <text>使用規(guī)格及二維碼</text>
  4. <image class="icon_right" src="../../images/up.png"></image>
  5. </label>
  6.  
  7. <!--彈出二維碼樣式-->
  8. <view class="qrcode_show_wrap {{item.toggle==true ? '':'none'}}">
  9. <view class="qrcode_container">
  10. <!--<image class="qrcode_big_bg"src="../../images/qrcode_black_bg.png"></image>-->
  11. <view class="block_qrcode_wrap">
  12. <image class="tiaoma" src="../../images/tiao_code.png"></image>
  13. <image class="rqcode" src="../../images/rq_code_img.png"></image>
  14. <text style="display:block;" class="fs12">erwr43545</text>
  15. </view>
  16. <text class="rq_code_title cfff fs13">使用規(guī)則</text>
  17. <text class="cfff code_txt">就掉粉絲活生生的誰(shuí)讓他和人文就掉粉絲活生生的誰(shuí)讓他和人文就掉粉絲活生生的誰(shuí)讓他和人文</text>
  18. </view>
  19. </view>
  20. </view>

gyhtr



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://www.u-renovate.com/wxmini/doc/course/18199.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×