Issue #2027💬 AnsweredOpened May 22, 2019by sdrebel0 reactions

How to add traits dynamically based on the dropdown selection

快速解答by sdrebel

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

Read full answer below ↓

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?

代码片段TEXT
**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, 2019

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, 2019

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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.