Programatically add/remove options from listbox ?? #1210
Answered
by
tlouisse
naveen2593kumar
asked this question in
Q&A
-
Hello team, I need to update the listbox at runtime, I see the listbox uses declarative approach like
|
Beta Was this translation helpful? Give feedback.
Answered by
tlouisse
May 23, 2021
Replies: 2 comments 3 replies
-
I'm not sure I understand what the question or point of discussion is? If you want to you can also create it with JS: const listboxEl = document.createElement('lion-listbox');
listboxEl.name = 'listbox';
listboxEl.label = 'Default';
['Apple', 'Artichoke', 'Asparagus', 'Banana', 'Beets'].forEach(fruit => {
const optionEl = document.createElement('lion-option');
optionEl.choiceValue = fruit;
optionEl.innerText = fruit;
listboxEl.appendChild(optionEl);
}); Another way is using a Lit property + render update: class FooElement extends LitElement {
static get properties() {
return {
fruits: { attribute: false },
}
}
constructor() {
super();
this.fruits = ['Apple', 'Artichoke', 'Asparagus', 'Banana', 'Beets'];
}
render() {
return html`
<lion-listbox name="listbox" label="Default">
${fruits.map(fruit => html`
<lion-option .choiceValue=${fruit}>${fruit}</lion-option>
`)}
</lion-listbox>
`;
}
} Then whenever the array of fruits is set to a new value (this.fruits = ...) LitElement will make sure the template is re-rendered with the needed changes. |
Beta Was this translation helpful? Give feedback.
3 replies
-
Fixed by d94d6bd |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
tlouisse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed by d94d6bd