Conditionally render items on Dropdown (Nuxt UI v2)
As the title says, how can one conditionally render items on a dropdown?
I got some users that are on a "trial" account and want to hide some buttons.
Code:
on component:
6 Replies
Hi @Medim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To conditionally render items in a dropdown using Nuxt UI (Nuxt UI v2), you should dynamically generate the items array based on your conditions. In your case, you want to hide certain buttons for users on a "trial" account.
The Nuxt UI DropdownMenu (UDropdownMenu) component expects an array of items, and you can use JavaScript logic to include or exclude items as needed. For example, you can filter or map your items array based on the user's status before passing it to the dropdown.
Here's how you can do it, following the pattern from the official documentation:
And in your template:
Key points:
- Build your items array conditionally in your function.
- Use the
onSelect
property for actions (not click
).
- Return an array of arrays if you want grouped items.
- Pass the computed items to the :items
prop of UDropdownMenu
.
This approach is consistent with the official Nuxt UI documentation, which shows how to dynamically generate dropdown items and control their properties based on your application's state or logic. For more details, see the DropdownMenu items documentation.
If you need to hide or show items based on more complex conditions, simply adjust your logic inside the function that returns the items array.
Note: The component in your code is namedDropdownMenu Items ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------UDropdown
, but the official Nuxt UI component isUDropdownMenu
. Make sure you are using the correct component as per the documentation.
<@229706675936821249> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Hey @Medim if solved do you mind marking it as solved. ❤️
@kapa.ai doesn't it need to be a computed? the trialDropdownItems
Let me check the knowledge base for you again. One moment! :books:
----------------------
No, it is not strictly required for your
- Use a computed property if your items depend on reactive state and you want automatic updates. - A plain function is fine if your items are static or only depend on the function argument. DropdownMenu with checkbox items ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
trialDropdownItems
function to return a computed property. You can return a plain array or an array of arrays, as shown in the official Nuxt UI documentation. However, using a computed property is recommended if you want to ensure reactivity—especially when the checked state or other properties of the items depend on reactive variables.
For example, in the DropdownMenu with checkbox items example, the items array is wrapped in a computed
to ensure that changes to the underlying reactive variables (like showBookmarks
) are reflected in the menu:
If your dropdown items depend on reactive state (such as selectedCompanyId
or row
), using a computed property will ensure the dropdown updates automatically when those values change. If your items are static or only depend on the function argument, a plain function is sufficient.
Summary:- Use a computed property if your items depend on reactive state and you want automatic updates. - A plain function is fine if your items are static or only depend on the function argument. DropdownMenu with checkbox items ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@229706675936821249> Kapa.ai is still learning and improving, please let me know how I did by reacting below.