如图:当在第五个选项卡的位置,下拉刷新或者点击查询的时候,加载的数据是第一个选项卡的初始数据,而不是第三个选项卡的数据。想要的效果是,要么下拉或者点击查询事件之后,选项卡选中第一个选项卡,要么加载相当应选项卡的数据。
代码:
/**
* 获取选项卡
*/
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();
},
默认数据是第一个选项卡,然后操作其他选项卡页面刷新同样回到第一个选项卡数据
this.lowdrelease(index);
但是点击查询的事件,要怎么弄?
<segment-carpool items="{{title}}" result="{{result}}" binditemchengde="onItemChengdeEvent" defaultIndex="0">
<view slot="0" class="segment-page wehicle-page">
<block wx:if="{{isQuerying == true}}">
<vehicle wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></vehicle>
</block>
<block wx:else>
<vehicle wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></vehicle>
</block>
</view>
onPullDownRefresh() {
const index = this.data.currentTabIndex;
this.lowdrelease(index);
this.setData({
isQuerying: false,
})
wx.stopPullDownRefresh();
},就实现了,点击查询事件,这个要怎么改,尝试了很多,都没搞定,不知道怎么弄