收藏
回答

查询后的数据 加载完之后,上拉会加载全部项的数据?

问题:如图,通过条件查询后得到是数据,列表中可以正常显示,当查询的数据没有之后,会继续加载全部项的数据,并且查询得到的数据会重新加载一次。

想要的效果:当查询得到的数据加载完之后,应该不再加载数据。显示没有更多数据。如果没有查询操作,正常显示并加载全部项的数据。更多代码可查看代码片段。求大师指点

WXML代码:
<view class="carpool-title">
  <text class="title-text">百姓同城拼车网-同城拼车</text>
</view>
<view class="ad-group">
  <image class="ad-image" src="../../images/专注.jpg" mode="" />
</view>
<form bindsubmit="formSubmit">
  <view class="search-input-group">
    <view class="input-group">
      <input class="search-input" placeholder="出发地" value="{{startPoint}}" bindinput="onStartPointEvent" />
      <image class="thumbnail" src="../../images/return.png" />
      <input class="search-input" placeholder="终点" value="{{goal}}" bindinput="onGoalEvent"  />
    </view>
    <button class="search-button" size="mini" formType="submit">查询</button>
  </view>
</form>
<segment-carpool items="{{items}}" countwe="{{result}}" binditemchengde="onItemChengdeEvent" defaultIndex="0" >
  <view slot="0" class="segment-page wehicle-page">
    <wehicle wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle>
  </view>
  <view slot="1" class="segment-page people-page">
    <wehicle wx:if="{{wehicle.carpool == '我要找车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle>
  </view>
  <view slot="2" class="segment-page train-page">
    <wehicle wx:if="{{wehicle.carpool == '我要找人'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle>
  </view>
  <view slot="3" class="segment-page money-page">
    <wehicle wx:if="{{wehicle.carpool == '我找货车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle>
  </view>
  <view slot="4" class="segment-page money-page" >
    <wehicle wx:if="{{wehicle.carpool == '我要找货'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle>
  </view>
</segment-carpool>
<loadingmore wx:if="{{wehicles.startPoint && wehicles.goal}}" hasmore="{{hasmore}}"></loadingmore>
<loadingmore else hasmore="{{hasmore}}"></loadingmore>

JS代码:

const db = wx.cloud.database();
const _ = db.command
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: ["全部""我要找车""我要找人""我找货车""我要找货"],
    hasmore: true,
    wehicles: [],
    result: 0,
    startPoint:'',
    goal:'',
    isQuerying:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.oncount();
  },
  onStartPointEvent(event){
    const that = this;
    const startPoint = event.detail.value;
    console.log('起点',startPoint);
    that.setData({
      startPoint:startPoint
    })
  },
  onGoalEvent(event){
    const that = this;
    const goal = event.detail.value;
    console.log('终点',goal);
    that.setData({
      goal:goal
    })
  },
  /**
   * 按条件查询数据
   */
  formSubmit(start){
    const that = this;
    that.setData({
      isQuerying:true
    })
    let promise = db.collection("wehicle").where({
        startPoint:that.data.startPoint,
        goal:that.data.goal,
      });
      if(start > 0){
        promise = promise.skip(start);
      }
      promise.limit(10).orderBy("create_itme","desc").get().then(res => {
        const wehicles = res.data;
        let hasmore = true;
        if(wehicles.length == 0 || !wehicles){
          hasmore = false
        }
        let newWehicles = [];
        if (start > 0) {
            newWehicles = that.data.wehicles.concat(wehicles);
        } else {
          newWehicles = wehicles;
        }
        // console.log(wehicles);
        newWehicles.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
            wehicles: newWehicles,
            hasmore: hasmore,
            isQuerying:false
          })
      })
      
  },
  oncount() {
    const that = this;
    db.collection('wehicle').count().then(res => {
      that.setData({
        result: res.total
      })
    });
  },
 /**
   * 获取数据库的总数量
   */ 
  onItemChengdeEvent(event) {
    // console.log(event)
    const that = this;
    var index = event.detail.index;
    let counts = db.collection('wehicle');
    const carpool1 = counts.where({carpool: '我要找车',}).count();
    const carpool2 = counts.where({carpool: '我要找人',}).count();
    const carpool3 = counts.where({carpool: '我找货车',}).count();
    const carpool4 = counts.where({carpool: '我要找货',}).count();
    let newcarpool = []; 
    if (index == 1) {
      newcarpool = carpool1.then(res => {
        const result = res.total;
        console.log(result)
        that.setData({
          result: result,
        })
      });
    } else if (index == 2) {
      newcarpool = carpool2.then(res => {
        const result = res.total;
        console.log(result)
        that.setData({
          result: result,
        })
      });
    } else if (index == 3) {
      newcarpool = carpool3.then(res => {
        const result = res.total;
        console.log(result)
        that.setData({
          result: result,
        })
      });
    } else if (index == 4){
      newcarpool = carpool4.then(res => {
        const result = res.total;
        console.log(result)
        that.setData({
          result: result,
        })
      });
    }else{
      newcarpool = counts.count().then(res=>{
        const result = res.total;
        console.log(result)
        that.setData({
          result: result,
        })
      })
    }
  },
  /**
   * 获取数据库数据
   */
  lowdrelease(start = 0) {
    const that = this;
    let promise = db.collection("wehicle");
    if (start > 0) {
      promise = promise.skip(start);
    }
    promise.limit(10).orderBy("create_time""desc").get().then(res => {
      //   console.log(res);
      const wehicles = res.data;
      let hasmore = true;
      if (wehicles.length == 0 ) {
        hasmore = false
      }
      let newWehicles = [];
      if (start > 0) {
          newWehicles = that.data.wehicles.concat(wehicles);
      } else {
        newWehicles = wehicles;
      }
      // console.log(wehicles);
      newWehicles.forEach((wehicle, index) => {
        wehicle.create_time = wehicle.create_time.toString();
      })
      
      that.setData({
        wehicles: newWehicles,
        hasmore: hasmore
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.lowdrelease();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.lowdrelease(0);
    wx.stopPullDownRefresh();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {   
    // this.formSubmit(this.data.wehicles.length)
    if(this.data.isQuerying){
      this.formSubmit(this.data.wehicles.length)
    }else{
      this.lowdrelease(this.data.wehicles.length);
    }
  },

})
回答关注问题邀请回答
收藏
登录 后发表内容