WeChat Mini Program Use of Loops and Nested Loops

不言
Release: 2018-06-22 16:39:37
Original
2776 people have browsed it

This article mainly introduces relevant information on the use summary of WeChat mini program loops and nested loops. I hope this article can help everyone. Friends in need can refer to

WeChat mini program Summary of the use of loops and nested loops

Regarding the WeChat applet, I was recently assigned to make a WeChat applet. This is my first contact with it. Generally speaking, it is not too difficult to get started.

I have a lot of feelings about the loop problem of small programs, because I have used loops and nested loops countless times to bind data to the interface.

For us in js, we obtain the data from the interface through POST or GET request and store it in the object defined in Page:

//首页话题列表 
   wx.request({ 
    url: 'https://*******************', 
    method: 'POST', 
    data: { 
     pageNum: 1, 
     pageSize: 10 
    }, 
    success:function(res){ 
     that.setData({ 
      listTop:res.data, 
       
     }) 
    } 
   })
Copy after login

In the wxml file,

 wx:for="{{listTop}}"
Copy after login

is used to loop through the data in the output object. Here we can get the subscript through {{index }} , you can also customize the subscript:

wx:for-index="index2"
Copy after login

When the object com exists in listTop, we can pass wx:for="{{item.com} }" to loop through the data in the loop.

In an actual project, I encountered such a problem: During the nested loop process, I need to convert the value of a certain field, such as the timestamp into date/a few days ago, etc. At this time, we should

Know that the WeChat applet does not support the interface to directly call JS. How should we solve it at this time:

At first, I spared a lot of trouble. I always wanted to use JS in JS. By looping it into an object, and then looping it out on the interface, I was actually close to the result in the previous step, but in actual development, I still have many shortcomings as a novice who has only been employed for less than a month, so You are trapped in an endless loop.

Solution

: When you traverse, just replace the original data with the desired data. . . . (It’s very simple, but the authorities are confused, but since I solved it myself, there may be a better way, so this is just a description)

for (var i = 0; i < res.data.data.length;i++){ 
    console.log(res.data.data[i].comments+"**********"+i)  
    
    console.log("***"+i) 
    if (res.data.data[i].comments !=null){ 
     for (var j = 0; j < res.data.data[i].comments.length;j++){ 
      res.data.data[i].comments[j].createTime=transDate(res.data.data[i].comments[j].createTime) 
     } 
    } 
     }
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About WeChat Mini Program Introduction to the life cycle

About the development of WeChat mini program canvas


##

The above is the detailed content of WeChat Mini Program Use of Loops and Nested Loops. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!