Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js v-show and v-else not working as intended?

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?

like image 560
Ryan Mortier Avatar asked Nov 27 '15 19:11

Ryan Mortier


2 Answers

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/

like image 148
nils Avatar answered Oct 22 '22 09:10

nils


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.

like image 22
Abdullah Khan Avatar answered Oct 22 '22 10:10

Abdullah Khan