您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)微信小程序云開發(fā)實現(xiàn)數(shù)據(jù)添加、查詢和分頁,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
WXML 不同類別數(shù)據(jù)的顯示
通過 if-elif-else 實現(xiàn),在wxml文件中通過 <block></block>渲染,因為它僅僅是一個包裝元素,不會在頁面中做任何渲染,只接受控制屬性。也就是說可以通過屬性來控制頁面是否要渲染這部分的內(nèi)容,可以減少頁面渲染時間。
云開發(fā)數(shù)據(jù)的獲取
先開通云開發(fā)功能 ,參考官方文檔,然后在創(chuàng)建項目的時候勾選上 使用云開發(fā)模板(看個人吧,我直接使用后點擊項目中的 login)就可以獲取到用戶的oppenid,之后就可以使用云數(shù)據(jù)庫了。
云開發(fā)登錄:
云數(shù)據(jù)的獲取
/** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ 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', // 填入當前用戶 openid }) .limit(that.data.pageSize) // 限制返回數(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 是一個對象,其中有 _id 字段標記剛創(chuàng)建的記錄的 id // 清空,然后重定向到首頁 console.log("success---->" + res) // 保存到發(fā)布歷史 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ù)的刪除
可查看官放文檔,這里不貼代碼了,有問題聯(lián)系。
云數(shù)據(jù)的更新
可查看官放文檔,這里不貼代碼了,有問題聯(lián)系。
數(shù)據(jù)列表的分頁
主要就是定義一個臨時數(shù)組存放加載上來的數(shù)據(jù),然后通過傳遞給對象,最后傳遞到布局中去。
/** * 頁面上拉觸底事件的處理函數(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) // 限制返回數(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: '沒有更多數(shù)據(jù)了', }) } }, fail: function (event) { console.log("======" + event); } }) } catch (e) { console.error(e); } }else{ wx.showToast({ title: '沒有更多數(shù)據(jù)了', }) } },
以上就是微信小程序云開發(fā)實現(xiàn)數(shù)據(jù)添加、查詢和分頁,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。