[QUESTIONS] How to add traits dynamically based on the dropdown selection
Question
Hi @artf , I have a custom component with custom traits. When I select the option from the dropdown, more fields needs to be added based on the selection. How do I do it?
**Dropdown**

**Extra fields**

Code is below, I have added the listener but not getting the option to add new fields to the current trait setting.
DomComps.addType('linking', {
model: CustomType.model.extend({
init() {
this.listenTo(this, 'change:link-type', function() {
alert();
});
},
defaults: Object.assign({}, CustomType.model.prototype.defaults, {
traits: [{
label: 'Title',
name: 'title',
placeholder: 'Title'
}, {
type: 'select',
label: 'Type',
name: 'link-type',
options: [
{ value: 'link', name: 'Link' },
{ value: 'popup', name: 'Popup' },
],
changeProp: 1
}, {
label: 'Link',
name: 'data-link',
placeholder: 'Link'
}, {
label: 'Tracking',
name: 'data-track',
placeholder: 'Tracking Id'
}],
}),
}),
view: CustomType.view.extend({
events: {
},
}),
});Answers (2)
I have tried this below code and it's adding in the traits array. But not reflecting in the editor.
domComps.getType('linking').model.prototype.defaults.traits.push({ name: 'product' })
Found the solution from the latest code.
this.addTrait({ type: 'checkbox', name: 'muted' })
Related Questions and Answers
Continue research with similar issue discussions.
Issue #417
Add Setting button to custom component.
Hello, How can I create a setting or you can say edit button for my custom component? LIke 4th option on this image:
Issue #1826
[Question] How to add link to an image?
Hi Artur, How to add a link to this image? If i need to create a custom component, please give me some example code Artur.. Thank you so mu...
Issue #987
[BUG/Question] Dropdown indicator not displayed for a custom trait
Hello, the dropdown (select) element arrow indicator icon is not rendered when adding a custom trait to a component: I'm using following co...
Issue #995
[Question ] how to add fields on trait dynamically
I am building the trait when the application starts. so getting the below fields at side bar as settings Now I want more field to be loaded...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.