Issue #2027Opened May 22, 2019by sdrebel0 reactions

[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?

Code SnippetTEXT
**Dropdown**
![before](https://user-images.githubusercontent.com/19382262/58178404-c52c6a00-7cc3-11e9-8a70-e00e45ed71ee.jpg)

**Extra fields**
![after](https://user-images.githubusercontent.com/19382262/58178909-b5615580-7cc4-11e9-968e-d5b35781c39f.jpg)

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)

sdrebelMay 23, 20190 reactions

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' })

sdrebelMay 24, 20190 reactions

Found the solution from the latest code.

this.addTrait({ type: 'checkbox', name: 'muted' })

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.