I can't seem to get v-show
and v-else
to work. The documentation says:
The
v-else
element must following immediately after thev-if
orv-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?
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/
From the docs
A v-else element must immediately follow a v-if or a v-else-if element - otherwise it will not be recognized.
Also see this
Note that v-show doesn’t support the
<template>
syntax, nor does it work with v-else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With