在vue 中 JSPlumb 修改 lable 的值
在连线的过程中必须有source target 才能链接, 一个source 只能对应 一个 target ,并且只能是 HTML 的 id 所以数据结构是
{
"nodes":[
{
"id":"8b058aab-e01c-4eb4-8deb-716481ec2335",
"name":"BookAuthor",
"type":"table",
"x":791,
"y":113,
"columns":[
{
"id":"book_id",
"datatype":"string",
"primaryKey":false,
"nodeId":"8b058aab-e01c-4eb4-8deb-716481ec2335.book_id"
}
]
},
{
"id":"97ff1fef-3b55-49fa-b8c7-52ba83df0c8b",
"name":"Book",
"type":"table",
"x":288,
"y":142,
"columns":[
{
"id":"id",
"datatype":"string",
"primaryKey":false,
"nodeId":"97ff1fef-3b55-49fa-b8c7-52ba83df0c8b.id"
}
]
}
],
"edges":[
{
"source":"97ff1fef-3b55-49fa-b8c7-52ba83df0c8b.id",
"target":"8b058aab-e01c-4eb4-8deb-716481ec2335.book_id",
"data":{
"type":"N:M"
}
}
]
}
methods: {
jsPlumb.ready( () => {
//把实例放在全局
this.instance = jsPlumb.getInstance({
// 可以添加默认配置
ConnectionOverlays: [
["Label", { label: "1", id: "label-1", cssClass: "aLabel", location:0.15,}],
["Label", { label: "1", id: "label-2", cssClass: "aLabel", location:0.85, }]
],
})
}
}
1、默认修改:就是在后台传送数据之后直接修改
// 默认连线
defaultConenction () {
//如果数据为空直接返回
if (this.dataset.edges === 'undefined') {
return
}
this.dataset.edges.forEach(item => {
// 连线成功之后重新添加 closeId 的 个数
let connection = this.instance.connect({
source: item.source,
target: item.target,
type: "basic",
endpointStyle: { fill: '#f35958' },
// 必须在这里配置才能使用 getOverlay,在这里我是踩了很大一个坑
ConnectionOverlays: [
["Label", { label: "1", id: "label-1", cssClass: "aLabel", location:0.15,}],
["Label", { label: "1", id: "label-2", cssClass: "aLabel", location:0.85, }]
],
});
let type = item.data.type.split(':')
if (connection) {
connection.getOverlay("label-1").setLabel(`${type[0]}`);
connection.getOverlay("label-2").setLabel(`${type[1]}`);
}
});
},
1、连线修改: 就是 source 和 target 链接成功之后的修改, 在这里偷了个懒,可以用连接前的检查,判断是否建立连接
this.instance.bind("beforeDrop", (connInfo) => {})
this.instance.bind("connection", (connInfo) => {
// 不能连接自己和类型不一样不能连接, 也可以用方法在链接之前判断时候合法
if (connInfo.connection.sourceId === connInfo.connection.targetId || connInfo.source.type !== connInfo.target.type) {
// 删除连线
this.instance.detach(connInfo.connection);
}else {
this.connInfo = connInfo;
this.connInfo.connection.getOverlay("label-1").setLabel(`${sourceLabel}`);
this.connInfo.connection.getOverlay("label-2").setLabel(`${targetLabel}`);
}
});