Home  >  Q&A  >  body text

Unable to add node or shape in Jsplumb Community Edition

I'm using @jsplumb/browser-ui to create some Nodes in my Nuxtjs/Vuejs application, as described in its documentation. But I want to create nodes at runtime. I can not do it.

I want to create a nodes/rectangle shape whenever the user clicks the Add Event button. So instead of creating the Nodes statically, I want to create it dynamically/runtime based on button click. I don't understand how to do this using the jsPlumb documentation as they don't have a specific code example to achieve the same.

The following is my code:

<template>
  <div>
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-6">
          <button class="btn btn-primary btn-sm" @click="addConnector()">
            Add Connector
          </button>&nbsp;
          <button class="btn btn-primary btn-sm" @click="addNode()">
            Add Event
          </button>&nbsp;
          <button class="btn btn-success btn-sm" @click="submitEvents()">
            Submit
          </button>&nbsp;
        </div>
      </div>
      <div class="row">
        <div class="col-md-12">
          <div id="diagram" ref="diagram" style="position: relative; width:100%; height:100%;" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
let jsPlumb = null

export default {
  data () {
    return {
      nodeCounter: 0,
      nodeArray: [],
      connectorCounter: 0,
      connectorArray: [],
      allEventsInfoArray: []
    }
  },
  async mounted () {
    if (process.browser) {
      const jsPlumbBrowserUI = await import('@jsplumb/browser-ui')

      jsPlumb = jsPlumbBrowserUI.newInstance({
        dragOptions: {
          cursor: 'pointer',
          zIndex: 2000
        },
        container: this.$refs.diagram
      })

      console.log(jsPlumb)
    }
  },
  methods: {
    // On click of Add Node button create the draggable node into the jsPlumb canvas
    addNode () {
      // const container = "<button class='btn btn-info' id='container_" + this.nodeCounter + "'></button>"
      this.nodeCounter++
    },
    // On click of Add Connector button create the draggable node into the jsPlumb canvas
    addConnector () {
      console.log('Add Connector : ')

      jsPlumb.connect({
        anchor: 'AutoDefault',
        endpoints: ['Dot', 'Blank'],
        overlays: [
          { type: 'Arrow', options: { location: 1 } },
          {
            type: 'Label',
            options: { label: 'foo', location: 0.25, id: 'myLabel' }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
</style>

P粉864872812P粉864872812204 days ago339

reply all(1)I'll reply

  • P粉956441054

    P粉9564410542024-03-29 14:06:31

    Hope this answer helps someone in the future:

    According to contributor GitHub's reply, we are unable to create Nodes/Shapes within Jsplumb Community Edition .

    I started using the DrawFlow library instead of the Jsplumb library, it's great and meets all the requirements I need for my application.

    reply
    0
  • Cancelreply