[QUESTION] How to store "data-" attributes into the html tag of the component
Question
Hi, I'm developing a plugin for charts using chart.js. In order to the generated template render the chart from a data model generated by the plugin, I need to store this model into the html tag of the component. The model used by the chart is like this:
{
"type": "line",
"data": {
"labels": ["Rótulo 1", "Rótulo 2", "Rótulo 3", "Rótulo 4"],
"datasets": [{
"label": "Dataset 1",
"data": [3, 9, 8, 2],
"backgroundColor": "rgba(252,111,116, 0.7)",
"borderColor": "rgba(35,100,224, 0.7)",
"borderWidth": 2,
"showLine": true,
"pointStyle": "circle"
}]
}
}
And have to be stored this way:
<canvas class="gjs-chart" data-gjs-chart-model="
{
'type': 'line',
'data': {
'labels': ['Rótulo 1', 'Rótulo 2', 'Rótulo 3', 'Rótulo 4'],
'datasets': [{
'label': 'Dataset 1',
'data': [3, 9, 8, 2],
'backgroundColor': 'rgba(252,111,116, 0.7)',
'borderColor': 'rgba(35,100,224, 0.7)',
'borderWidth': 2,
'showLine': true,
'pointStyle': 'circle'
}]
}
}">
However, I don't know how to do this. If I do:
this.model.set('data-gjs-chart-model', JSON.stringify(theChartModel));
the html attribute "data-gjs-chart-model" is not stored in the "html" section of the storage, only in the "components" section. This way, the generated chart only works on a grapesjs environment, but doesn't work if I take just the html and css of the generated template and make a page outside a grapesjs environment.
Answers (2)
I have seen there's
component.setAttributes({ id: 'test', 'data-key': 'value' });
method, I'll test this and close this issue.
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 #1414
[QUESTION] Component "first render" event
Hi. I'm developing a plugin for charts using chart.js. I need to persist the model of the chart inside the generated component to render th...
Issue #957
[Question] Add custom attributes (data-) to assets/images on Upload/Selection
Hi, I am trying my best to find the proper/best way to do this, but I need to attach two custom HTML5 data tags to an image once it is sele...
Issue #1012
Is it possible to export the html content with data attributes in editor?
@artf Is it possible to export the html content with data attributes in order reuse? We discussed about this on earlier , on How to pass da...
Issue #1255
[QUESTION]: How to set editor html contents from custom component?
I'm having trouble getting a custom block+component to display its template HTML inside the grapes editor. Within the exported HTML, the ma...
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.