微信小程序> [项目]小程序数组操作(上移下移编辑删除)

[项目]小程序数组操作(上移下移编辑删除)

浏览量:535 时间: 来源:哈肉肉可萌了
//编辑--取出指定下标并跳转到编辑页面toRemake:function(){    var questions = [];    questions = wx.getStorageSync('questions');    var index = this.data.idx;    var question = questions[index];    var qType = question.type;    if(qType==='单选'){      wx.navigateTo({        url: '../single/single?idx='+this.data.idx,      })    }else if(qType==='多选'){      wx.navigateTo({        url: '../check/check?idx=' + this.data.idx,      })    }else{      wx.navigateTo({        url: '../input/input?idx=' + this.data.idx,      })    }     },
//删除指定下标的元素,重新保存数组,并刷新页面
 toDelete:function(){    var questions = wx.getStorageSync('questions');    var index = this.data.idx;    questions.splice(index, 1);    wx.setStorageSync("questions", questions);    wx.redirectTo({      url: '../lookServey/lookServey',    })  },
 //上移并刷新页面和下移并刷新页面  toChange:function(){    var questions = this.data.questions;   if(this.data.idx===0){     wx.showToast({       title: '已经在第一位了',       icon:"none"     })   }else{     this.changeUp(questions, this.data.idx);     wx.setStorageSync("questions", questions);     wx.redirectTo({       url: '../lookServey/lookServey',     })   }      },  toChange1: function () {    var questions = this.data.questions;if(this.data.idx===questions.length-1){  wx.showToast({    title: '已经在最后一位了',    icon:'none'  })}else{  this.changeDown(questions, this.data.idx);  wx.setStorageSync("questions", questions);  wx.redirectTo({    url: '../lookServey/lookServey',  })}     },
//上移方法
 changeUp: function(arr, index){    var temp;    if(index===0 || index arr.length - 1) {      return arr;    }    temp = arr[index];    arr[index] = arr[index - 1];    arr[index - 1] = temp;    return arr;   },


//下移方法

 changeDown: function (arr, index) {

   var temp;    if (index === arr.length-1) {      return arr;    }    temp = arr[index];    arr[index] = arr[index +1];    arr[index + 1] = temp;    return arr;  },

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎