Exploration of vue v-for Loop Nesting (2)

The purpose of using v-for loops is to process a large number of types of duplicate data. Ultimately, it is a regular data, but some rules are not so easy. Many times, we use loops, or even multiple loops nesting. Different loops nesting corresponds to different json data structures. This article mainly describes the use of v-for loops to solve the partSimilarly, in some different cases, it is mainly used for table tables.

The simulated json data is as follows:

	  	"term":"2017 First semester of the year",
	  	"term":"2017 Second semester of the year",

Requirements: Show this report in a form!

Thought: Analyzing the structure, we can find that the results for Xiao Wang are the results of different semesters, the same part is the subject, the different part is the semester

If you write them individually, you will need many loops. How to solve the problem with as few loops as possible?

html structure:

 <p><span>{{name}}</span>Achievement Sheet</p>
		    <table class="tablegrade">
		    	<tr v-for="(i,index) in grades">
		    		<td v-for="value in i.project">

Here we can clearly see that the display of tabular data is achieved by using two layers of loops.

The results are as follows:

Data binding is as follows:

First, introduce the location of the json data you need.

Then get these values when you create it;

By nesting the data in a loop, this is actually a very simple solution. Smart people can come up with better ideas just by knowing how the for loop works.

import data from '../../common/data.json';  //1. Introduce the json data to be used
	export default{
		name: 'Order',
				grades:[],   //2. Define an array that can hold data
		created() {         //3. Acquisition of json data content
			this.name = data.name;
			this.age = data.age;
			this.phone = data.phone;
			this.sex = data.sex;
	    	this.grades = data.grades;		//4. json data and data transfer and preservation	

To summarize how to use the for loop:

1. Display all values through a loop and use the for loop directly

2. Display by cyclic satisfying conditions, not satisfying not showing, use for cyclic + if (meeting the conditions to be displayed) [partial display]

3. Show one content when satisfied by loop, show another when not satisfied, use for loop + if (satisfy display condition) + else (show when not satisfied) [categorize display]

Source View Location: https://download.csdn.net/download/colourfultiger/10585045

Keywords: JSON

Added by platinum on Tue, 07 Jan 2020 19:43:40 +0200