微信小程序云開(kāi)發(fā)實(shí)現(xiàn)數(shù)據(jù)添加、查詢和分頁(yè)
本文實(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)文章
JavaScript實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05JavaScript cookie 跨域訪問(wèn)之廣告推廣
這篇文章主要介紹了JavaScript cookie 跨域訪問(wèn)之廣告推廣 的相關(guān)資料,需要的朋友可以參考下2016-04-04JavaScript中的Array 對(duì)象(數(shù)組對(duì)象)
Array 對(duì)象用于在單個(gè)的變量中存儲(chǔ)多個(gè)值。2016-06-06Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果
本文主要分享Canvas實(shí)現(xiàn)動(dòng)態(tài)的雪花效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02在網(wǎng)頁(yè)中使用document.write時(shí)遭遇的奇怪問(wèn)題
很多時(shí)候我們都會(huì)在網(wǎng)頁(yè)上的JavaScript中使用document.write來(lái)寫(xiě)入一些東西,有的時(shí)候可能因?yàn)槲覀儫o(wú)法改變某一部分HTML而不得不使用這樣的辦法,也有的時(shí)候是因?yàn)樵谶M(jìn)行跨應(yīng)用的腳本調(diào)用。2010-08-08JS輕松實(shí)現(xiàn)CSS設(shè)置,DIV+CSS常用CSS設(shè)置
JS輕松實(shí)現(xiàn)CSS設(shè)置,DIV+CSS常用CSS設(shè)置...2007-02-02JS簡(jiǎn)單編號(hào)生成器實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了JS簡(jiǎn)單編號(hào)生成器實(shí)現(xiàn)方法,涉及JavaScript針對(duì)表單與字符串操作的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04Javascript中的方法鏈(Method Chaining)介紹
這篇文章主要介紹了Javascript中的方法鏈(Method Chaining)介紹,本文講解了Javascript Method Chaining、Method Chaining 使用、Method Chaining VS prototype Chaining等內(nèi)容,需要的朋友可以參考下2015-03-03