微信小程序已做事件的回顾修改

**

遇到的问题描述

**
因为是初学者,在做小程序的项目,所以被这个事件难住了一会,最终解决了这个问题,这里记录一下。

展示问题

微信小程序已做事件的回顾修改_第1张图片
我定义了三数组个spots,vehicles,playcontents 来存每次的操作,
定义 了index 来记录添加的个数,然后利用下一站 index+1,上一站index-1
的方式进行数组索引,成功的完成了功能。以下是代码。

 <view class="areaset">
            <textarea class="textareaset" placeholder="输入景点(请包含城市名称)" bindinput="spotset"  value="{
     {spot}}"></textarea>
            <textarea class="textareaset" placeholder="交通选择:(公交,驾车,骑行,地铁,火车)" bindinput="vehicleset" value="{
     {vehicle}}"></textarea>
          </view>
          <view class="describe">
          <textarea class="describewrite" placeholder="输入游玩描述" maxlength="-1" bindinput="contentset" value="{
     {playcontent}}"></textarea>
          </view>
          <view  class="buttonset">
           <button class="buttonwrite2" bindtap="lastplay">上一站</button>
           <button class="buttonwrite" bindtap="nextplay">下一站</button>
          </view>
 //上一站
  lastplay:function(){
     
    let self=this;
    let index= self.data.index;
    index=index-1;
    if(index<0){
     
      wx.showModal({
     
        title:"已到达第一站"
      })
    }else{
     
     self.setData({
     
       spot:self.data.spots[index],
       vehicle:self.data.vehicles[index],
       playcontent:self.data.playcontents[index],
       index:index,
     })
    }
  },
  //下一站
  nextplay:function(){
     
     let self=this;
     let spot=self.data.spot;
     let vehicle=self.data.vehicle;
     let playcontent=self.data.playcontent;
     let index =self.data.index;
     if(typeof spot == "undefined" || spot == null ||spot == ""||
     typeof vehicle == "undefined" || vehicle == null ||vehicle == ""||
     typeof playcontent == "undefined" ||playcontent == null ||playcontent == ""
     ){
     
      wx.showModal({
     
        title:"有数据未填写",
      })
     }else{
     
      wx.showToast({
     
        title: '填充完成',
        success:function(){
     
       self.getmapimage();
        if(index<=self.data.spots.length-1){
     
          self.lastnextplay();
        }else{
     
          self.addarray();
        }
        }
      }) 
     }; 
  },
  //上一站的下一站,修改数组
  lastnextplay:function(){
     
  let self=this;
  let index=self.data.index;
  let spot=self.data.spot;
  let vehicle=self.data.vehicle;
  let playcontent=self.data.playcontent;
  self.changetoloa(spot);
  self.data.spotloa.splice(index,1,self.data.changeitem);
  self.data.spots.splice(index,1,spot);
  self.data.vehicles.splice(index,1,vehicle);
  self.data.playcontents.splice(index,1,playcontent);
  var spots=self.data.spots;
  var vehicles=self.data.vehicles;
  var playcontents=self.data.playcontents;
  var spotloa=self.data.spotloa;
  var play= playcontents[index+1];
  var vs= vehicles[index+1];
  var sp = spots[index+1];
  self.setData({
     
    spots:spots,
    spotloa:spotloa,
    vehicles:vehicles, 
    playcontents:playcontents,
    spot:typeof sp=='undefined'?'':sp,
    vehicle:typeof vs=='undefined'?'':vs,
    playcontent:typeof play=='undefined'?'':play,
    index:index+1,
  })
  },
  //添加进数组
  addarray:function(){
     
  let self=this;
  let spot=self.data.spot;
  let vehicle=self.data.vehicle;
  let playcontent=self.data.playcontent;
  var index=self.data.index;
  self.changetoloa(spot);
  self.data.spotloa.push(self.data.changeitem);
  self.data.spots.push(spot);
  self.data.vehicles.push(vehicle);
  self.data.playcontents.push(playcontent);
  index=index+1;
  var spots=self.data.spots;
  var vehicles=self.data.vehicles;
  var playcontents=self.data.playcontents;
  var spotloa=self.data.spotloa;
  self.setData({
     
    spots:spots,
    spotloa:spotloa,
    vehicles:vehicles, 
    playcontents:playcontents,
    index:index,
    spot:"",
    vehicle:"",
    playcontent:"",
  })
  },

为了在写的时候理清思路,我把原本一个函数可以解决的问题分成了三个,方便查错,与序号索引。

你可能感兴趣的:(小程序,数组,小程序)