如图布局图,在点击查询后,在查询的状态下,下拉刷新,没有退出查询状态,下拉刷新失效,在第二次下拉才生效。在查询状态下,切换到其他页面,再回到该页面,没有回到初始页面,一直保持在查询状态下。
想要的效果是,在查询状态下,下拉刷新退出查询状态,回到初始页面状态,从其他页面切换到该页面,回到初始页面。
代码:
/**
* 获取选项卡
*/
onItemChengdeEvent(event) {
var index = event.detail.index;
// console.log(index);
this.setData({
currentIndex: index
});
if (!this.data.isQuerying) {
this.lowdrelease();
} else {
this.onchaxun()
}
},
/**
* 获取数据库数据
*/
async loadData(start = 0, vehicle,newQuery = {}) {
const that = this;
const index = that.data.currentIndex;
console.log(index);
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)
if (that.data.isQuerying) {
newQuery = _.and(query,{
startPoint: db.RegExp({
regexp: '^' + that.data.startPoint
}),
goal: db.RegExp({
regexp: '^' + that.data.goal
}),
})
} else {
newQuery = query
}
console.log('条件查询', newQuery)
try {
let promise = db.collection(vehicle).where(newQuery);
if (start > 0) {
promise = promise.skip(start);
}
promise = promise.limit(2)
.orderBy("redden.reddenStartTime", 'desc')
.orderBy("topping.toppingStarttime", "desc")
.orderBy("create_time", "desc");
const vehiclesRes = await promise.get();
const vehicles = vehiclesRes.data;
console.log('获取数据库数据', vehicles)
const countResult = await promise.count();
const total = countResult.total;
console.log('总计数', total)
const newVehicles = start > 0 ? this.data.vehicles.concat(vehicles) : vehicles;
newVehicles.forEach((vehicle, index) => {
vehicle.create_time = vehicle.create_time.toString();
});
const hasmore = false;
return {
newVehicles,
total,
hasmore,
};
} catch (error) {
console.log('failed to load data:', error);
throw error;
}
},
/**
* 获取初始数据
*/
async lowdrelease(start = 0) {
const that = this;
try {
const {
newVehicles: vehicles,
total: vehiclesTotal,
hasmore: hasmore
} = await that.loadData(start, 'vehicle');
that.setData({
vehicles: vehicles,
hasmore: hasmore,
result: vehiclesTotal,
})
} catch (error) {
console.log('failed to load data:', error);
throw error;
}
},
/**
* 点击查询事件
*/
async onchaxun(start = 0) {
const that = this;
that.setData({
isQuerying:true
})
try {
const {
newVehicles: vehicles,
total: vehiclesTotal,
hasmore: hasmore
} = await that.loadData(start, 'vehicle');
that.setData({
startPointStr: '',
goalStr: '',
vehicles: vehicles,
hasmore: hasmore,
result: vehiclesTotal,
});
} catch (error) {
console.log('failed to load data:', error);
throw error;
}
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
this.lowdrelease(0);
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
this.lowdrelease(0);
this.setData({
isQuerying: false
})
wx.stopPullDownRefresh();
},
下拉的时候,你把之前的数据清空(做下监控),下拉后把默认数据赋值,你切换页面如果还是首次查询的数据,说明切换页面首次页面没有再次加载
查询接口时加一个全局loading