- 上拉刷新,加载的数据不是对应选项卡的内容?
如图:当在第五个选项卡的位置,下拉刷新或者点击查询的时候,加载的数据是第一个选项卡的初始数据,而不是第三个选项卡的数据。想要的效果是,要么下拉或者点击查询事件之后,选项卡选中第一个选项卡,要么加载相当应选项卡的数据。 [图片] 代码: /** * 获取选项卡 */ 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(); },
2024-11-06 - 上拉加载更多失效?
原来的代码是查询了5次才实现的效果,想优化代码,优化之后,遇到两个问题,1、上拉加载失效,加载不出数据。2、第二个问题是,总计数,当上拉加载后,计数被改了,显示的是数据库全部的数量,如图。求大神指点,是什么问题? [图片] [图片] 代码: /** * 获取数据库数据 */ 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 } let promise = db.collection("wehicle").where(query); if (start > 0) { promise = promise.skip(start); console.log(promise) } promise.limit(5) .orderBy("redden.reddenStartTime",'desc') .orderBy("topping.toppingStarttime","desc") .orderBy("create_time", "desc") .get().then(res => { console.log(res); const wehicles = res.data; promise.count().then(res => { that.setData({ result: res.total }); }); let hasmore = false let newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles; newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }); that.setData({ wehicles:newWehicles, hasmore:hasmore, }) }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { let hasmore = true; if(this.data.wehicles.length == 0){ hasmore = false } this.setData({ hasmore:hasmore }) if(!this.data.isQuerying) { this.lowdrelease(this.data.wehicles.length); }else{ this.onchaxun(this.data.listzeros.length); } },
2024-10-25 - 想实现一个动态样式,让文字颜色改变?
实现效果:当点击变红按钮弹窗,点击确定后,如果数据库有redden这个字段,就让文字变成红色,如果没有就不变。现在写的代码是,文字颜色没有改变,是什么问题? wxml代码: <view class="left" style="color:{{color}};" > <view class="top-group"> <view class="top" wx:if="{{wehicle.istoppinged}}">顶</view> <view class="start-time">出发:{{wehicle.day}} {{wehicle.times}}</view> </view> <view class="distance-group"> <view class="distance">{{wehicle.startPoint}}</view> <view class="distance">→</view> <view class="distance">{{wehicle.goal}}</view> <view class="distance" wx:if="{{wehicle.carpool == '我要找人' || wehicle.carpool == '我要找货'}}"> ({{wehicle.pass}})</view> </view> <view class="car">车型:{{wehicle.car}}</view> <view class="content">备注:{{wehicle.content}}</view> <view class="end-time">发布时间:{{util.timeFormat(wehicle.create_time)}}</view> </view> js代码: // 变红点击确定 async confirmRedden(e) { const that = this; const wehicles = that.data.wehicles; const redindex = that.data.redindex; const wehicle = wehicles[redindex]; const id = wehicle._id; const ReddenStartTime = new Date().getTime(); const ReddenEndTime = that.data.codered; wx.showLoading({ title:"正在变红中..." }) await db.collection("wehicle").doc(id).update({ data:{ redden:{ ReddenStartTime:ReddenStartTime, ReddenEndTime:ReddenEndTime } } }).then(res => { if(!wehicle.redden){ wehicle.redden = (ReddenEndTime) }else{ wehicle.redden.push(ReddenEndTime) } wehicle.isreddened = true; wehicles[redindex] = wehicle; if(wehicle.redden){ wx.hideLoading; wx.showToast({ title:'恭喜!变红成功!' }) setTimeout(function(){ that.setData({ show_red: false, color:'red', // 改变文字颜色为红色 wehicles:wehicles, }) },800) }else{ wx.showToast({ title:"变红失败!请重新变红!" }) that.setData({ color:'', }) } }) },
2024-10-18 - 云函数定时器,出发显示调用成功,返回的是null?
在学习中,想实现一个置顶功能,用云函数定时器,到时间删除字段实现取消置顶功能,调用成功,返回的结果是null,是什么问题? [图片] [图片] 云函数代码: 定时器: { "permissions": { "openapi": [ ] }, "triggers":[ { "name":"mytimer", "type":"timer", "config":"*/30 * * * * * *" } ] } 执行代码: // 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() const _ = db.command // 云函数入口函数 exports.main = async (event, context) => { exports.schedule = { run:async() => { const currentTime = new Date().getTime(); return await db.collection("wehicle").where({timelength:topping.timelength}).watchChanges({ onChange:snapshot => { snapshot.docs.forEach(doc => { if(doc.topping.timelength < currentTime){ await db.collection("wehicle").doc(doc._id).update({ data:{ topping:_.remove() } }) } }) } }) }, } }
2024-10-15 - 定时取消置顶,报错:排序字符不合法?
如图:设定到期时间,到时间让topping字段为空或者删除,取消置顶的功能,在定时项目中,报错:排序字符不合法。求大神指教: [图片] [图片] 插入timelength字段代码: async confirm(e) { const that = this; const code = that.data.code; const timelength = new Date() + code*1000*60*60; const index = that.data.index; const wehicle = that.data.wehicles[index]; const wehicles = that.data.wehicles; wx.showLoading({ title: "正在置顶中..." }) wx.cloud.callFunction({ name: "topping", data: { id: wehicle._id, timelength: timelength, }, success: res => { console.log(res); if(!wehicle.topping){ wehicle.topping = (timelength) }else{ wehicle.topping.push(timelength) } wehicle.istoppinged = true; wehicles[index] = wehicle; if (wehicle.topping) { wx.hideLoading(); wx.showToast({ title: "恭喜!置顶成功!", }) setTimeout(function () { that.setData({ show_input: false, wehicles:wehicles }) }, 800) } else { wx.showToast({ title: "置顶失败,请重新置顶!", }) } } }) }, 定时器代码timer: { "permissions": { "openapi": [ ] }, "triggers":[ { "name":"mytimer", "type":"timer", "config":"*/10 * * * * * *" } ] } 需要执行代码: // 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() const _ = db.command // 云函数入口函数 exports.main = async (event, context) => { const currentTime = new Date().getTime() // 当前时间的时间戳(毫秒) // 查询所有到期(即当前时间已超过timelength)的文档并更新他们,删除topping字段 return await db.collection("wehicle").orderBy('topping.timelength','lt').watchChanges({ onChange:snapshot => { snapshot.docs.forEach(doc => { if(doc.topping.timelength < currentTime){//如果当前时间超过了timelength,则更新文档,删除topping字段 db.collection("wehicle").doc(doc._id).update({data:{topping:[]}})//清空topping字段或设置为空数组(根据你的需求决定) } }) } }) }
2024-10-12 - 如何到时间自动取消置顶?
实现效果:当置顶后,需要按着置顶的时长,自动取消置顶。 如图:当置顶时长到了之后自动删除topping字段,以达到自动取消置顶的效果,自己写了一段代码,能够实现自动删除这个字段,但是当所有数据中都没有topping这个字段后就会报错。哪位大师提供一个更好的实现案例学习下 [图片] [图片] 代码: // 取消置顶 async timer(){ await app.loadUserInfo(); let openid = await app.globalData.userInfo._openid; const toppings = await db.collection("wehicle").where({ _openid: openid }).get() if(toppings){ const wehicles = toppings.data; console.log(wehicles); wehicles.forEach((wehicle, index) => { const id = wehicle._id; console.log(id); if(wehicle.topping.timelength){ setTimeout(function(){ db.collection("wehicle").doc(id).update({ data:{ "topping":_.remove() } }) },wehicle.topping.timelength*1000*60) } }) } },
2024-10-08 - 更新数据库字段后,更新页面显示问题?
如图:想要实现的效果,当点击了弹窗的确定按钮后更新数据库字段,在页面及时显示“顶”这个字,在代码中设置0:const wehicle = that.data.wehicles[0],就能够实现效果,如果设置为id,const wehicle = that.data.wehicles[id],就实现不了。这个id,是点击置顶按钮时的id值,这是什么原因呢? [图片] /** * 置顶弹窗 */ showCode(event) { console.log(event); const id = event.currentTarget.dataset.id console.log(id) this.setData({ show_input: true, id: id }) }, // 获取input输入框 getCodeValue(e) { this.setData({ code: e.detail.value, }) }, // 关闭弹窗 closeModal(e) { console.log(e); this.setData({ show_input: false }) }, // 点击确定 async confirm(e) { const that = this; const itmelength = that.data.code; const id = that.data.id; // 点击弹窗按钮的id, const wehicle = that.data.wehicles[0]; // 传入0能够实现效果,当传入id时,就不能实现。 console.log(wehicle) wx.showLoading({ title: "正在置顶中..." }) wx.cloud.callFunction({ name: "topping", data: { id: id, itmelength: itmelength, }, success: res => { console.log(res); const updated = res.result.stats.updated; if(!wehicle.topping){ wehicle.topping = [itmelength] }else{ wehicle.topping.push(itmelength) } const wehicles = that.data.wehicles; console.log(wehicles) wehicles[id] = wehicle console.log(wehicles[id]); that.setData({ wehicles:wehicles }) if (updated) { wx.hideLoading(); wx.showToast({ title: "恭喜!置顶成功!", }) setTimeout(function () { that.setData({ show_input: false, }) }, 800) } else { wx.showToast({ title: "置顶失败,请重新置顶!", }) } } }) },
2024-09-26 - 更新数据库字段,怎么更新不了?
如图:当点击确定后,更新数据库,在数据库增加一个字段,在JS中能够成功添加,当在云函数中实现时,添加不了 [图片] 在这段代码中,能够成功添加: confirm(e){ const that = this; console.log(e); const itmelength = that.data.code; const id = that.data.id; const wehicles = that.data.wehicles; db.collection("wehicle").doc(id).update({ data:{ top:{ itmelength:itmelength, pud_data:new Date(), } } }) .then(res => { console.log(res); }) }, 在云函数中添加不了: // 云函数入口函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext(); const openid = wxContext.OPENID; const itmelength = event.itmelength; const id = event.id; console.log(id); return await db.collection("wehicle").doc(id).update({ data:{ "top":_.push({ itmelength:itmelength, pud_data:new Date(), }) } }) } js代码: confirm(e){ const that = this; console.log(e); const itmelength = that.data.code; const id = that.data.id; const wehicles = that.data.wehicles; wx.cloud.callFunction({ name:'topping', data:{ top:{ itmelength:itmelength, } }, success:res => { console.log(res); } }) }
2024-09-23 - url的type怎么传?
如图中点击跳转到详情页,url中的type值怎么传? [图片][图片] <segment-carpool items="{{items}}" result="{{result}}" resultthree="{{result3}}" resultfous="{{result4}}" binditemchengde="onItemChengdeEvent" defaultIndex="0"> <view slot="0" class="segment-page wehicle-page"> </view> <view slot="1" class="segment-page people-page"> <view wx:if="{{isQuerying == true}}"> <view class="comment-title">全部信息(共有{{result1}}条)</view> <sumlistone wx:for="{{listones}}" wx:key="key" wx:for-item="listone" list1="{{listone}}"></sumlistone> </view> <view wx:else> <view class="comment-title">全部信息(共有{{result1}}条)</view> <wehicle wx:for="{{listones}}" wx:key="key" wx:for-item="listone" list1="{{listone}}" items="items.item" detailurl="../detail/detail?type={{itemchengde.items}}&index={{index}}"></wehicle> </view> </view> <view slot="2" class="segment-page train-page"> </view> <view slot="3" class="segment-page money-page"> </view> <view slot="4" class="segment-page money-page"> </view> </segment-carpool> <loadingmore hasmore="{{hasmore}}"></loadingmore> JS代码 data: { items: ["全部", "我要找车", "我要找人", "我找货车", "我要找货"], }, onItemChengdeEvent(event) { console.log(event) const that = this; var index = event.detail.index; that.setData({ index:index }) },
2024-09-08 - 当点击查询清空input输入框,上拉加载更多失效?
如图,当点击查询后清空输入框的值,但是上拉加载更多时失效,获取不到数据,这个要怎么做? [图片] 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> JS代码: data: { hasmore: true, sumlistress:[], startPoint:'', goal: '', isQuerying: false, }, /** * 获取起点输入信息 */ 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 }) }, /** * 按条件查询数据 */ async formSubmit(start = 0) { const that = this; that.setData({ isQuerying: true, }) let promite = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, }) if(start>0){ promite = promite.skip(start) } let pro = await promite.limit(2).orderBy('create_time','desc').get() console.log(pro) if(pro){ const sumlistress = pro.data; console.log(sumlistress); promite.count().then(res => { console.log(res) that.setData({ result:res.total }) }) let hasmore = false let newsumlistress = []; if (start > 0) { newsumlistress = that.data.sumlistress.concat(sumlistress); } else { newsumlistress = sumlistress; } newsumlistress.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ sumlistress: newsumlistress, hasmore: hasmore, startPoint:'', goal: '' }) } }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { let hasmore = true; if(this.data.wehicles.length == 0 && this.data.sumlistress.length == 0){ hasmore = false } this.setData({ hasmore:hasmore }) if(!this.data.isQuerying) { this.lowdrelease(this.data.wehicles.length); }else{ this.formSubmit(this.data.sumlistress.length); } },
2024-09-02