search
HomeWeChat AppletMini Program DevelopmentWeChat Mini Program Use of Loops and Nested Loops

WeChat Mini Program Use of Loops and Nested Loops

Jun 22, 2018 pm 04:39 PM
cycleWeChat applet

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, 
       
     }) 
    } 
   })

In the wxml file,

 wx:for="{{listTop}}"

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"

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) 
     } 
    } 
     }

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!