m0_62833091 2024-07-12 21:24 采纳率: 66.7%
浏览 3
已结题

当查询获取到的数据没有了,如何让上拉不加载全部项的数据?

img


如图:当查询的时候,下拉刷新或上拉加载时,加载的是查询后的数据,而不是加载全部项的数据。现在的bug就是,当查询后的数据没有了,加载的就是全部项的数据。这个问题怎么解决?
代码:


data: {
    hasmore: true,
    wehicles: [],
    startPoint:'',
    goal:'',
  },
  /**
   * 按条件查询数据
   */
  formSubmit(start){
    const that = this;
    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_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,
      })
    })
  },

/**
   * 获取数据库数据
   */
  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 || !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
      })
    })
  },
onReachBottom() {
    this.lowdrelease(this.data.wehicles.length);
    this.formSubmit(this.data.wehicles.length)
  },
  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-12 21:24
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据你提供的代码和描述,问题出现在当查询后没有数据时,上拉加载(onReachBottom)应该只加载查询后的数据,而不是加载全部数据。这需要确保hasmore变量在没有查询结果时被正确设置为false,从而阻止加载更多数据。

    首先,你的formSubmitlowdrelease函数都设置了hasmore变量,但它们在设置hasmore时使用了不同的条件。在formSubmit中,当查询结果为空时,hasmore被设置为false。而在lowdrelease中,除了当结果为空时,如果结果为undefinednullhasmore也应该被设置为false

    为了解决这个问题,你可以将两个函数中的hasmore设置逻辑统一。这里是一个可能的解决方案:

    1. 统一hasmore的设置逻辑:确保在两种情况下都没有更多数据时,hasmore都被设置为false
    2. 避免重复加载:确保onReachBottom不会同时调用formSubmitlowdrelease,因为它们可能会加载不同的数据集。

    下面是修改后的代码示例:

    // 统一 hasmore 的设置逻辑
    let hasmore = true;
    if (wehicles.length === 0 || !wehicles) {
        hasmore = false;
    }
    
    // 修改 onReachBottom 函数,确保只调用 formSubmit
    onReachBottom() {
        this.formSubmit(this.data.wehicles.length); // 只调用 formSubmit 来加载查询后的数据
    },
    

    请注意,你的代码中可能还需要确保formSubmit函数只在查询条件下调用,而lowdrelease只在没有特定查询条件时调用。这样可以避免在上拉加载时同时加载查询数据和全部数据。

    另外,确保在onReachBottom调用formSubmit时,传递的start参数是正确的,它应该是当前已加载的车辆列表的长度,这样可以实现分页加载。

    最后,如果你的小程序有缓存机制,确保在查询后更新缓存,避免加载旧的数据。

    如果你需要进一步的帮助,或者需要检查其他可能的问题,请随时告诉我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月21日
  • 已采纳回答 7月13日
  • 创建了问题 7月12日

悬赏问题

  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥100 单片机hardfaulr
  • ¥20 手机截图相片分辨率降低一半
  • ¥50 求一段sql语句,遇到小难题了,可以50米解决
  • ¥15 速求,对多种商品的购买力优化问题(用遗传算法、枚举法、粒子群算法、模拟退火算法等方法求解)
  • ¥100 速求!商品购买力最优化问题(用遗传算法求解,给出python代码)
  • ¥15 虚拟机检测,可以是封装好的DLL,可付费