Hello Team,
I am a bit new to Syncfusion + Vue component, I am trying to implement the dynamic creation of the `HTML Nodes` within the `EJS Diagram` within my `Vuejs/Nuxtjs` application. All I want to do is create the `HTML Nodes` within the `EJS Diagram` when the user clicks on `Add Node` button. Each of the `HTML Node` should consist of a `Button` within it so I can perform some things when it is clicked.
I have done it in `Angular` but am unable to do the same within my `Vuejs/Nuxtjs` application. I have added my code to the `CodeSandbox`: https://codesandbox.io/s/infallible-meadow-prmp5?file=/pages/index.vue
Can you please help me out with this issue and let me know how can I create the nodes dynamically with `HTML Button` inside it within my `Vuejs/Nuxtjs` application?
I want to achieve something like this within my application:
|
addNode () {
const ports = [
{ id: 'port1', offset: { x: 0, y: 0.5 }},
{ id: 'port2', offset: { x: 1, y: 0.5 }},
{ id: 'port3', offset: { x: 0.5, y: 0 } },
{ id: 'port4', offset: { x: 0.5, y: 1 } }
]
const name = 'Event' + this.nodeCounter
const label = [{ text: name, offset: { x: 0.5, y: 0.06 } }]
const drawingshape = {
type: 'HTML',
content: `<div style="background-color: red;width: 100%;height: 100%;" class="text-center">
<button class="btn btn-info btn-sm" @click='ShowFormDataModal($event)'> Event Info </button>
<div style="margin-left:45%">annotation</div>
</div>`,
value: {
select: this.nodeCounter
}
}
const node = {
shape: drawingshape,
ports:ports,
annotations:[{ content:"ssss", offset: { x: 0.5, y: 0.06 } }]
}
diagramInstance.drawingObject = node
// To draw an object once, activate draw once
diagramInstance.tool = DiagramTools.DrawOnce
diagramInstance.dataBind()
this.nodeCounter++
}, |
@Gowtham
Thanks a lot for your response. I have some more doubts regarding the HTML Node creation and usage. I would like to add a button in each of the created HTML Nodes (this is working correctly now) but when I click the button I want to trigger the method/function to perform some actions which it is unable to do so. I have created the method but when I click on HTML Node button nothing is performed.
Also, I would like to add some beautification aspects to my HTML Node, I do not want the Node to be transparent and it should have a border something like as shown in this main question.
Can you please guide me through the documentation or provide some explanation using an example?
Following is the code I have currently:
<template>
<div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<button class="btn btn-primary btn-sm" @click="addNode()">
Add Event
</button>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div id="app">
<ejs-diagram
id="diagram"
ref="diagramObj"
:width="width"
:height="height"
style="display: block"
/>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Vue from 'vue'
import { DiagramPlugin, DiagramTools } from '@syncfusion/ej2-vue-diagrams'
Vue.use(DiagramPlugin)
let diagramInstance
export default {
name: 'App',
data () {
return {
width: '100%',
height: '500px',
nodes: [],
connectorArray: [],
nodeCounter: 0,
connectorCounter: 0
}
},
mounted () {
diagramInstance = this.$refs.diagramObj.ej2Instances
},
methods: {
// On click of the add node button add the node to diagram
addNode () {
const ports = [
{ name: 'port1', offset: { x: 0, y: 0.5 }, shape: 'circle' },
{ name: 'port2', offset: { x: 1, y: 0.5 }, shape: 'circle' },
{ name: 'port3', offset: { x: 0.5, y: 0 }, shape: 'circle' },
{ name: 'port4', offset: { x: 0.5, y: 1 }, shape: 'circle' }
]
const name = 'Event' + this.nodeCounter
// const label = [{ text: name, offset: { x: 0.5, y: 0.06 } }]
const drawingshape = {
type: 'HTML',
content: `<div style="margin-top:15%;" class="text-center">
<button class="btn btn-info btn-sm" @click='showEventInfoModal($event)'> Event Info </button>
</div>`,
value: {
select: this.nodeCounter
}
}
const node = {
shape: drawingshape,
ports,
style: {
strokeColor: 'black',
strokeWidth: 3
},
annotations: [
{
content: name,
offset: { x: 0.5, y: 0.06 }
}
]
}
diagramInstance.drawingObject = node
// To draw an object once, activate draw once
diagramInstance.tool = DiagramTools.DrawOnce
diagramInstance.dataBind()
this.nodeCounter++
},
// On click of the EventInfo button within each node call the method to display modal and get info
showEventInfoModal (event) {
console.log('DISPLAY MODAL FOR INFO')
console.log(event)
}
}
}
</script>
<style>
@import "@/node_modules/@syncfusion/ej2-vue-diagrams/styles/material.css";
</style>
@Sivakumar
Thanks a lot for the sample code and video link. This is working as expected for me.
I have two small questions.
As you can see from the main question image I have the Node assigned with the Label -> Event1, similarly, I want to have for these Nodes. Currently I am using the Annotations but they are not visible on the HTML Node.
Following is the current code I have:
@Sivakumar
I am really hoping to get some suggestions from your side as I am struck at the above-mentioned 2 issues. Any input or workaround from your side will be really useful for me. Thanks a lot in advance.
Thanks and Best regards,
|
I would like to keep track of each Node that is added to the Diagram so I am creating a unique number and assigning it to each Node which will be added as an Annotation but they are not visible on these Nodes because maybe the template is coming from another Vuejs Component. Can you please let me know how can I add the Annotation to each of the created Nodes? |
As we suggested before, if we need to apply annotations to the HTML nodes we have to add it to the template itself as shown in the below code.
| |
|
I would like to keep track of which Node button the user has clicked. Is there a way to identify it? For example, if there are 10 Nodes then I would like to know which Nodes HTML button was clicked by the user. I am already assigning the name to each of the Nodes but when the button is clicked then how can I know which Node was clicked? |
We can assign the unique value to the button and can able to trigger the specified action based on the unique value of the button. Please refer to the below code to add nodes value to the template.
|
@Sivakumar
Thanks a lot for the detailed explanation and for providing the sample code. I am able to understand how we are achieving this.
I am just curious isn't there a way to include the NodeTemplate within the EJSDiagram component that will make managing some of these things a bit easier I feel. As of now, we are using the external NodeTemplate and including it for every Node. Just wanted to know if there is a way to avoid the NodeTemplate.
Because when I implemented something similar within the AngularJS application we were using something like this, which was adding the Button inside each created Node due to which were able to know which Node being clicked using the DiagramInstance:
<script ng-model="NodeEvents" id="htmlTemplate" type="text/ng-template">
<div style="margin-top:15%;" class="text-center">
<button class="btn btn-info btn-sm" ng-click='$parent.ShowFormDataModal($event);'> Event Info </button>
</div>
</script>
Just wanted to know if there is something similar even in Vuejs?
|
const drawingshape = {
type: 'HTML',
content: `<div style="background-color: red;width: 100%;height: 100%;" class="text-center">
<button class="btn btn-info btn-sm"> Event Info </button>
<div style="margin-left:45%">annotation</div>
</div>`,
value: {
select: this.nodeCounter
}
} |
@Sivakumar
Thanks a lot for the answer. You have understood my query correctly. Sure, I will continue to use the NodeTemplate. I have one small query regarding the addition of the Annotation at runtime.
I have the HTML Node and each HTML Node consists of Button. Upon clicking the Button I am opening the Bootstrap Modal where users can provide some information. After submitting the Modal information I would like to obtain the information from this Modal and add it to the HTML Node. Since I am using the HTML Node you were saying we need to add it to the NodeTemplate directly so I am a bit confused on how to add it.
Previously when using AngularJS I was doing something like this:
Initially, when I was creating the Node I was adding the Label then I was updating them something like this:
@Sivakumar
Thanks a lot for your response. I implemented it according to your suggestion and everything seems to be working fine. Thanks for all the help :)