收藏
回答

查询到的计数不正确,怎么做?

如图:当在全部项查询的时候,显示正常,切换点击其他项,计数也显示正常。当我在其它项点击查询的时候,显示的计数都是全部项的计数,也就是说,全部项查询的计数是多少,在其他项点击查询的时候显示的都是全部项的计数。如果再切换各项的时候,显示的计数又正常了。请问这个问题怎么解决?

components/segment/wxml代码

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

components/segment/wxml代码

 properties: {
   result :{
     type:Number,
     value:0
   },
 },

index.wxml代码

<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代码
const db = wx.cloud.database();
Page({
  data: {
    items: ["全部""我要找车""我要找人""我找货车""我要找货"],
    hasmoretrue,
    nomorefalse,
    wehicles: [],
    result0,
    startPoint: [],
    goal: [],
    isQueryingfalse,
    list:[],
    listones:[],
    listtwos:[],
    listthrees:[],
    listfours:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  /**
   * 按条件查询数据
   */
  formSubmit(event) {
    console.log(event);
    const that = this;
    that.setData({
      isQueryingtrue
    })
    let list = db.collection("wehicle").where({
      startPoint: that.data.startPoint,
      goal: that.data.goal,
    });
    if (event > 0) {
      list = list.skip(event);
    }
    list.limit(10).orderBy("create_time""desc").get().then(res => {
      console.log(res);
      const wehicles = res.data;
      let nomore = true;
      let hasmore = true
      if (!wehicles) {
        nomore = false
      } else {
        hasmore = 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(10).orderBy("create_time""desc").get().then(res => {
          console.log(res);
        const listones = res.data;
        let nomore = true;
        let hasmore = true
        if (!listones) {
          nomore = false
        } else{
          hasmore = 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
        })
      });
      let list2 = db.collection("wehicle").where({
        startPoint: that.data.startPoint,
        goal: that.data.goal,
        carpool:'我要找人'
      });
      if (event > 0) {
        list2 = list2.skip(event);
      }
      list2.limit(10).orderBy("create_time""desc").get().then(res => {
          console.log(res);
        const listtwos = res.data;
        let nomore = true;
        let hasmore = true
        if (!listtwos) {
          nomore = false
        } else{
          hasmore = 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(10).orderBy("create_time""desc").get().then(res => {
          console.log(res);
        const listthrees = res.data;
        let nomore = true;
        let hasmore = true
        if (!listthrees) {
          nomore = false
        } else{
          hasmore = 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(10).orderBy("create_time""desc").get().then(res => {
          console.log(res);
        const listfours = res.data;
        let nomore = true;
        let hasmore = true
        if (!listfours) {
          nomore = false
        } else{
          hasmore = 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,
        })
      });
      if(list){
        list.count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }
      else if(list1){
        list1.count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }
      else if(list2){
        list2.count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }
      else if(list3){
        list3.count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }
      else{
        list4.count().then(res => {
          that.setData({
            result: res.total,
          })
        });
      }
      
  },
  /**
   * 初始化总计数
   */
  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 => {
          const result = res.total;
          console.log(result)
          that.setData({
            result: result,
          })
        });
      }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 => {
          const result = res.total;
          console.log(result)
          that.setData({
            result: result,
          })
        });
      }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 => {
          const result = res.total;
          console.log(result)
          that.setData({
            result: result,
          })
        });
      }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 => {
          const result = res.total;
          console.log(result)
          that.setData({
            result: result,
          })
        });
      }else{
        db.collection("wehicle").where({
          startPoint: that.data.startPoint,
          goal: that.data.goal,
          carpool:'我要找货车'
        }).count().then(res => {
          that.setData({
            result: res.total
          })
        });
      }  
    }
  },
})
回答关注问题邀请回答
收藏
登录 后发表内容