微信小程序(15)--上传图片公用组件(2)

news/2024/7/7 20:25:42 标签: json

接下来开始写写上传图片的公用组件,可以自定义上传几张图片。

chooseImage文件夹里面的index.wxml和index.js,涉及图片上传,删除,预览。

<view class="img-v clearfix">
 <view class="img-chooseImage" wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
  <image src="{{item}}" data-index="{{index}}" mode="aspectFill" bindtap="previewImg" class="list-img"></image>
  <view class="delete-btn" data-index="{{index}}" catchtap="deleteImg"><image  src="../../../image/close.png"></image></view>
 </view>
 <view class="upload-img-btn" bindtap="chooseImg" hidden='{{ishide}}'><image class="add-img" src="../../../image/add-img.jpg"></image></view>
</view>
Component({ 
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    count: String   //父子传参
  },
  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    count:1,
    ishide:false
  },
  // 上传图片
  methods: { 
    chooseImg: function (e) {
      var that = this;
      var imgs = this.data.imgs;
      if (imgs.length >= 9) {
        this.setData({
          lenMore: 1
        });
        setTimeout(function () {
          that.setData({
            lenMore: 0
          });
        }, 2500);
        return false;
      }
      wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          var imgs = that.data.imgs;
          // console.log(tempFilePaths + '----');
          console.log(that.data.count);
          for (var i = 0; i < tempFilePaths.length; i++) {
            if (imgs.length > that.data.count-1) {
              that.setData({
                imgs: imgs
              });
              return false;
            } else {
              imgs.push(tempFilePaths[i]);
              if (imgs.length > that.data.count-1){
                that.setData({
                  ishide: true
                });
              }
            }
          }
          // console.log(imgs);
          that.setData({
            imgs: imgs
          });
          that.triggerEvent("chooseImg");//触发回调
        }
      });
    },
    // 删除图片
    deleteImg: function  (e) {
      var imgs = this.data.imgs;
      var index = e.currentTarget.dataset.index;
      imgs.splice(index, 1);
      this.setData({
        imgs: imgs
      });
      if (imgs.length < this.data.count) {
        // console.log(imgs.length);
        this.setData({
          ishide: false
        });
      }
      this.triggerEvent("deleteImg");//触发回调
    },
    // 预览图片
    previewImg: function  (e) {
      //获取当前图片的下标
      var index = e.currentTarget.dataset.index;
      //所有图片
      var imgs = this.data.imgs;
      wx.previewImage({
        //当前显示图片
        current: imgs[index],
        //所有图片
        urls: imgs
      })
    }
  }
})

index文件夹的index.json和index.wxml和index.js

{
  "navigationBarTitleText": "图片上传",
  "usingComponents": {
    "pop": "../common/chooseImage/index",
    "popnum": "../common/chooseImage/index"    
  }
}
 <view class="perfect-title">图片上传公用组件</view>  
  <view class="pop-box">
    <view class="upload-title">营业执照原件(1张)</view>
      <view style="margin:30rpx;">
        <pop id="pop" bind:chooseImg="chooseImg" bind:deleteImg="deleteImg" bind:previewImg="previewImg" count="{{count}}"></pop>
      </view>
  </view>  
  <view class="pop-box">
    <view class="upload-title">身份证原件正反面(2张)</view>
      <view style="margin:30rpx;">
        <popnum id="popnum" bind:chooseImg="chooseImgnum" bind:deleteImg="deleteImgnum" bind:previewImg="previewImg" count="{{countnum}}"></popnum>
      </view>
  </view> 
  <view class="btn-area" id="buttonContainer2">
    <button type="primary" bindtap="submitBtn">确认</button>
  </view> 
Page({

  /**
   * 页面的初始数据
   */
  data: {
   imgs:[],
   imgsnum: [],
   count:1,
   countnum:2
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获得pop组件
    this.pop = this.selectComponent("#pop");
    this.popnum = this.selectComponent("#popnum");
  },
  chooseImg: function (e) {
    // console.log(this.pop.data.imgs);
    this.setData({
      imgs: this.pop.data.imgs
    })
  },
  deleteImg: function (e) {
    // console.log(this.pop.data.imgs);
    this.setData({
      imgs: this.pop.data.imgs
    })
  },
  chooseImgnum: function (e) {
    // console.log(this.pop.data.imgs);
    this.setData({
      imgsnum: this.popnum.data.imgs
    })
  },
  deleteImgnum: function (e) {
    // console.log(this.pop.data.imgs);
    this.setData({
      imgsnum: this.popnum.data.imgs
    })
  },
  submitBtn: function(){
    console.log(this.data.imgs);
    console.log(this.data.imgsnum);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  }
})

 

转载于:https://www.cnblogs.com/juewuzhe/p/9313928.html


http://www.niftyadmin.cn/n/976007.html

相关文章

SharePoint online Multilingual support - Settings

博客地址&#xff1a;http://blog.csdn.net/FoxDave This post will talk about how to enable sharepoint online site multilingual support. Below are the labels supported by SharePoint online when site language is changed. Site TitleSite DescriptionList TitleLis…

Linux运维文件系统

文章系统和目录结构文件系统的基本介绍文件名的规则以及文件颜色的分类 注&#xff1a;文件名有的可以不区分大小写文件系统的基本结构linux的文件类型centos7和6的目录变化转载于:https://blog.51cto.com/13873505/2149199

「转载」基环树学习笔记

本文转载于cly_none&#xff0c;并在此基础上修改 已征得本人同意并获取markdown源码 原地址 基环树&#xff0c;也是环套树&#xff0c;简单地讲就是树上在加一条边。它形如一个环&#xff0c;环上每个点都有一棵子树的形式。因此&#xff0c;对基环树的处理大部分就是对树处理…

JavaScript 开发的技巧

JavaScript 开发的45个经典技巧 分类 编程技术 JavaScript是一个绝冠全球的编程语言&#xff0c;可用于Web开发、移动应用开发&#xff08;PhoneGap、Appcelerator&#xff09;、服务器端开发&#xff08;Node.js和Wakanda&#xff09;等等。JavaScript还是很多新手踏入编程世界…

Angular系列学习三:父子组件之间的交互(常见的组件通讯场景)

作者&#xff1a;心叶时间&#xff1a;2018-07-24 16:48:56 通过输入型绑定把数据从父组件传到子组件 通过Input()可以实现父组件传递数据给子组件&#xff0c;下面先看看子组件代码&#xff1a; import { Component, Input } from angular/core;Component({selector: child-co…

map按照value排序的方法

1.将map以pair(string,double)的存储方式加入vector中 2.vector的sort()中的第三个参数重载 #typedef pair<string,double> PAIR struct CmpByValue { bool operator()(const PAIR& lhs, const PAIR& rhs) { return lhs.second < rhs.second; } }; map<QS…

python基础学习笔记(六)

学到这里已经很不耐烦了&#xff0c;前面的数据结构什么的看起来都挺好&#xff0c;但还是没法用它们做什么实际的事。 基本语句的更多用法 使用逗号输出 >>> print age:,25 age: 25 如果想要同时输出文本和变量值&#xff0c;却又不希望使用字符串格式化的话&#xf…

Apache多处理模块

介绍 Apache HTTP 服务器被设计为一个功能强大&#xff0c;并且灵活的 web 服务器&#xff0c; 可以在很多平台与环境中工作。不同平台和不同的环境往往需要不同 的特性&#xff0c;或可能以不同的方式实现相同的特性最有效率。Apache httpd 通过模块化的设计来适应各种环境。这…