Vue: Else statement for v-for directive

Created on 11 Feb 2016  路  8Comments  路  Source: vuejs/vue

First of all huge thanks Evan! Vue's great!

To the point: I am developing app that uses many lists. Those list are often filtered by user input. Before switching to Vue I used plain Django templates, and relied heavily on {% empty %} tag.

Unfortunately there is no such directive in Vue core. In my opinion it would be incredibly awesome and handy to be able to write:

Have you ever thought about implementing it? What's your opinion?

Most helpful comment

Thanks for the answer! Yeah sure, I already used that a couple times. Why do you think it is not a good idea to implement?

All 8 comments

You can use a custom filter to "record" the length of the filtered result:

Vue.filter('recordLength', function (result, key) {
  this.$set(key, result.length)
})
<li v-for="product in products | filterBy q in 'name' | orderBy sortKey sortOrder | recordLength 'productCount'">
</li>
<li v-if="!productCount">
    There are no products matching your query
</li>

Thanks for the answer! Yeah sure, I already used that a couple times. Why do you think it is not a good idea to implement?

I second that!

@yyx990803 Shouldn't the recordLength filter return the result ?

I'm using vue 1.0.26 and if i don't return the result from the filter nothing appears on the screen.

@shadoWalker89 indeed.

Vue.filter('recordLength', function (result, key) {
  this.$set(key, result.length)
  return result
})

How can you display the value of the number of results? I tried {{productCount}} or {{productCount.length}} but both displays nothing.

This answer doesn't seem to work anymore. Is there a new way of accomplishing this?

@GioLogist did you find a solution?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robertleeplummerjr picture robertleeplummerjr  路  3Comments

paceband picture paceband  路  3Comments

finico picture finico  路  3Comments

franciscolourenco picture franciscolourenco  路  3Comments

loki0609 picture loki0609  路  3Comments