[Question] Can I set for loop value in components render?
Question
Hi,I'm beginner in grapesjs.
I want to make a question number by for loop when I dropped components,
↓I want final like this image. the div can show for loop number.
↓my component's code
// default html
const questionCom =
`<div class="gjs-row">
<div class="gjs-cell">{{i}}</div> //number variable
<div class="gjs-cell"></div>
</div>`;
const dc = editor.DomComponents;
const textType = dc.getType('text');
editor.BlockManager.add('m2', {
label: "question number",
activate: true,
content: {
type:'m2',
questionCom ,
},
}),
dc.addType('m2', {
model: textType.model.extend({
//...
}
}),
view:textType.view.extend({
onActive(){
//...
},
render(val) {
var a=""
this.model.set('components',
function a1(){ //on onActive I call function a1
for(var i=0;i<3;i++){
a+=`<div class="gjs-row"><div class="gjs-cell">`+
val+
`</div><div class="gjs-cell"></div></div>`
}
return a
}
}
return this;
},
})
});
In render, I divided the my default html questionCom from {{i}} into front and back. then,put them in render. {{i}} can perfect to show 1 2 3 in component.
but ,the divided method is not good. HTML can't easy to read and I can't set complex html if I want like↓
<div class="gjs-row"> //one div
<div class="gjs-cell"> //two div
<div class="gjs-row"> //three div
<div class="gjs-cell">{{i}}</div> //four div
<div class="gjs-cell"></div>
</div>
</div>
<div class="gjs-cell"></div>
</div>
Have any better method can solve my question? Thanks!
Answers (1)
Use the questionCom you're passing to the model (use model to update model related logic) and I'd suggest using API from the Components documentation (eg. textType.view.extend is the old one)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2584
Set a button from Component settings active on click
So i made a custom countdown with a button The button set the countdown but i want to make the button active like other elements on click....
Issue #1723
[QUESTION] replacing particular text in dropped component
Hi, I get the value from custom traits from what I entered but I don't know how to change the particular value in dropped component and ren...
Issue #2865
[QUESTION] enable 'view components' button on panel by default
this button is currently off by default. so you have to turn it on every time, which is not obvious to everyone. how do you make it active...
Issue #2583
Breakpoints don't match media query in canvas
Hey @artf I have multiple breakpoints The problem is that when i am a resolution (set using the icons) like 411 (mobile) not always the can...
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.