小程序图片上传,长按删除,weui

class="weui-cells">
 
  class="weui-cell">
 
    class="weui-cell__bd">
 
      class="weui-uploader">
 
        class="weui-uploader__hd">
 
          class="weui-uploader__title">图片上传
 
          class="weui-uploader__info">{{files.length}} / 9
 
        
 
        class="weui-uploader__bd">
 
          class="weui-uploader__files" id="uploaderFiles">
 
            for="{{files}}" wx:key="*this">
 
              class="weui-uploader__file" bindtap="previewImage" bindlongpress="deleteImage" id="{{item}}" data-index="{{index}}">
 
                class="weui-uploader__img" src="{{item}}" mode="aspectFill"/>
 
              
 
            
 
          
 
          if="{{files.length < 9}}" class="weui-uploader__input-box" >
 
            class="weui-uploader__input" bindtap="chooseImage">
 
          
 
        
 
      
 
    
 
  
Page({
  data: {
    files: []
  },
  chooseImage: function(e) {
    var that = this;    var images = that.data.files;
    wx.chooseImage({
      count: 9 - images.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
      }
    })
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.id,
      urls: this.data.files
    })
  },
  deleteImage: function(e) {
    var that = this;    var images = that.data.files;    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗?',
      success: function(res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
            return false;
        }
        that.setData(
          files: images
        });
      }
    })
  }
})

你可能感兴趣的:(小程序图片上传,长按删除,weui)