List/Array Trait
Question
Hello,
I am trying to write up a trait that allows creating a list of forms and generates the respective in the model. So far I have this:
editor.TraitManager.addType('list', {
events: {
'keyup': 'onChange'
},
getInputEl: function() {
var self = this;
var idx = 1;
function createForm(input) {
self.model.attributes.options.map(function(m) {
// TOOD: Add select options
// console.log(m);
if (m.type === 'select') {
var s = $('<select name="'+ m.name +'"></select>').get(0);
s.appendChild($('<option value="">'+ m.placeholder +'</option>').get(0));
m.options.map(function(opt) {
s.appendChild($('<option value="'+ opt.value +'">'+ opt.name +'</option>').get(0))
})
input.appendChild(s);
} else {
input.appendChild($('<input name="'+ m.name +'" placeholder="'+ m.placeholder +'"/>').get(0))
}
});
input.appendChild($('<button id="'+ idx +'" class="btn btn-sm btn-danger pull-left"><i class="fa fa-minus"></i> Remove</button>').get(0))
}
if (!this.inputEl) {
var input = document.createElement('div');
var btn = $('<button class="btn btn-sm btn-default"><i class="fa fa-plus"></i> Add</button>');
// Listen for clicks on add button and add to the model
btn.on('click', function(ev) {
// var id = $(ev.currentTarget).data('');
idx += 1;
createForm(input);
});
input.appendChild(btn.get(0));
createForm(input);
// input.value = this.target.get('content');
this.inputEl = input;
}
return this.inputEl;
},
onValueChange: function () {
console.log(this.model.get('value'));
this.target.set('list', this.model.get('value'));
}
});
The current issues I am unable to bind the resulting elements to the onChange event listeners. Will I have to manually wire up each element to a listener? If so how would I proceed to update the form's model?
Thank you.
Answers (2)
@dkmurakaru pay attention that you're creating not a real input
var input = document.createElement('div');
therefore you should update also methods like onChange and setInputValue
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 #1025
How add new traits to custom block
grapesjs.plugins.add('CustomBlocks', function(editor, options) { options = options || {}; TraitsToComponent(); addHTMLCodeComponent(); addH...
Issue #413
Use trait to modify component class
Hi! I'm trying to change the value of the classes with very little succes. Here is my custom trait's onValueChange function: Which results...
Issue #1998
Extending Link component not working properly.
hello @hello @artf I am trying to extend the link component and add new traits like href and target the code is given below. `function load...
Issue #1894
[BUG] Custom Trait documentation example doesn't work
Specifically this: https://grapesjs.com/docs/modules/Traits.html#define-new-trait-type First of all, the keyUp event references onChange wh...
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.