如图,5个选项,当在第5个选项位置时,下拉刷新或点击查询事件,获取到的是第一个选项卡的计数和数据而不是第5个选项卡的计数和数据。想要的效果:要么获取对应选项卡的计数和数据,要么默认第一个选项卡
js代码:
/**
* 获取选项卡
*/
onItemChengdeEvent(event) {
var index = event.detail.index;
this.setData({
currentTabIndex: index
});
if(!this.data.isQuerying){
this.lowdrelease(index);
}else{
this.onchaxun(index);
}
},
/**
* 获取数据库数据
*/
async lowdrelease(index,start = 0) {
const that = this;
let query = {};
switch (index) {
case 0:
break;
case 1:
query = {
carpool: '我要找车'
};
break;
case 2:
query = {
carpool: '我要找人'
};
break;
case 3:
query = {
carpool: '我找货车'
};
break;
case 4:
query = {
carpool: '我要找货'
};
break;
}
console.log('carpool条件',query)
let promise = db.collection("wehicle").where(query);
if (start > 0) {
promise = promise.skip(start);
}
// 限制每次获取数据数量并排序
promise = promise.limit(5)
.orderBy("redden.reddenStartTime", 'desc')
.orderBy("topping.toppingStarttime", "desc")
.orderBy("create_time", "desc");
const wehiclesRes = await promise.get();
const wehicles = wehiclesRes.data;
console.log('获取数据库数据',wehicles)
// 统计符合条件的总数而不是所有数据
const countResult = await promise.count();
console.log('总计数',countResult)
// 合并当前获取的数据
const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles;
newWehicles.forEach((wehicle, index) => {
wehicle.create_time = wehicle.create_time.toString();
});
// const hasmore = false;
const hasmore = countResult.total > (that.data.wehicles.length + start);
that.setData({
wehicles: newWehicles,
hasmore: hasmore,
result: countResult.total
});
},
/**
* 点击查询事件
*/
async onchaxun(index,start = 0) {
const that = this;
that.setData({
isQuerying: true,
})
const whereRes = {
startPoint: db.RegExp({ regexp: that.data.startPoint}),
goal: db.RegExp({ regexp: that.data.goal}),
}
console.log('查询事件起点和终点条件',whereRes)
let query = {};
switch (index) {
case 0:
break;
case 1:
query = {carpool: '我要找车'};
break;
case 2:
query = {carpool: '我要找人'};
break;
case 3:
query = {carpool: '我找货车'};
break;
case 4:
query = {carpool: '我要找货'};
break;
}
console.log('查询事件查询条件',query)
let promise = db.collection("wehicle").where(_.and(whereRes,query))
if (start > 0) {
promise = promise.skip(start)
}
// 限制每次获取数据数量并排序
promise = promise.limit(2)
.orderBy("redden.reddenStartTime", 'desc')
.orderBy("topping.toppingStarttime", "desc")
.orderBy("create_time", "desc");
const wehiclesRes = await promise.get();
const wehicles = wehiclesRes.data;
console.log('查询事件获取数据库数据',wehicles)
// 统计符合条件的总数而不是所有数据
const countResult = await promise.count();
console.log('查询事件总计数',countResult)
// 合并当前获取的数据
const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles;
newWehicles.forEach((wehicle, index) => {
wehicle.create_time = wehicle.create_time.toString();
});
// const hasmore = false;
const hasmore = countResult.total > (that.data.wehicles.length + start);
that.setData({
startPointStr: '',
goalStr: '',
wehicles: newWehicles,
hasmore: hasmore,
result:countResult.total,
});
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
this.lowdrelease(0);
this.setData({
isQuerying: false
})
wx.stopPullDownRefresh();
},
/**
* 页面上拉触底事件的处理函数
*/
async onReachBottom() {
let hasmore = true;
if (this.data.wehicles.length == 0) {
hasmore = false
}
this.setData({
hasmore: hasmore
})
if (!this.data.isQuerying) {
const currentIndex = this.data.currentTabIndex;
this.lowdrelease(currentIndex, this.data.wehicles.length);
} else {
const currentIndex = this.data.currentTabIndex;
this.onchaxun(currentIndex,this.data.wehicles.length);
}
},