微信小程序 刷新当前页面部分内容方法【亲测可用】
发布于 3 年前 作者 lei47 2109 次浏览 来自 分享
//index.js代码
Page({
  //页面的初始数据
  data: {},
  //生命周期函数--监听页面加载
  onLoadfunction (options{},
  //生命周期函数--监听页面初次渲染完成
  onReadyfunction () {
       //此处先调用一次数据
       this.GetYourContent()
    },
  //生命周期函数--监听页面显示
  onShowfunction () {},
    //生命周期函数--监听页面隐藏
  onHidefunction () {},
  //生命周期函数--监听页面卸载
  onUnloadfunction () {},
  //页面相关事件处理函数--监听用户下拉动作
  onPullDownRefreshfunction () {},
  //页面上拉触底事件的处理函数
  onReachBottomfunction () {},
  //用户点击右上角分享
  onShareAppMessagefunction () {},
    

    //自定义获取内容的函数
    GetYourContent: function(){
      //此处可从数据库读取数据,示例代码:
      const db = wx.cloud.database();
      db.collection('stock_calculate').where("stkNum" != '').orderBy('timesTamp''desc').get().then(res => {
      this.setData({stkSearchRecord : res.data});
   })
    },
    //这个是操作后,需要更新数据的函数
    OnDelData: function(e){
      const db = wx.cloud.database();
      var id = e.currentTarget.dataset.id;
      var that = this;
      wx.showModal({
        title'提示',
        content'确认删除?',
        success (res) {
          if (res.confirm) {
            //console.log('用户点击确定')
              db.collection('stock_calculate').where({
                '_id' : id
              }).remove({
                successres => {
                  console.log('删除计算记录成功'),
                  wx.showToast({
                    title'删除成功'
                  }),
                                    //此处为用户删除操作后,需要调用的回调操作,注:因this操作域原因,所以使用了that=this
                  that.onReady()
                },
                failerr => {
                  console.log('删除失败'),
                  wx.showToast({
                    title'删除失败'
                  })
                }
              })
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })
  },
})

/*
//index.wxml循环代码送上
<view class="recentlySearch">
   <block wx:for="{{stkSearchRecord}}" wx:key="key" wx:for-item="item">
          //注意,此处我使用的是bindlongpress长按回馈操作
     <view class="recentlySearchList" data-id="{{item._id}}" data-stkid="{{item.stkNum}}" bindlongpress="delCalRecord" bindtap="fillTheCheckForm">{{item.stkName}}</view>
   </block>
</view>
*/

注:上述代码经过精简,可能存在bug,若有问题需要协助可在下方留言给我即可。

回到顶部