[QUESTION] Dynamically changing Trait Properties
Question
Hi, I went through the other questions posted here and could not get it to work.
All I need to do is to access a specific trait of a specific component (or selected one) and to set some of its attributes. I default the trait to display: none and when I did some server logic the trait gets a value and is displayed to the user.
I did this:
var blockManager = editor.BlockManager;
var domComps = editor.DomComponents;
var dType = domComps.getType('default');
var dModel = dType.model;
var dView = dType.view;
// Add components
loadComponents(editor, options);
// Add navigation block
blockManager.add('NAVIGATE_SCREEN', {
id: 'initiateJob',
label: 'Navigate to screen',
content:{
content:'<div class="my-block fa fa-paint-brush">screen name</div>',
type:'NAVIGATE_SCREEN'
},
});
//component traits
domComps.addType('NAVIGATE_SCREEN', {
model: dModel.extend({
defaults: Object.assign({}, dModel.prototype.defaults, {
traits: [
{
type: 'screenID',
label: 'Screen ID',
name: 'screenID',
attributes: {style: 'display: none'}
},
{
type: 'screenName',
label: 'Screen name',
name: 'screenName',
attributes: {style: 'display: none'}
},
{
type: 'screenSelectorButton',
label: 'Click to select',
name: 'ScreenSelector'
},
],
}),
}, {
isComponent: function(el) {
if(el.tagName == 'INPUT'){
return {type: 'input'};
}
},
}),
view: dView,
});
//extend screenSelectorButton
editor.TraitManager.addType('screenSelectorButton', {
events: {
'click': 'selectScreen' , // trigger parent onclick method on click
},
selectScreen: function(){
var flowTemplate = prompt("screen name: ");
//implement
//NavToHomeScreenOpenPopup(); //call the fake notify
//implement - after selection update trait value:
domComps.addType('NAVIGATE_SCREEN', {
model: dModel.extend({
defaults: Object.assign({}, dModel.prototype.defaults, {
traits: [
{
type: 'screenID',
label: 'Screen ID',
name: 'screenID',
attributes: {style: 'display: none'}
},
{
type: 'screenName',
label: 'Screen name',
name: 'screenName',
attributes: {style: 'display: none'}
},
{
type: 'screenSelectorButton',
label: 'Click to select',
name: 'ScreenSelector'
},
],
}),
}, {
isComponent: function(el) {
if(el.tagName == 'INPUT'){
return {type: 'input'};
}
},
}),
view: dView,
});
},
getInputEl() {
var button = document.createElement('button');
button.id = 'btnScreenSelector';
button.classList.add('actionButton');
button.appendChild(document.createTextNode('Select a screen'));
return button;
},
});
//code for setting via jQuery
//get the screenName trait in the current context, change its value
var screenNameEl= document.querySelector('.gjs-trt-trait input[type=\'screenName\']');
screenNameEl && grapesjs.$(screenNameEl).val('" + OrgScreen.Name + "').trigger('change');
//get screenNameEl's gjs-trait parent and set it to display
//only changes DOM but not editor components
var screenNameElParentDiv = $('.gjs-field-screenName').parents('div[class^=\'gjs-trt-trait\']').eq(0);
screenNameElParentDiv.css('display', 'flex');
Essentially, I need to be able to access the screenName type trait and change its attributes (value, style etc.) so that it reflects in the local storage.
Thanks.
Answers (2)
Please read carefully this https://grapesjs.com/docs/modules/Traits.html and check your code, it's a bit a mess (you're mixing component code inside traits)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #865
[QUESTION] How can I set default tagName
Hi ! I want to set tagName tr for all components(or specific component in use). Is there any predefined function to set tagName ?
Issue #1169
[Question] Get and set some styles to active element (in components)
Is it possible to have some style attributes, for example, color, background-color, font-size, in component settings tab, together with tra...
Issue #1959
[Question] Edit innerHTML in a custom button
Hi All, I am trying to change the text in a button component, but I am not able to edit it. I added this type: I included the editable: tru...
Issue #1537
Duplicate component not style
Hi, First of all, thanks a lot for the awesome work ❤️ !!! I've a question about the duplication tool so here the steps to follow :I drag&d...
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.