欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序云開(kāi)發(fā)實(shí)現(xiàn)數(shù)據(jù)添加、查詢和分頁(yè)

 更新時(shí)間:2019年05月17日 14:15:57   作者:_龍衣  
這篇文章主要為大家詳細(xì)介紹了微信小程序云開(kāi)發(fā)實(shí)現(xiàn)數(shù)據(jù)添加、查詢和分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了微信小程序云開(kāi)發(fā)實(shí)現(xiàn)數(shù)據(jù)添加、查詢和分頁(yè),供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)的效果

實(shí)現(xiàn)要點(diǎn)

WXML 不同類別數(shù)據(jù)的顯示

通過(guò) if-elif-else 實(shí)現(xiàn),在wxml文件中通過(guò) <block></block>渲染,因?yàn)樗鼉H僅是一個(gè)包裝元素,不會(huì)在頁(yè)面中做任何渲染,只接受控制屬性。也就是說(shuō)可以通過(guò)屬性來(lái)控制頁(yè)面是否要渲染這部分的內(nèi)容,可以減少頁(yè)面渲染時(shí)間。
云開(kāi)發(fā)數(shù)據(jù)的獲取

先開(kāi)通云開(kāi)發(fā)功能 ,參考官方文檔,然后在創(chuàng)建項(xiàng)目的時(shí)候勾選上 使用云開(kāi)發(fā)模板(看個(gè)人吧,我直接使用后點(diǎn)擊項(xiàng)目中的 login)就可以獲取到用戶的oppenid,之后就可以使用云數(shù)據(jù)庫(kù)了。

云開(kāi)發(fā)登錄:

云數(shù)據(jù)的獲取

 /**
 * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
 */
 onLoad: function(options) {
 console.log('onload');
 this.getData(this.data.page); 
 },
 /**
 * 獲取列表數(shù)據(jù)
 * 
 */
 getData: function(page) {
 var that = this;
 console.log("page--->" + page);
 const db = wx.cloud.database();
 // 獲取總數(shù)
 db.collection('topic').count({
  success: function(res) {
  that.data.totalCount = res.total;
  }
 })
 // 獲取前十條
 try {
  db.collection('topic')
  .where({
   _openid: 'oSly***********vU1KwZE', // 填入當(dāng)前用戶 openid
  })
  .limit(that.data.pageSize) // 限制返回?cái)?shù)量為 10 條
  .orderBy('date', 'desc')
  .get({
   success: function(res) {
   // res.data 是包含以上定義的兩條記錄的數(shù)組
   // console.log(res.data)
   that.data.topics = res.data;
   that.setData({
    topics: that.data.topics,
   })
   wx.hideNavigationBarLoading();//隱藏加載
   wx.stopPullDownRefresh();
   
   },
   fail: function(event) {
   wx.hideNavigationBarLoading();//隱藏加載
   wx.stopPullDownRefresh();
   }
  })
 } catch (e) {
  wx.hideNavigationBarLoading();//隱藏加載
  wx.stopPullDownRefresh();
  console.error(e);
 }
 },

云數(shù)據(jù)的添加

 /**
 * 保存到發(fā)布集合中
 */
 saveDataToServer: function(event) {
 var that = this;
 const db = wx.cloud.database();
 const topic = db.collection('topic')
 db.collection('topic').add({
  // data 字段表示需新增的 JSON 數(shù)據(jù)
  data: {
  content: that.data.content,
  date: new Date(),
  images: that.data.images,
  user: that.data.user,
  isLike: that.data.isLike,
  },
  success: function(res) {
  // res 是一個(gè)對(duì)象,其中有 _id 字段標(biāo)記剛創(chuàng)建的記錄的 id
  // 清空,然后重定向到首頁(yè)
  console.log("success---->" + res)
  // 保存到發(fā)布?xì)v史
  that.saveToHistoryServer();
  // 清空數(shù)據(jù)
  that.data.content = "";
  that.data.images = [];

  that.setData({
   textContent: '',
   images: [],
  })

  that.showTipAndSwitchTab();

  },
  complete: function(res) {
  console.log("complete---->" + res)
  }
 })
 },

云數(shù)據(jù)的刪除

可查看官放文檔,這里不貼代碼了,有問(wèn)題聯(lián)系。

云數(shù)據(jù)的更新

可查看官放文檔,這里不貼代碼了,有問(wèn)題聯(lián)系。

數(shù)據(jù)列表的分頁(yè)

主要就是定義一個(gè)臨時(shí)數(shù)組存放加載上來(lái)的數(shù)據(jù),然后通過(guò)傳遞給對(duì)象,最后傳遞到布局中去。 

/**
 * 頁(yè)面上拉觸底事件的處理函數(shù)
 */
 onReachBottom: function() {
 var that = this;
 var temp = [];
 // 獲取后面十條
 if(this.data.topics.length < this.data.totalCount){
 try {
 const db = wx.cloud.database();
 db.collection('topic')
  .skip(5)
  .limit(that.data.pageSize) // 限制返回?cái)?shù)量為 5 條
  .orderBy('date', 'desc') // 排序
  .get({
  success: function (res) {
  // res.data 是包含以上定義的兩條記錄的數(shù)組
  if (res.data.length > 0) {
  for(var i=0; i < res.data.length; i++){
   var tempTopic = res.data[i];
   console.log(tempTopic);
   temp.push(tempTopic);
  }

  var totalTopic = {};
  totalTopic = that.data.topics.concat(temp);

  console.log(totalTopic);
  that.setData({
   topics: totalTopic,
  })
  } else {
  wx.showToast({
   title: '沒(méi)有更多數(shù)據(jù)了',
  })
  }


  },
  fail: function (event) {
  console.log("======" + event);
  }
  })
 } catch (e) {
 console.error(e);
 }
 }else{
 wx.showToast({
 title: '沒(méi)有更多數(shù)據(jù)了',
 })
 }
 
 },

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論