Asked 1 month ago by ZenithRanger245
Vue 3: How Do I Exclude Specific Items from a q-select Using a Computed Property?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by ZenithRanger245
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Good morning,
I have a q-select component that displays a list of employers from an array with identifiers and labels. I want to exclude specific items from this list (specifically the ones corresponding to lines 6 and 7).
Here is my q-select component code:
HTML<q-select dense v-model="DataOrganisation.id_ref_employeur" :options="filtrageListeEmployeurs" :disable="DisableEmployeur" :rules="[val => !!val || 'L'employeur est obligatoire']" options-dense option-value="id" option-label="nom" emit-value map-options class="custom-q-select" />
My data is structured as follows:
JAVASCRIPTconst OptionsListeEmployeurs = JSON.parse(localStorage.getItem('Liste_Employeurs')) [{id: 10, nom: "AIRPL"}, {id: 9, nom: "Autres"}, {id: 2, nom: "CNRS"}, {id: 12, nom: "CTS"}, ... 0: {id: 10, nom: "AIRPL"} 1: {id: 9, nom: "Autres"} 2: {id: 2, nom: "CNRS"} 3: {id: 12, nom: "CTS"} 4: {id: 11, nom: "ESB"} 5: {id: 7, nom: "ICAM"} 6: {id: 1, nom: "IMT Atlantique"} 7: {id: 5, nom: "IMT (autre)"} 8: {id: 14, nom: "IMT Transfert"} 9: {id: 3, nom: "INRIA"} 10:{id: 13, nom: "UBO"} 11:{id: 4, nom: "Univ Nantes"}
This is my filtering function in a computed property:
JAVASCRIPTconst filtrageListeEmployeurs = computed(() => { return OptionsListeEmployeurs.filter(item => ![1, 5].includes(item.id)) })
How can I exclude the items from lines 6 and 7 in my list?
Thank you.
Comments 0
•Answers 1
•Views 15
Answer by TychoAI Agent • 1 month ago
Below is one way to do that using the filter method on your OptionsListeEmployeurs array. Since line 6 and line 7 in your data have id values 1 and 5 respectively, your computed property can filter them out like this:
JAVASCRIPTconst filtrageListeEmployeurs = computed(() => { return OptionsListeEmployeurs.filter(item => ![1, 5].includes(item.id)) })
In this code, the filter method iterates over each item in OptionsListeEmployeurs and removes those whose id is either 1 or 5. This way, the q-select will only display the remaining items.
Make sure that the id values you want to exclude (1 and 5) correctly correspond to the lines you wish to remove.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.