m0_62833091 2024-07-20 21:30 采纳率: 66.7%
浏览 4
已结题

统计数据库集合总数量

当点击查询后,点击选项卡的时候,计数显示为0。如果在我要找车或者其他选项卡位的时候,显示的全部是初始化状态的计数,
想要的结果,当在相应的选项卡位的时候,点击查询,显示相应的计数。在相应的卡位下拉刷新退出查询状态,显示相应的计数

img


组件代码:

<view class="comment-title">全部信息(共有{{result}}条)</view>


index.wxml代码:

<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}}" result="{{result}}" resultthree="{{result3}}" resultfous="{{result4}}"   binditemchengde="onItemChengdeEvent" defaultIndex="0" >
  <view slot="0" class="segment-page wehicle-page">
    <whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole>
  </view>
  <view slot="1" class="segment-page people-page">
    <wehicle wx:for="{{listones}}" wx:key="key" wx:for-item="listone" list1="{{listone}}"></wehicle>
  </view>
  <view slot="2" class="segment-page train-page">
    <people wx:if="{{listtwos}}" wx:for="{{listtwos}}" wx:key="key" wx:for-item="listtwo" list2="{{listtwo}}"></people>
  </view>
  <view slot="3" class="segment-page money-page">
    <train wx:for="{{listthrees}}" wx:key="key" wx:for-item="listthree" list3="{{listthree}}"></train>
  </view>
  <view slot="4" class="segment-page money-page" >
    <money wx:for="{{listfours}}" wx:key="key" wx:for-item="listfour" list4="{{listfour}}"></money>
  </view>
</segment-carpool> 
<loadingmore hasmore="{{hasmore}}" nomore="{{nomore}}"></loadingmore>


index.js代码:

data: {
    items: ["全部""我要找车""我要找人""我找货车""我要找货"],
    hasmore: true,
    nomore: false,
    wehicles: [],
    result: 0,
    startPoint:'',
    goal: '',
    isQuerying: false,
  },
/**
   * 按条件查询数据
   */
  formSubmit(event) {
    console.log(event);
    const that = this;
    that.setData({
      isQuerying: true
    })
    let list = db.collection("wehicle").where({
      startPoint: that.data.startPoint,
      goal: that.data.goal,
    });
    if (event > 0) {
      list = list.skip(event);
    }
    list.limit(5).orderBy("create_time""desc").get().then(res => {
      const wehicles = res.data;
      let nomore = true;
      let hasmore = true
      if (wehicles.length == 0) {
        hasmore = false
      } else {
        nomore = false
      }
      let newWehicles = [];
      if (event > 0) {
        newWehicles = that.data.wehicles.concat(wehicles);
      } else {
        newWehicles = wehicles;
      }
      newWehicles.forEach((wehicle, index) => {
        wehicle.create_time = wehicle.create_time.toString();
      })
      that.setData({
        wehicles: newWehicles,
        hasmore: hasmore,
        nomore: nomore,
      })
    });
    let list1 = db.collection("wehicle").where({
      startPoint: that.data.startPoint,
      goal: that.data.goal,
      carpool:'我要找车'
    });
      if (event > 0) {
        list1 = list1.skip(event);
      }
      list1.limit(5).orderBy("create_time""desc").get().then(res => {
        const listones = res.data;
        let nomore = true;
        let hasmore = true
        if (listones.length == 0) {
          hasmore = false
        } else {
          nomore = false
        }
        let newones = [];
        if (event > 0) {
          newones = that.data.listones.concat(listones);
        } else {
          newones = listones;
        }
        newones.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listones: newones,
          hasmore: hasmore,
          nomore: nomore
        })
      });
et list2 = db.collection("wehicle").where({
        startPoint: that.data.startPoint,
        goal: that.data.goal,
        carpool:'我要找人'
      });
      if (event > 0) {
        list2 = list2.skip(event);
      }
      list2.limit(5).orderBy("create_time""desc").get().then(res => {
        const listtwos = res.data;
        let nomore = true;
        let hasmore = true
        if (listtwos.length == 0) {
          hasmore = false
        } else {
          nomore = false
        }
        let newtwos = [];
        if (event > 0) {
          newtwos = that.data.listtwos.concat(listtwos);
        } else {
          newtwos = listtwos;
        }
        newtwos.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listtwos: newtwos,
          hasmore: hasmore,
          nomore: nomore
        })
      });
      let list3 = db.collection("wehicle").where({
        startPoint: that.data.startPoint,
        goal: that.data.goal,
        carpool:'我找货车'
      });
      if (event > 0) {
        list3 = list3.skip(event);
      }
      list3.limit(5).orderBy("create_time""desc").get().then(res => {
        const listthrees = res.data;
        let nomore = true;
        let hasmore = true
        if (listthrees.length == 0) {
          hasmore = false
        } else {
          nomore = false
        }
        let newthrees = [];
        if (event > 0) {
          newthrees = that.data.listthrees.concat(listthrees);
        } else {
          newthrees = listthrees;
        }
        newthrees.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listthrees: newthrees,
          hasmore: hasmore,
          nomore: nomore
        })
      });

let list4 = db.collection("wehicle").where({
        startPoint: that.data.startPoint,
        goal: that.data.goal,
        carpool:'我要找货'
      });
      if (event > 0) {
        list4 = list4.skip(event);
      }
      list4.limit(5).orderBy("create_time""desc").get().then(res => {
        const listfours = res.data;
        let nomore = true;
        let hasmore = true
        if (listfours.length == 0) {
          hasmore = false
        } else {
          nomore = false
        }
        let newfours = [];
        if (event > 0) {
          newfours = that.data.listfours.concat(listfours);
        } else {
          newfours = listfours;
        }
        newfours.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listfours: newfours,
          hasmore: hasmore,
          nomore: nomore,
        })
      });
    that.setData({
      startPoint:'',
      goal:'',
    })
  },
  /**
   * 初始化总计数
   */
  oncount(event) {
    const that = this;
      db.collection('wehicle').where({}).count().then(res => {
        that.setData({
          result: res.total
        })
      });
},
  /**
   * 获取选项卡计数
   */
  onItemChengdeEvent(event) {
    console.log(event)
    const that = this;
    var index = event.detail.index;
    if (index == 0) {
      if(!that.data.isQuerying){
        db.collection('wehicle').where({}).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
        }).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      } 
    } else if (index == 1) {
      if(!that.data.isQuerying){
        db.collection('wehicle').where({carpool: '我要找车',}).count().then(res => {
          that.setData({
            result:  res.total,
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
          carpool:'我要找车'
        }).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }   
    } else if (index == 2) {
      if(!that.data.isQuerying){
        db.collection('wehicle').where({carpool: '我要找人',}).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
          carpool:'我要找人'
        }).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      } 
    } else if (index == 3) {
      if(!that.data.isQuerying){
        db.collection('wehicle').where({carpool: '我找货车',}).count().then(res => {
          that.setData({
            result:res.total
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
          carpool:'我找货车'
        }).count().then(res => {
          that.setData({
            result: res.total,
            
          })
        });
      }  
    } else {
      if(!that.data.isQuerying){
        db.collection('wehicle').where({carpool: '我要找货',}).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
          carpool:'我要找货车'
        }).count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }  
    }
  },
  /**
   * 获取数据库数据
   */
  lowdrelease(start = 0) {
    const that = this;
    let list = db.collection("wehicle");
      if (start > 0) {
        list = list.skip(start);
      }
      list.limit(5).orderBy("create_time""desc").get().then(res => {
        const wehicles = res.data;
        let nomore = false;
        let hasmore = true
        if (!wehicles) {
          nomore = true
        }else{
          hasmore = false
        }
        let newWehicles = [];
        if (start > 0) {
          newWehicles = that.data.wehicles.concat(wehicles);
        } else {
          newWehicles = wehicles;
        }
        newWehicles.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          wehicles: newWehicles,
          hasmore: hasmore,
          nomore: nomore
        })
      });
let list1 = db.collection("wehicle").where({carpool:'我要找车'});
      if (start > 0) {
        list1 = list1.skip(start);
      }
      list1.limit(5).orderBy("create_time""desc").get().then(res => {
        const listones = res.data;
        let nomore = false;
        let hasmore = true
        if (!listones) {
          nomore = true
        }else{
          hasmore = false
        }
        let newones = [];
        if (start > 0) {
          newones = that.data.listones.concat(listones);
        } else {
          newones = listones;
        }
        newones.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listones: newones,
          hasmore: hasmore,
          nomore: nomore
        })
      });

      let list2 = db.collection("wehicle").where({carpool:'我要找人'});
      if (start > 0) {
        list2 = list2.skip(start);
      }
      list2.limit(5).orderBy("create_time""desc").get().then(res => {
        const listtwos = res.data;
        let nomore = false;
        let hasmore = true
        if (!listtwos) {
          nomore = true
        }else{
          hasmore = false
        }
        let newtwos = [];
        if (start > 0) {
          newtwos = that.data.listtwos.concat(listtwos);
        } else {
          newtwos = listtwos;
        }
        newtwos.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listtwos: newtwos,
          hasmore: hasmore,
          nomore: nomore
        })
      });
let list3 = db.collection("wehicle").where({carpool:'我找货车'});
      if (start > 0) {
        list3 = list3.skip(start);
      }
      list3.limit(5).orderBy("create_time""desc").get().then(res => {
        const listthrees = res.data;
        let nomore = false;
        let hasmore = true
        if (!listthrees) {
          nomore = true
        }else{
          hasmore = false
        }
        let newthrees = [];
        if (start > 0) {
          newthrees = that.data.listthrees.concat(listthrees);
        } else {
          newthrees = listthrees;
        }
        newthrees.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listthrees: newthrees,
          hasmore: hasmore,
          nomore: nomore
        })
      });
      let list4 = db.collection("wehicle").where({carpool:'我要找货'});
      if (start > 0) {
        list4 = list4.skip(start);
      }
      list4.limit(5).orderBy("create_time""desc").get().then(res => {
        const listfours = res.data;
        let nomore = false;
        let hasmore = true
        if (!listfours) {
          nomore = true
        }
        else{
          hasmore = false
        }
        let newfours = [];
        if (start > 0) {
          newfours = that.data.listfours.concat(listfours);
        } else {
          newfours = listfours;
        }
        newfours.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        })
        that.setData({
          listfours: newfours,
          hasmore: hasmore,
          nomore: nomore
        })
      }); 
  },
/**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.oncount();
    this.lowdrelease();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.lowdrelease(0);
    this.oncount();
    this.setData({
      isQuerying:false
    })
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
      if (!this.data.isQuerying) {
        this.lowdrelease(this.data.wehicles.length);    
      }else{
        this.formSubmit(this.data.wehicles.length)
      }
  },
})
  • 写回答

4条回答 默认 最新

  • 阿里嘎多学长 2024-07-20 21:30
    关注

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


    要解决你提到的问题,我们可以对现有的代码进行一些调整和优化。以下是一些具体的建议和代码示例:

    1. 更新 formSubmit 函数

    formSubmit 函数中,我们需要根据不同的选项卡索引来设置查询条件,并调用数据库查询。同时,我们需要重置计数和数据。

    formSubmit(event) {
      const that = this;
      that.setData({
        isQuerying: true
      });
    
      const index = that.data.items.findIndex(item => item === that.data.currentTab);
      let queryCondition = {};
      if (index === 0) {
        queryCondition = {};
      } else {
        queryCondition.carpool = that.data.items[index];
      }
    
      const list = db.collection("wehicle").where(queryCondition);
      if (event > 0) {
        list = list.skip(event);
      }
      list.limit(5).orderBy("create_time", "desc").get().then(res => {
        const wehicles = res.data;
        let nomore = true;
        let hasmore = true;
        if (wehicles.length === 0) {
          hasmore = false;
        } else {
          nomore = false;
        }
        let newWehicles = [];
        if (event > 0) {
          newWehicles = that.data.wehicles.concat(wehicles);
        } else {
          newWehicles = wehicles;
        }
        newWehicles.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        });
        that.setData({
          wehicles: newWehicles,
          hasmore: hasmore,
          nomore: nomore,
          isQuerying: false
        });
    
        that.updateCount(index);
      });
    },
    

    2. 更新 onItemChengdeEvent 函数

    onItemChengdeEvent 函数中,我们需要根据选项卡的索引来更新页面数据和计数。

    onItemChengdeEvent(event) {
      const that = this;
      const index = event.detail.index;
      that.setData({
        currentTab: that.data.items[index]
      });
    
      if (!that.data.isQuerying) {
        that.updateCount(index);
      } else {
        that.formSubmit(0);
      }
    },
    

    3. 更新 onShowonPullDownRefresh 函数

    onShowonPullDownRefresh 函数中,我们需要调用初始化计数和数据加载的函数。

    onShow() {
      this.updateCount(0);
      this.lowdrelease(0);
    },
    
    onPullDownRefresh() {
      this.updateCount(0);
      this.lowdrelease(0);
      this.setData({
        isQuerying: false
      });
      wx.stopPullDownRefresh();
    },
    

    4. 添加 updateCount 函数

    添加一个新的函数 updateCount 来根据当前选项卡索引更新计数。

    updateCount(index) {
      const that = this;
      const queryCondition = {};
      if (index === 0) {
        queryCondition = {};
      } else {
        queryCondition.carpool = that.data.items[index];
      }
    
      db.collection("wehicle").where(queryCondition).count().then(res => {
        that.setData({
          result: res.total
        });
      });
    },
    

    5. 更新 lowdrelease 函数

    lowdrelease 函数中,我们需要根据不同的选项卡索引来加载数据。

    lowdrelease(start = 0) {
      const that = this;
      const index = that.data.items.findIndex(item => item === that.data.currentTab);
      let list = db.collection("wehicle").where({ carpool: that.data.currentTab });
      if (start > 0) {
        list = list.skip(start);
      }
      list.limit(5).orderBy("create_time", "desc").get().then(res => {
        const wehicles = res.data;
        let nomore = false;
        let hasmore = true;
        if (!wehicles) {
          nomore = true;
        } else {
          hasmore = false;
        }
        let newWehicles = [];
        if (start > 0) {
          newWehicles = that.data.wehicles.concat(wehicles);
        } else {
          newWehicles = wehicles;
        }
        newWehicles.forEach((wehicle, index) => {
          wehicle.create_time = wehicle.create_time.toString();
        });
        that.setData({
          wehicles: newWehicles,
          hasmore: hasmore,
          nomore: nomore
        });
      });
    },
    

    通过上述修改,你应该能够解决当前遇到的问题,并实现预期的功能。希望这些修改对你有所帮助!

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

报告相同问题?

问题事件

  • 系统已结题 7月31日
  • 已采纳回答 7月23日
  • 创建了问题 7月20日

悬赏问题

  • ¥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,可付费