vue3.x 使用jsplumb进行多列拖拽连线

前言:

最近很多小伙伴问到使用jsplumb进行多列拖拽连线怎么实现?
下面介绍vue3.x 使用jsplumb进行多列拖拽连线示例,以三列举例:

  1. 安装 npm install --save jsplumb
  2. 引入
<script lang="ts" setup>
  import {ref, reactive,onMounted} from 'vue'
  import jsPlumb from 'jsplumb'
</script>
  1. 使用
    在这里插入图片描述
<template>
    <h3>jsplumb使用 ( 多列 )</h3>
    <div id="container">
        <div class="col1">
            <div v-for="item in list1" :key="item.nodeId" :id="item.nodeId" name="source">{{ item.name }}</div>
        </div>
        <div class="col2">
            <div v-for="item in list2" :key="item.nodeId" :id="item.nodeId" name="center">{{ item.name }}</div>
        </div>
        <div class="col3">
            <div v-for="item in list3" :key="item.nodeId" :id="item.nodeId" name="target">{{ item.name }}</div>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import jsPlumb from 'jsplumb'
//jsplumb使用
let $jsPlumb = jsPlumb.jsPlumb;
let jsPlumb_instance = null; // 缓存实例化的jsplumb对象
const list1 = reactive([
    { name: "name1", nodeId: "name1" },
    { name: "name2", nodeId: "name2" },
    { name: "name3", nodeId: "name3" },
    { name: "name4", nodeId: "name4" },
    { name: "name5", nodeId: "name5" },
    { name: "name6", nodeId: "name6" }
]);
const list2 = reactive([
    { name: '数据2-1', nodeId: 'data2-1' },
    { name: '数据2-2', nodeId: 'data2-2' },
    { name: '数据2-3', nodeId: 'data2-3' },
    { name: '数据2-4', nodeId: 'data2-4' },
    { name: '数据2-5', nodeId: 'data2-5' },
    { name: '数据2-6', nodeId: 'data2-6' }
]);
const list3 = reactive([
    { name: '数据3-1', nodeId: 'data3-1' },
    { name: '数据3-2', nodeId: 'data3-2' },
    { name: '数据3-3', nodeId: 'data3-3' },
    { name: '数据3-4', nodeId: 'data3-4' },
    { name: '数据3-5', nodeId: 'data3-5' },
    { name: '数据3-6', nodeId: 'data3-6' }
]);

onMounted(() => {
    showPlumb();
})

const showPlumb = () => {
    jsPlumb_instance = $jsPlumb.getInstance({
        Container: 'container', // 选择器id
        EndpointStyle: { radius: 0.11, fill: '#fff' }, // 端点样式
        PaintStyle: { stroke: '#000', strokeWidth: 2 }, // 绘画样式,默认8px线宽  #456
        HoverPaintStyle: { stroke: '#1E90FF' }, // 默认悬停样式  默认为null
        ConnectionOverlays: [ // 此处可以设置所有箭头的样式,因为我们要改变连接线的样式,故单独配置
            ['Arrow', { // 设置参数可以参考中文文档
                location: 1,
                length: 10,
                paintStyle: {
                    stroke: '#000',
                    fill: '#000'
                }
            }]
        ],
        Connector: ['Straight'], // 要使用的默认连接器的类型:直线,折线,曲线等
        DrapOptions: { cursor: 'crosshair', zIndex: 2000 }
    },)

    console.log(jsPlumb_instance)

    jsPlumb_instance.batch(() => {

        for (let i = 0; i < list1.length; i++) {
            initLeaf(list1[i].nodeId, 'source')
        }
        for (let i = 0; i < list2.length; i++) {
            initLeaf(list2[i].nodeId, 'center')
        }
        for (let i = 0; i < list3.length; i++) {
            initLeaf(list3[i].nodeId, 'target')
        }
    })


    jsPlumb_instance.bind('click', (conn, originalEvent) => {
        jsPlumb_instance.deleteConnection(conn)
    })

}

// 初始化具体节点
const initLeaf = (id, type) => {
    const ins = jsPlumb_instance;
    const elem = document.getElementById(id)
    if (type == 'source') {
        ins.makeSource(elem, {
            anchor: 'Right',
            allowLoopback: false,
            maxConnections: 1
        })
    } else if (type == 'center') {//中间列,即能主动连接,也能被动连接
        ins.makeSource(elem, {
            anchor: 'Right',
            allowLoopback: false,
            maxConnections: 1
        })
        ins.makeTarget(elem, {
            anchor: 'Left',
            allowLoopback: false,
            maxConnections: 1
        })
    } else {
        ins.makeTarget(elem, {
            anchor: 'Left',
            allowLoopback: false,
            maxConnections: 1
        })
    }
}

</script>

<style scoped lang="less">
#container {
    position: relative;
    margin-top: 20px;
    width: 100%;
    height: 300px;
}

.col2,
.col1,
.col3 {
    float: left;
    text-align: center;
}

.col1 {
    width: 80px;
}

.col2 {
    width: 120px;
    margin-left: 80px;
}

.col3 {
    width: 120px;
    margin-left: 80px;
}

#container>div>div {
    line-height: 30px;
    margin: 0 0 17px 0;
    background: #ef631e;
    color: #fff;
}
</style>

小伙伴们对jsplumb感兴趣的话,可以查看我的其他几篇关于jsplumb文章。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现js功能,可以使用html5的放API或者jQuery UI等库来实现。下面是一个使用jQuery UI实现功能的例子: ```html <!DOCTYPE html> <html> <head> <title>功能实现</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.4.7/js/jsplumb.min.js"></script> <style type="text/css"> #container{ position: relative; width: 500px; height: 500px; border: 1px solid #000; } .block{ position: absolute; width: 50px; height: 50px; background-color: #f00; cursor: move; text-align: center; line-height: 50px; font-size: 20px; color: #fff; } </style> <script type="text/javascript"> $(function(){ //初始化jsPlumb实例对象 jsPlumb.ready(function(){ var instance = jsPlumb.getInstance({ Endpoint: ["Dot", {radius:2}], Connector:"StateMachine", HoverPaintStyle: {stroke:"#1e8151", strokeWidth:2 }, ConnectionOverlays: [ [ "Arrow", { location:1, id:"arrow", length:14, foldback:0.8 } ] ] }); }); //初始化功能 $(".block").draggable({ containment: "#container", stop: function(event, ui) { //更新jsplumb连接线 jsPlumb.repaint(ui.helper); } }); //初始化连接功能 jsPlumb.connect({ source: "block1", target: "block2" }); }); </script> </head> <body> <div id="container"> <div id="block1" class="block">1</div> <div id="block2" class="block" style="top: 100px; left: 100px;">2</div> </div> </body> </html> ``` 在上面的代码中,首先通过jQuery UI的draggable方法初始化了功能,然后通过jsPlumb的connect方法初始化了连接功能。同时在jsPlumb.ready方法中初始化了jsPlumb实例对象,以便后续使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值