Detailed explanation and examples of data interaction and rendering of WeChat applet

高洛峰
Release: 2017-03-28 13:45:53
Original
2572 people have browsed it

This article mainly introduces the relevant information on the detailed explanation of data interaction and rendering examples of WeChat applet. Friends in need can refer to it

WeChat applet data interaction and rendering

Implementation renderings:

Detailed explanation and examples of data interaction and rendering of WeChat applet

The API of the WeChat applet provides an API for network interaction. We only need to call it to communicate with the backend. For data interaction, the API is wx.request. The specific code is as follows.

//list.js 
//获取应用实例 
var app = getApp() 
Page({ 
 data: { 
  list:[], 
  hiddenLoading: true, 
  url: '' 
 }, 
 loadList: function () { 
  var that = this; 
  that.setData({ 
   hiddenLoading: !that.data.hiddenLoading 
  }) 
  var url = app.urls.CloudData.getList; 
  that.setData({ 
   url: url 
  }); 
  wx.request({ 
   url: url, 
   data: {}, 
   method: 'GET', 
   success: function (res) { 
    var list= res.data.list; 
    if (list == null) { 
     list = []; 
    } 
    that.setData({ 
     list: list, 
     hiddenLoading: !that.data.hiddenLoading 
    }); 
     wx.showToast({ 
     title: "获取数据成功", 
     icon: 'success', 
     duration: 2000 
    }) 
   }, 
   fail: function (e) { 
    var toastText='获取数据失败' + JSON.stringify(e); 
    that.setData({ 
     hiddenLoading: !that.data.hiddenLoading 
    }); 
    wx.showToast({ 
     title: toastText, 
     icon: '', 
     duration: 2000 
    }) 
   }, 
   complete: function () { 
    // complete 
   } 
  }), 
 //事件处理函数 
 bindViewTap: function () { 
  wx.navigateTo({ 
   url: '../logs/logs' 
  }) 
 }, 
 onLoad: function () { 
 
 }, 
 onReady: function () { 
  this.loadList(); 
 }, 
 onPullDownRefresh: function () { 
  this.loadList(); 
  wx.stopPullDownRefresh() 
 } 
})
Copy after login

A network request is made in the loadList function, and the requested data is placed in the data list. We use setData to modify the list. After this function is called, the WeChat applet framework will determine the change in data status, then perform a diff judgment, and render it to the interface if there is any change. This is similar to the rendering method of react.js. It mainly maintains an object similar to a virtual document internally, and then renders the interface by judging the virtual document, which can greatly improve performance.

Here we also made a pull-down refresh trigger, that is, the onPullDownRefresh function. In order to be able to use pull-down refresh, we need to configure it. Now we only need the current page to take effect, so just configure it in the json of the corresponding page. Yes, it can be configured in list.json.

list.json

{ 
  "navigationBarTitleText": "产品列表",   
  "enablePullDownRefresh":true 
}
Copy after login

If you need all pages to take effect, you can configure it in the window in app.json.

app.json

{ 
 "pages":[ 
  "pages/index/index", 
  "pages/logs/logs", 
  "pages/list/list" 
 ], 
 "window":{ 
  "backgroundTextStyle":"light", 
  "navigationBarBackgroundColor": "#fff", 
  "navigationBarTitleText": "WeChat", 
  "navigationBarTextStyle":"black", 
  "enablePullDownRefresh":true 
 } 
}
Copy after login

In app.json, there is also a pages. The pages we need to route need to be registered here, otherwise they cannot be routed.

When requesting data, prompts for waiting and obtaining success and failure have been added. This requires the cooperation of the corresponding page. The page code list.wxm is as follows

<!--list.wxml--> 
<view> 
 <!--默认隐藏--> 
 <loading>正在加载</loading> 
 <scroll-view> 
  <view> 
   <block> 
    <view> 
     <view> 
      <text>{{item.no}}({{item.content}})</text> 
     </view> 
    </view> 
   </block> 
  </view> 
 </scroll-view> 
</view>
Copy after login
/**list.wxss**/ 
 
.widget { 
 position: relative; 
 margin-top: 5rpx; 
 margin-bottom: 5rpx; 
 padding-top: 10rpx; 
 padding-bottom: 10rpx; 
 padding-left: 40rpx; 
 padding-right: 40rpx; 
 border: #ddd 1px solid; 
}
Copy after login
/**app.wxss**/ 
.container { 
 height: 100%; 
 display: flex; 
 flex-direction: column; 
 align-items: center; 
 justify-content: space-between; 
 box-sizing: border-box; 
 padding-top: 10rpx; 
 padding-bottom: 10rpx; 
}
Copy after login

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

The above is the detailed content of Detailed explanation and examples of data interaction and rendering of WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!