8000 More information about list render (#669) · sdras/vuejs.org@3732474 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3732474

Browse files
defccchrisvfritz
authored andcommitted
More information about list render (vuejs#669)
* keyCodes now support array type * remove trailing whitespace * add more info about v-for with v-if * update code example * fix typo * fix typo * Update links to list rendering guide * Update copy about v-if with -vofr on conditional guide
1 parent 4316087 commit 3732474

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

src/v2/api/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,8 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
13281328
Conditionally render the element based on the truthy-ness of the expression value. The element and its contained directives / components are destroyed and re-constructed during toggles. If the element is a `<template>` element, its content will be extracted as the conditional block.
13291329

13301330
This directive triggers transitions when its condition changes.
1331+
1332+
<p class="tip">When used together with v-if, v-for has a higher priority than v-if. Check the <a href="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.</p>
13311333

13321334
- **See also:** [Conditional Rendering - v-if](../guide/conditional.html)
13331335

@@ -1411,7 +1413,9 @@ All lifecycle hooks automatically have their `this` context bound to the instanc
14111413
{{ item.text }}
14121414
</div>
14131415
```
1414-
1416+
1417+
<p class="tip">When used together with v-if, v-for has a higher priority than v-if. Check the <a href="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.</p>
1418+
14151419
The detailed usage for `v-for` is explained in the guide section linked below.
14161420

14171421
- **See also:**

src/v2/guide/conditional.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,7 @@ The difference is that an element with `v-show` will always be rendered and rema
193193
In comparison, `v-show` is much simpler - the element is always rendered regardless of initial condition, with just simple CSS-based toggling.
194194

195195
Generally speaking, `v-if` has higher toggle costs while `v-show` has higher initial render costs. So prefer `v-show` if you need to toggle something very often, and prefer `v-if` if the condition is unlikely to change at runtime.
196+
197+
## `v-if` with `v-for`
198+
199+
When used together with `v-for`, `v-for` has a higher priority than `v-if`. See the <a href="../guide/list.html#V-for-and-v-if">list rendering guide</a> for details.

src/v2/guide/list.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,90 @@ new Vue({
329329
</script>
330330
{% endraw %}
331331

332-
## key
332+
### V-for and v-if
333+
334+
In Vue.js template, v-for has a higher priority than v-if when they exists in the same node.
335+
336+
Suppose the example code bellow:
337+
338+
```html
339+
<div id="v-for-if" class="demo">
340+
<div v-for="item in list" v-if="item"><div>{{ item }}</div></div>
341+
<div v-for="item2 in list2" v-else><div>{{ item2 }}</div></div>
342+
</div>
343+
```
344+
345+
```js
346+
new Vue({
347+
el: '#v-for-if',
348+
data: {
349+
list: [0, false, 1],
350+
list2: [4, 5, 6]
351+
}
352+
})
353+
```
354+
355+
356+
Because v-for has higher priority than v-if, Vue.js will loop item in the list first. If item is true, it will output `item` value, else it will loop list2 and output the items of it.
357+
358+
So the result of the above example code will be:
359+
360+
{% raw %}
361+
<div id="v-for-if" class="demo">
362+
<div v-for="item in list" v-if="item"><div>{{ item }}</div></div>
363+
<div v-for="item2 in list2" v-else><div>{{ item2 }}</div></div>
364+
</div>
365+
<script>
366+
new Vue({
367+
el: '#v-for-if',
368+
data: {
369+
list: [0, false, 1],
370+
list2: [4, 5, 6]
371+
}
372+
})
373+
</script>
374+
{% endraw %}
375+
376+
Let's see a complicated example. This example shows the case that v-for, v-if, v-else-if and v-else used together.
377+
378+
```html
379+
<div id="v-for-if-complicated" class="demo">
380+
<div v-for="item in list" v-if="item"><div>{{ item }}</div></div>
381+
<div v-for="item2 in list2" v-else-if="item === false"><div>{{ item2 }}</div></div>
382+
<div v-else><div>else</div></div>
383+
</div>
384+
```
385+
386+
```js
387+
new Vue({
388+
el: '#v-for-if-complicated',
389+
data: {
390+
list: [0, false, 1],
391+
list2: [4, 5, 6]
392+
}
393+
})
394+
```
395+
396+
Result:
397+
398+
{% raw %}
399+
<div id="v-for-if-complicated" class="demo">
400+
<div v-for="item in list" v-if="item"><div>{{ item }}</div></div>
401+
<div v-for="item2 in list2" v-else-if="item === false"><div>{{ item2 }}</div></div>
402+
<div v-else><div>else</div></div>
403+
</div>
404+
<script>
405+
new Vue({
406+
el: '#v-for-if-complicated',
407+
data: {
408+
list: [0, false, 1],
409+
list2: [4, 5, 6]
410+
}
411+
})
412+
</script>
413+
{% endraw %}
414+
415+
## Key
333416

334417
When Vue.js is updating a list of elements rendered with `v-for`, it by default uses an "in-place patch" strategy. If the order of the data items has changed, instead of moving the DOM elements to match the order of the items, Vue will simply patch each element in-place and make sure it reflects what should be rendered at that particular index. This is similar to the behavior of `track-by="$index"` in Vue 1.x.
335418

0 commit comments

Comments
 (0)
0