效果:
解决思路:
在数据允许范围内,选取四个随机数,然后对数据列表字段重新赋值。
实现:
1)在一次请求接口拿到列表所有数据时,把列表原始数据存储起来。
2)数据格式如下:
[{ActType:"协会活动"Act_Title:"烟花协会"Act_id:"3636445707881152512"ItemType:"1"LOGOURL:"../upload/clubs/0410161650_9dbda401-4b23-4d49-b5a6-aacd1ad08e00.png"Plorder:0Price:"100.00"Title:""},{ActType:"协会活动"Act_Title:"文艺交流晚会"Act_id:"3636445707881152512"ItemType:"1"LOGOURL:"../upload/clubs/0410161650_9dbda401-4b23-4d49-b5a6-aacd1ad08e00.png"Plorder:0Price:"100.00"Title:""}]
3)点击换一换,调用下面方法即可
chooseEvent: function () {let maxNum = this.data.recomendEventRedis.length //计算数据长度let r1 = parseInt(Math.random() * (maxNum - 0) + 0); //取【0-数据长度】内的整数随机数let r2 = parseInt(Math.random() * (maxNum - 0) + 0);let r3 = parseInt(Math.random() * (maxNum - 0) + 0);let r4 = parseInt(Math.random() * (maxNum - 0) + 0); //务必先清空列表数据this.setData({recomendEvent: []})//重新取四组数据this.data.recomendEvent.push(this.data.recomendEventRedis[r1])this.data.recomendEvent.push(this.data.recomendEventRedis[r2])this.data.recomendEvent.push(this.data.recomendEventRedis[r3])this.data.recomendEvent.push(this.data.recomendEventRedis[r4])//重新赋值this.setData({recomendEvent: this.data.recomendEvent})},