1. 程式人生 > >小程式獲取各種資料值跟設定資料值

小程式獲取各種資料值跟設定資料值

修改設定data陣列中的某個值: 

//小程式接收後臺資料若為陣列時,下標最好為預設索引陣列,從0開始排序

 var articleId ='1'//對應陣列中的索引值
 var thisArticle = "articleList[" + articleId + "].iscollection"
     console.log(thisArticle);
     that.setData({
     //必須加[], 
       [thisArticle]: ishow 
     })
     //若為索引值為定值:egarticleList[0].iscollection,則直接寫,不用加[],必須加""
      that.setData({
       "egarticleList[0].iscollection": ishow 
     })

小程式修改本頁面data中陣列中的某個值 :

//小程式data中的某個值若為陣列時,下標最好為**關聯**陣列
Page({
  data: {
    teacherInfo:{
      rachel1:{
        name:"rachel",
        decoration: "加拿大人,兩年多的線上英語教學經驗友善耐心,為人隨和。擅長烹飪喜歡影視音樂、跳舞更多",
        audioImg: "/ico_play.png",
        headImg:"/teacher.jpg",
        audioSrc:"/001.mp3",
      },
      rachel2:{
        name: "rachel",
        decoration: "加拿大人,兩年多的線上英語教學經驗友善耐心,為人隨和。擅長烹飪喜歡影視音樂、跳舞更多",
        audioImg: "/ico_play.png",
        headImg: "/teacher.jpg",
        audioSrc: "/230.mp3",
      },
    },

  },
  play:function(event){
      var that = this;
      var id = event.currentTarget.dataset.id;
      console.log(id);
      var thisTeacher_autioImg = "teacherInfo."+id+".audioImg";
      console.log(thisTeacher_autioImg);

      //解析:
      thisTeacher_autioImg 若寫為這種[]格式("teacherInfo[" + id+ "].audioImg"),則直接修改整個陣列,因為[]中只能是數字,若為"teacherInfo[3].audioImg",則teacherInfo陣列變為4個值:
      teacherInfo:{0:null,1:null,2:null,3:{audioImg:"/ico_playing.png"}}
      //解析結束

      that.setData({
          [thisTeacher_autioImg]: "/ico_playing.png",
          playStatus: "pause",
        })
    },
   });

獲取陣列中某個變數的值:

var id = event.currentTarget.dataset.id;
 var a = that.data.teacherInfo[id].decoration;

獲取自定義屬性 data-id 的值: 

event.currentTarget.dataset.id

小程式獲取頁面傳值id 的值: 

onLoad: function (options) {
    var that = this
    var articleId = options.id//獲取文章的id值

  },

小程式獲取input框輸入: 

wxml:
<input type="number"  placeholder="請輸入手機號" bindinput="bindPhone" maxlength="11"/>

js:
bindPhone:function(e){
      this.setData({
        phone:e.detail.value
      })
  },