Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  31] [ 5]  / answers: 1 / hits: 16030  / 9 Years ago, fri, november 27, 2015, 12:00:00

I can't seem to get v-show and v-else to work. The documentation says:



The v-else element must following immediately after the v-if or v-show element - otherwise it will not be recognized.



Documentation: http://vuejs.org/guide/conditional.html#v-show


Fiddle: https://jsfiddle.net/p2ycjk26/2/


Html:


<table>
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody>
<tr v-for="test in tests" v-show="tests.length">
<td>{{ test.name }}</td>
</tr>
<tr v-else>
<td>No data available in table</td>
</tr>
</tbody>
</table>

JavaScript:


new Vue({
el: 'table',

data: {
tests: [{
name: 'Testing'
}, {
name: 'Testing 2'
}, {
name: 'Testing 3'
}]
}
});

It's probably something simple but I can't quite figure it out?


More From » vue.js

 Answers
53

It looks like v-for and v-else don't work together well. I would place the condition higher up (on the <tbody>) and use v-if instead (that way, you wont have two <tbody> elements):



<table>
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody v-if=tests.length>
<tr v-for=test in tests>
<td>{{ test.name }}</td>
</tr>
</tbody>
<tbody v-else>
<tr>
<td>No data available in table</td>
</tr>
</tbody>
</table>


https://jsfiddle.net/p2ycjk26/4/


[#64247] Wednesday, November 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckinleyk

Total Points: 730
Total Questions: 99
Total Answers: 99

Location: South Georgia
Member since Fri, Nov 13, 2020
4 Years ago
mckinleyk questions
Tue, Sep 7, 21, 00:00, 3 Years ago
Fri, Aug 21, 20, 00:00, 4 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Wed, Mar 25, 20, 00:00, 4 Years ago
Fri, Sep 27, 19, 00:00, 5 Years ago
;