当前位置:   article > 正文

Vue 使用jsPlumb 实现连线绘图_jsplumb vue

jsplumb vue

第一步:安装jsPlumb

npm i jsplumb

第二步:全局引入

        在main.js 中引入,且挂在Vue事例上

  1. import jsPlumb from 'jsplumb'
  2. Vue.prototype.$jsPlumb = jsPlumb.jsPlumb

第三步:编写组件代码

创建 slsJsPlumbComponent.vue文件

  1. <template>
  2. <div>
  3. <div id="container">
  4. <div class="left1">
  5. <ul class="left1_ul">
  6. <li v-for="(item,index) in leftList" :key="'left' + index" :id="item.nodeId" name="source">
  7. {{item.name}}
  8. </li>
  9. </ul>
  10. </div>
  11. <div class="right1">
  12. <ul class="left1_ul">
  13. <li v-for="(item,index) in rightList" :key="'right' + index" :id="item.nodeId" name="target">
  14. {{item.name}}
  15. </li>
  16. </ul>
  17. </div>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: "slsJsPlumbComponent",
  24. props:{
  25. leftList:{ //左边节点数组[{"name":"xxx",nodeId:"l_xxxx"}]
  26. type:Array
  27. },
  28. rightList:{//右边节点数组
  29. type:Array
  30. },
  31. isLink:{//是否可连接
  32. type:Boolean,
  33. default:true
  34. },
  35. fieldRelas:{//连接的详情
  36. type:Array
  37. }
  38. },
  39. data(){
  40. return {
  41. jsPlumb: this.$jsPlumb.getInstance({
  42. Container: 'container', // 选择器id
  43. EndpointStyle: {radius: 0.11, fill: '#999'}, // 端点样式
  44. PaintStyle: {stroke: '#999', strokeWidth: 2}, // 绘画样式,默认8px线宽 #456
  45. HoverPaintStyle: {stroke: '#994B0A', strokeWidth: 3 }, // 默认悬停样式 默认为null
  46. ConnectionOverlays: [ // 此处可以设置所有箭头的样式
  47. ['Arrow', { // 设置参数可以参考中文文档
  48. location: 1,
  49. length: 12,
  50. paintStyle: {
  51. stroke: '#999',
  52. fill: '#999'
  53. }
  54. }]
  55. ],
  56. Connector: ['Straight'], // 要使用的默认连接器的类型:直线,折线,曲线等
  57. DrapOptions: {cursor: 'crosshair', zIndex: 2000}
  58. }), // 缓存实例化的jsplumb对象
  59. field_rela:[]
  60. }
  61. },
  62. watch:{
  63. leftList(){
  64. if(this.isLink){
  65. this.$nextTick(()=>{
  66. this.showPlumb()
  67. })
  68. }
  69. },
  70. fieldRelas(){
  71. this.$nextTick(()=>{
  72. this.initConnect()
  73. })
  74. }
  75. },
  76. methods:{
  77. showPlumb() {
  78. // this.jsPlumb =
  79. let this_ = this
  80. this.jsPlumb.batch(() => {
  81. for(let i = 0; i < this_.leftList.length; i++){
  82. this_.initLeaf(this_.leftList[i].nodeId, 'source');
  83. }
  84. for(let j = 0; j < this_.rightList.length; j++){
  85. this_.initLeaf(this_.rightList[j].nodeId , 'target')
  86. }
  87. })
  88. this.setjsPlumb(true,true);
  89. //点击连线删除连接
  90. this.jsPlumb.bind('click', (conn, originalEvent) => {
  91. // console.log(conn, originalEvent)
  92. this.jsPlumb.deleteConnection(conn)
  93. let remIndex = -1
  94. for(let i=0;i<this.field_rela.length;i++){
  95. if("l_"+this.field_rela[i].sls == conn.sourceId){
  96. remIndex = i
  97. }
  98. }
  99. this.field_rela.splice(remIndex,1)
  100. this.$emit("getFieldRela",this.field_rela)
  101. })
  102. //连线时触发 存储连接信息
  103. this.jsPlumb.bind('connection', (conn, originalEvent) => {
  104. // console.log(conn.sourceId)
  105. // console.log(conn.targetId)
  106. this.field_rela.push({"sls":conn.sourceId.replace("l_"),"table_field":conn.targetId.replace("r_")})
  107. this.$emit("getFieldRela",this.field_rela)
  108. })
  109. //右键触发
  110. /*this.jsPlumb.bind('contextmenu', (conn, originalEvent) => {
  111. console.log(conn, originalEvent)
  112. })*/
  113. },
  114. // 初始化规则使其可以连线、拖拽
  115. initLeaf(id, type) {
  116. const ins = this.jsPlumb;
  117. const elem = document.getElementById(id);
  118. if (type === 'source') {
  119. ins.makeSource(elem, {
  120. anchor: [1, 0.5, 0, 0], // 左 上 右 下
  121. allowLoopback: false, //允许回连
  122. maxConnections: 1 //最大连接数(-1表示不限制)
  123. })
  124. } else {
  125. ins.makeTarget(elem, {
  126. anchor: [0, 0.5, 0, 0],
  127. allowLoopback: false,
  128. maxConnections: 1
  129. })
  130. }
  131. },
  132. setjsPlumb(sourceFlag, targetFlag){
  133. const source = document.getElementsByName('source')
  134. const target = document.getElementsByName('target')
  135. this.jsPlumb.setSourceEnabled(source, sourceFlag)
  136. this.jsPlumb.setTargetEnabled(target, targetFlag)
  137. this.jsPlumb.setDraggable(source, false) // 是否支持拖拽
  138. this.jsPlumb.setDraggable(target, false) // 是否支持拖拽
  139. },
  140. clearAllNode(){
  141. this.jsPlumb.deleteEveryConnection()
  142. },
  143. initConnect(){
  144. const jsplumbConnectOptions ={
  145. isSource: true,
  146. isTarget: true,
  147. // 动态锚点、提供了4个方向 Continuous、AutoDefault
  148. anchor: "Continuous",
  149. }
  150. this.jsPlumb.deleteEveryConnection()
  151. for (var i = 0; i < this.fieldRelas.length; i++) {
  152. let line = this.fieldRelas[i]
  153. this.jsPlumb.connect({
  154. source: 'll_'+line.sls,
  155. target: 'rr_'+line.table_field,
  156. }, jsplumbConnectOptions);
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped>
  163. .left1_ul{
  164. list-style: none;
  165. }
  166. #container{
  167. width: 500px;
  168. min-height: 200px;
  169. padding: 20px;
  170. position: relative; /*一定加上这句,否则连线位置发生错乱*/
  171. }
  172. .left1{
  173. float: left;
  174. width: 150px;
  175. }
  176. .right1{
  177. float: right;
  178. width: 150px;
  179. }
  180. .left1 li,.right1 li{
  181. width: 100%;
  182. border-radius: 4px;
  183. border: 1px solid #1e90ff;
  184. background: #70a1ff;
  185. margin-bottom: 20px;
  186. padding: 8px 5px;
  187. text-align: center;
  188. color: white;
  189. font-weight: bold;
  190. }
  191. </style>

在父组件中引入使用

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/117162
推荐阅读
相关标签
  

闽ICP备14008679号