Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development List Rendering and Conditional Rendering


May 22, 2021 Mini Program Cloud Development Study Guide


Table of contents


In the section on data binding, we learned how to render data for array types and object types, but at that time we were just outputing data from an array or an array of objects, what if we were to output the entire list? This time you need to use list rendering.

Render all the data in the array

The same structure is a prerequisite for list rendering

In the actual development scenario, goods, news, stocks, collections, bookshelf lists, etc. will have thousands of pieces of data, they all have a common feature is the same structure of the data, which is the premise that we can bulk rendering. Or take the data from the previous Internet Newsletter as an example, the following headlines have a very single structure.

  1. data: {
  2. newstitle:[
  3. "瑞幸咖啡:有望在三季度达到门店运营的盈亏平衡点",
  4. "腾讯:广告高库存量还是会持续到下一年",
  5. "上汽集团云计算数据中心落户郑州,总投资20亿元",
  6. "京东:月收入超2万元快递小哥数量同比增长163%",
  7. "腾讯:《和平精英》日活跃用户已超五千万",
  8. ],
  9. }

So how do we render the whole list? There's a lot of knowledge about JavaScript array traversal, so there are a lot of ways to render a small program array, so it's a bit confusing to look at the technical documentation.

Technical documentation: List index

  1. <view wx:for="{{newstitle}}" wx:key="*this">
  2. {{item}}
  3. </view>

Here wx:for "" You can also use the following method:

  1. <view wx:for-items="{{newstitle}}" wx:for-item="title" wx:key="*this">
  2. {{title}}
  3. </view>

The underlying variable name of the current item of the default array defaults to index, the variable name of the current item of the array defaults to item;

Movie list page

First of all, we write data from multiple movies in data, which is equivalent to data of an array type, containing data of multiple object types.

  1. movies: [{
  2. name: "肖申克的救赎",
  3. img:"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.webp",
  4. desc:"有的人的羽翼是如此光辉,即使世界上最黑暗的牢狱,也无法长久地将他围困!"},
  5. {
  6. name: "霸王别姬",
  7. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1910813120.webp",
  8. desc: "风华绝代。"
  9. },
  10. {
  11. name: "这个杀手不太冷",
  12. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p511118051.webp",
  13. desc: "怪蜀黍和小萝莉不得不说的故事。"
  14. },
  15. {
  16. name: "阿甘正传",
  17. img: "https://img1.doubanio.com/view/photo/s_ratio_poster/public/p510876377.webp",
  18. desc: "一部美国近现代史。"
  19. },
  20. {
  21. name: "美丽人生",
  22. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p510861873.webp",
  23. desc: "最美的谎言。"
  24. },
  25. {
  26. name: "泰坦尼克号",
  27. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p457760035.webp",
  28. desc: "失去的才是永恒的。"
  29. },
  30. {
  31. name: "千与千寻",
  32. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1606727862.webp",
  33. desc: "最好的宫崎骏,最好的久石让。"
  34. },
  35. {
  36. name: "辛德勒名单",
  37. img: "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p492406163.webp",
  38. desc: "拯救一个人,就是拯救整个世界。"
  39. },
  40. ],

Then we'll change the code for the data binding section and add a wx:for statement to render the data in the list to the loop.

  1. <view class="page__bd">
  2. <view class="weui-panel weui-panel_access">
  3. <view class="weui-panel__bd" wx:for="{{movies}}" wx:for-item="movies" wx:key="*item">
  4. <navigator url="" class="weui-media-box weui-media-box_appmsg" hover-class="weui-cell_active">
  5. <view class="weui-media-box__hd weui-media-box__hd_in-appmsg">
  6. <image class="weui-media-box__thumb" mode="widthFix" src="{{movies.img}}" sytle="height:auto"></image>
  7. </view>
  8. <view class="weui-media-box__bd weui-media-box__bd_in-appmsg">
  9. <view class="weui-media-box__title">{{movies.name}}</view>
  10. <view class="weui-media-box__desc">{{movies.desc}}</view>
  11. </view>
  12. </navigator>
  13. </view>
  14. </view>
  15. </view>

Here is a wx:for-item, give it a value is dianying.name movies, in fact, can also be other values, such as dianeing, that ,"movies.img", . . . movies.name . W hy is that? This is also a knowledge of JavaScript that you don't have to look into first.

Picture style

We found that the pictures in the movie list are distorted, why? Going back to the image components we learned earlier, let's look at the image component documentation and find out from the technical documentation.

Technical documentation: Image component documentation

In the technical documentation, we found that if we don't write the picture mode, the picture mode defaults to scaleToFill, that is, does not maintain the aspect ratio to zoom the picture, so that the width of the picture is completely stretched to fill the image element.

Then we want the picture to remain the same width, the height changes automatically, keep the original aspect ratio unchanged, then we need to use the widthFix mode.

Let's add widthFix mode to the image component,

  1. <image class="weui-media-box__thumb" mode="widthFix" src="{{movies.img}}"

After adding the pattern, we found that the picture scale appeared normal, but the image component overflowed because weui gave the class a look:60px css style for the weui-media-box__hd_in-appmsg component, which limits the height, which we can add in home.wxss

  1. .weui-media-box__hd_in-appmsg{
  2. height: auto;
  3. }

Cover this height: 60px.

The override principle of css is based on priority, the higher the style priority written after the css file, the higher the previous override;

Clicking on the movie list is not linked, you can review the previous knowledge points, add a link to each movie, add the path to each page in the page configuration item.

Grid nine palace style reference

You often see in the app that some of the categories are laid out in nine palaces. W e use the emulator in the WeUI app to find the grid under the underlying components and look at the style that the grid presents. Then refer to the code in the grid page file grid.wxml under the example folder in the WeUI apple file structure and add the code in home.wxml:

  1. <view class="page__bd">
  2. <view class="weui-grids">
  3. <block wx:for="{{grids}}" wx:for-item="grid" wx:key="*this">
  4. <navigator url="" class="weui-grid" hover-class="weui-grid_active">
  5. <image class="weui-grid__icon" src="{{grid.imgurl}}" />
  6. <view class="weui-grid__label">{{grid.title}}</view>
  7. </navigator>
  8. </block>
  9. </view>
  10. </view>

In weUI's source code, we see a label that says it contains a multi-node structure block, and that replacing it with a multi-node structure doesn't make much difference, as if it's a different component, just as it doesn't make a difference.

Then add .js data to the home

  1. grids:[
  2. { imgurl:"https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon1.png",
  3. title:"招聘"
  4. },
  5. {
  6. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon2.png",
  7. title: "房产"
  8. },
  9. {
  10. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon3.png",
  11. title: "二手车新车"
  12. },
  13. {
  14. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon4.png",
  15. title: "二手"
  16. },
  17. {
  18. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon5.png",
  19. title: "招商加盟"
  20. },
  21. {
  22. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon6.png",
  23. title: "兼职"
  24. },
  25. {
  26. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon7.png",
  27. title: "本地"
  28. },
  29. {
  30. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon8.png",
  31. title: "家政"
  32. },
  33. {
  34. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon9.png",
  35. title: "金币夺宝"
  36. },
  37. {
  38. imgurl: "https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/icon10.png",
  39. title: "送现金"
  40. },
  41. ]

You can see a lot of app interfaces have a nine palace grid. H ere the nine palace grid is a row of three columns, how to make the nine palace grid into a row of five columns? First of all, we need to know why this nine-miyat grid becomes a row of three columns, in weui.wxss to weui-grid defined a width:33.333333333333% style, we can add a style in home.wxs to cover the original width.

  1. .weui-grid{
  2. width: 20%;
  3. }

List style reference

You can first experience the list style below the WeUI applet form in the developer tool simulator, and find the wxml code corresponding to the list style, in the excel/list directory of the developer tool's file directory. We can refer to the code inside and enter the following code in home.wxml in conjunction with the previous example:

  1. <view class="weui-cells weui-cells_after-title">
  2. <block wx:for="{{listicons}}" wx:for-item="listicons">
  3. <navigator url="" class="weui-cell weui-cell_access" hover-class="weui-cell_active">
  4. <view class="weui-cell__hd">
  5. <image src="{{listicons.icon}}" style="margin-right: 5px;vertical-align: middle;width:20px; height: 20px;"></image>
  6. </view>
  7. <view class="weui-cell__bd">{{listicons.title}}</view>
  8. <view class="weui-cell__ft weui-cell__ft_in-access">{{listicons.desc}}</view>
  9. </navigator>
  10. </block>
  11. </view>

Add the .js data to the data in the home data:

  1. listicons:[{
  2. icon:"https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/listicons1.png",
  3. title:"我的文件",
  4. desc:""
  5. },
  6. {
  7. icon:"https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/listicons2.png",
  8. title:"我的收藏",
  9. desc:"收藏列表"
  10. },
  11. {
  12. icon:"https://hackweek.oss-cn-shanghai.aliyuncs.com/hw18/hackwork/weapp/listicons3.png",
  13. title:"我的邮件",
  14. desc:""
  15. }
  16. ],

To see the effect again, the first and third descs here are not written and do not affect the display of this list