question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] v-empty for v-for

See original GitHub issue

It’s a common issue when rendering lists to show a message in case the list is empty. In Django, for example, {% for %} template tag supports {% empty %} for this:

{% for platform in game.platform_set.all %}       
    <td>{{ platform.system }} -- ${{ platform.price}}</td> 
{% empty %}
    <td>No Platforms</td>
{% endfor %}

Probably we could benefit from the similar feature in Vue.

There are cases with the list being contained inside an element to be hidden when the list is empty, and it works just fine with Vue if we want to hide the whole container:

<ul v-if="items.length">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>
<p v-else>Empty list</p>

When it’s not the case, however, the syntax is getting a little verbose. Emulating the Django example mentioned above:

<td v-for="platform in game.platforms" v-if="game.platforms.length">
  {{ platform.system }} -- ${{ platform.price }}
</td>
<td v-else>No Platforms</p>

we could benefit from a simplification, getting rid of expicit v-if:

<td v-for="platform in game.platforms">
  {{ platform.system }} -- ${{ platform.price }}
</td>
<td v-else>No Platforms</p>

UPD: v-empty could form a nice name as well, in case we’d like to avoid conflicts with existing v-else implementation.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:23
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

32reactions
matthiasgcommented, May 11, 2018

@yyx990803 I am also concerned about enlarging APIs for convenience, but since v-for also handles the case of iteration through objects that aren’t arrays, I would indeed value a v-empty or v-for-empty directive. Iterating through e.g an objects properties does not have a direct length property etc. The fact that an iterator is empty seems very much symmetrical to v-if/v-else to me.

In my current case, I iterate through properties (easy with v-for) without necessarily needing to express in the template how the iterator is implemented. Since v-for has to iterate anyway, extending it to keep a boolean (empty/notempty) and rendering an ‘empty’ branch if the loop content was never touched after iteration should be helpful, straightforward and more performant than calling Object.keys(obj).length even if the template includes an understanding of the iterator implementation.

Empty Iterator branches also have precedence in all other template languages I used so far.

The current way seems quite clumsy (especially since it binds the template to an implementation detail of the iterator) and it won’t really get any better when using a component method on this either.

<section v-for="(part,key) of locals.missing">
  <h3>{{key}}</h3>
  <span>{{part}}</span>
</section>
<section v-if="Object.keys(locals.missing).length === 0">
  <span v-translate="vt>All translations available</span>
</section>
22reactions
matthiasgcommented, Jan 20, 2021

To be honest I would rather write:

<td v-for="item in items">{{ item.message }}</td>
<td v-empty>Empty list</td>

than

<template v-if="items.length">
  <td v-for="item in items">{{ item.message }}</td>
</template>
<td v-else>Empty list</td>
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature Request]: allow searching for empty fields #11622
For example, I need to search for unassigned assets (where "checked out to" is empty) from the list of all the assets or...
Read more >
Feature Request: Empty start url, preselect "about:blank"
Hello, i'm using edge with an empty start page. After starting edge: With edge legacy : the address field is empty, i have...
Read more >
empty / Feature Requests - SourceForge
#. Sort A ‑> Z; Sort Z ‑> A Summary▾. Sort A ‑> Z; Sort Z ‑> A Milestone▾. Sort A ‑> Z;...
Read more >
Feature Request: Hide empty categories
I have added a feature request for the ability to hide categories in the sidebar as you suggest, and to look at the...
Read more >
Scheduled Reports - Conditional (Non-Empty)
Scheduled Reports - Conditional (Non-Empty). Add a check box to the Schedule Manager for "Only Send If Not Empty." I have a large...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found