“全部”你不用再写一遍,和免费付费一样,给onFilter穿个参数就行了。 onAll: function () { this.onFilter('all') }, onFilter: function (type) { let newData = []; let data = this.data.list; let i = 0; let len = data.length; for (; i < len; i++) { if(type == 'all'){ if (data[i].price != -1){ newData.push(data[i]); } }else if(type == 'cost'){ if (data[i].price > 0){ newData.push(data[i]); } }else{ if (data[i].price == 0){ newData.push(data[i]); } } } this.setData({ list1: newData }) }
还有onLoad里边不用写for,本来就是list里有什么无条件给list1,所以我觉得直接给就行了。 onLoad: function () { let data = this.data.list; this.setData({ list1:data }) }, 目前我测试没问题,我也小白,乱七八糟学了一堆,不对的地方请指教
感谢老师分享
我也是小白,看了第二遍还是似懂非懂的。坚持直到熟练。
代码发不出来,只能试试发图片。
完美解决按钮点击后一片空白的问题。不用复制声明一遍list,如果你有动态数据库,不可能每次都去复制一遍。
onAll: function () {
this.onFilter('all')
},
onFilter: function (type) {
let newData = [];
let data = this.data.list;
let i = 0;
let len = data.length;
for (; i < len; i++) {
if(type == 'all'){
if (data[i].price != -1){
newData.push(data[i]);
}
}else if(type == 'cost'){
if (data[i].price > 0){
newData.push(data[i]);
}
}else{
if (data[i].price == 0){
newData.push(data[i]);
}
}
}
this.setData({
list1: newData
})
}
onLoad: function () {
let data = this.data.list;
this.setData({
list1:data
})
},
目前我测试没问题,我也小白,乱七八糟学了一堆,不对的地方请指教
或者定义一个全局变量,保存所有的课程:
var courses = [{
price: 0,
name: '人人都会小程序',
coverPath: '../../images/cover.png'
},
{
price: 149,
name: '人人都会小程序实战',
coverPath: '../../images/cover.png'
},
{
price: 249,
name: '人人都会小程序实战2',
coverPath: '../../images/cover.png'
}]
然后再onFilter 中赋值:let data = courses;
全部js:
var courses = [{
price: 0,
name: '人人都会小程序',
coverPath: '../../images/cover.png'
},
{
price: 149,
name: '人人都会小程序实战',
coverPath: '../../images/cover.png'
},
{
price: 249,
name: '人人都会小程序实战2',
coverPath: '../../images/cover.png'
}]
Page({
data: {
course_list: [
{
price: 0,
name: '人人都会小程序',
coverPath: '../../images/cover.png'
},
{
price: 149,
name: '人人都会小程序实战',
coverPath: '../../images/cover.png'
},
{
price: 249,
name: '人人都会小程序实战2',
coverPath: '../../images/cover.png'
}
],
},
onFree: function () {
this.onFilter()
},
// 函数
onCost: function () {
this.onFilter('cost')
},
onFilter: function (type) {
let data = courses;
let newData = [];
let i = 0;
for (; i < data.length; i++) {
if (type == 'cost') {
if (data[i].price > 0) {
newData.push(data[i]);
}
} else {
if (data[i].price == 0) {
newData.push(data[i]);
}
}
}
this.setData({
course_list: newData
})
}
})
老师讲的真的很好
遇到了相同的问题,按照老师的教程走的,编译之后点击【付费】可以出来付费内容,再点击免费的时候就空白了,然后再重新编译之后点击【免费】可以出来免费内容,就是不能再点了,js方法都走了,为什么页面会空白吗,请问这js代码里还需要怎么做吗,才能做出来【付费】和【免费】可以相互的切换相应的信息呢
hahhaha,jero老师,2019年寒假学过