当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序 动态绑定数据及动态事件处理

微信小程序 动态绑定数据及动态事件处理

2019年03月20日  | 移动技术网IT编程  | 我要评论

微信小程序 动态绑定数据及动态事件处理

关键核心代码

<image class="midimage" data-type="{{item.type}}" data-billcode="{{item.billcode}}" data-src="{{item.imgurl}}"
 src="{{item.imgurl}}" mode="scaletofill" bindtap="addtaskimg" bindlongtap="imglongtap" ></image>
var objurl= json.parse(res.data);
        //重置图片参数
        var temppostionlist=that.data.postionlist;
        for (var i=0;i<temppostionlist.length;i++)
        {
          if (temppostionlist[i]["type"]==e.currenttarget.dataset.type){           
           temppostionlist[i]["imgurl"]=temppaths;
           temppostionlist[i]["serverimgurl"]=objurl.body.data;
           break;
          }            
        } 
        that.setdata( {  
             postionlist:temppostionlist  
        })

利用image的data-type,可以在js后台中知道是点击了哪个image,上传图片后再循环得出相等data-type的数据项进行赋值,这样就能动态给相应的标签赋值

addtaskimg 为统一的动态事件

主要代码如下

.wxml

 <scroll-view class="center" scroll-y="true">
      <view class="midcenter" wx:for="{{postionlist}}">
      <view class="mid_top" >
          <image class="smallimage" src="../images/my/xxh/line_title.png" mode="aspectfit" ></image>
          <text>{{item.keyvalue}}</text>
      </view>
      <view class="mid_center">
          <text>{{item.remark}}</text>
      </view>
      <view class="mid_bottom">
          <image class="midimage" data-type="{{item.type}}" data-billcode="{{item.billcode}}" data-src="{{item.imgurl}}"
          src="{{item.imgurl}}" mode="scaletofill" bindtap="addtaskimg" bindlongtap="imglongtap" ></image>
      </view>      
      </view>
    </scroll-view>

.js

addtaskimg:function(e){   
  //添加选择图片  
  if (this.data.blongtap)
  {
   //处理如果是长按,则不再执行下面的
   this.setdata({
    blongtap:false
   });
   return;
  }
  var that = this;
   wx.chooseimage({
    count:1, //默认1张
    success:function(res){
      //先上传至服务器,再返回路径供保存
      var temppaths=res.tempfilepaths[0];//+".jpg";
      wx.uploadfile({
       url:app.globaldata.ghost+"ywtask/putuploadfile?billcode="+that.data.detail.billcode+"&id="+that.data.id,
       filepath:temppaths,
       name:'image',
       formdata:{},
       success:function(res){
        //res.data返回的是一个字符串,需进行转换成objcet,wx不识别eval函数
        console.log(res.data);
        var objurl= json.parse(res.data);
        //重置图片参数
        var temppostionlist=that.data.postionlist;
        for (var i=0;i<temppostionlist.length;i++)
        {
          if (temppostionlist[i]["type"]==e.currenttarget.dataset.type){           
           temppostionlist[i]["imgurl"]=temppaths;
           temppostionlist[i]["serverimgurl"]=objurl.body.data;
           break;
          }            
        } 
        that.setdata( {  
             postionlist:temppostionlist  
        })        
       }
      })
    }
   })
 }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网