A smart way to display “No items found” on a list
It’s often front-end developers use IF/ELSE to display “No Items found” when using dynamic lists on HTML.
This solution works for every framework (Angular, React, Vue, …) as it’s based on CSS.
Giving an angular example below:
or even
- {{item}}
- No item
Sometimes the example above uses pipe/filters:
Or maybe functions:
To solve this problem, you can simply use CSS, and it will work in any situation:
.no-item:not(:first-child) { display:none; }
Add this class to the last item on your list, and it will just work! Note: that the list has to be wrapped by any tag, and in that wrap, and all the items as siblings and wrapped by the same tag (commonly used with ul and li)
Find a working example below: