当前位置:   article > 正文

vue中使用jsplumb基本教程_jsplumb vue

jsplumb vue

jsplumb中文教程地址: https://wdd.js.org/jsplumb-chinese-tutorial/#/

需求效果展示:

说明:在展示普通流程图的基础上,实现以下功能:在流程图中展示当前所在节点,并体现出其流转过程。上图中当前节点为分公司咨询组,流转过程为:派单->省公司虚拟专家组->分公司咨询组

安装依赖包:npm i jsplumb

在main.js中将jsplumb挂在vue下便于页面获取

import jsPlumb from 'jsplumb'

Vue.prototype.$jsPlumb = jsPlumb.jsPlumb

主要代码:

  1. <div id="container">
  2. <div class="col1">
  3. <div id="processDefineDiv_node_2" name="cell">派单</div>
  4. </div>
  5. <div class="col2">
  6. <div v-for="item in list2" :key="item.nodeId" :id="item.nodeId" name="cell">{{ item.name }}</div>
  7. </div>
  8. <div class="col3">
  9. <div id="processDefineDiv_node_6" name="cell">完成</div>
  10. </div>
  11. </div>
  12. // 样式
  13. #container{
  14. position: relative;
  15. width: 100%;
  16. padding: 0 50px;
  17. }
  18. .col2,.col1{
  19. float: left;
  20. text-align: center;
  21. }
  22. .col3{
  23. float: right;
  24. text-align: center;
  25. }
  26. .col1{
  27. width: 180px;
  28. }
  29. .col2{
  30. width:200px;
  31. margin: 0 200px;
  32. }
  33. .col3{
  34. width: 180px;
  35. }
  36. #container>div>div{
  37. line-height: 40px;
  38. background: #496def;
  39. margin: 50px 0;
  40. }

节点中的id尽量设置为可以从后台返回的流转数据中获取的唯一的key值,可以较方便的控制当前所在节点的样式,以下为data中的相关数据

  1. jsPlumb: null, // 缓存实例化的jsplumb对象
  2. list2: [{ name: '省公司虚拟专家组', nodeId: 'processDefineDiv_node_3' }, { name: '分公司咨询组', nodeId: 'processDefineDiv_node_5' }, { name: '县咨询组', nodeId: 'processDefineDiv_node_4' }], // 节点参数
  3. connlist: [
  4. { sourceNodeId: 'processDefineDiv_node_2', targetNodeId: 'processDefineDiv_node_3' },
  5. { sourceNodeId: 'processDefineDiv_node_2', targetNodeId: 'processDefineDiv_node_5' },
  6. // { sourceNodeId: 'processDefineDiv_node_2', targetNodeId: 'processDefineDiv_node_4' },
  7. { sourceNodeId: 'processDefineDiv_node_3', targetNodeId: 'processDefineDiv_node_5' },
  8. { sourceNodeId: 'processDefineDiv_node_5', targetNodeId: 'processDefineDiv_node_3' },
  9. { sourceNodeId: 'processDefineDiv_node_3', targetNodeId: 'processDefineDiv_node_6' },
  10. { sourceNodeId: 'processDefineDiv_node_5', targetNodeId: 'processDefineDiv_node_4' },
  11. { sourceNodeId: 'processDefineDiv_node_4', targetNodeId: 'processDefineDiv_node_5' },
  12. { sourceNodeId: 'processDefineDiv_node_5', targetNodeId: 'processDefineDiv_node_6' },
  13. { sourceNodeId: 'processDefineDiv_node_4', targetNodeId: 'processDefineDiv_node_6' }
  14. ], // 指定需要连接的两节点

根据后台获取的节点流转数据重置节点状态

  1. //后台参考数据
  2. this.oncedata = [
  3. {proc_inst_task_code:'processDefineDiv_node_2',proc_inst_task_name:'派单'},
  4. {proc_inst_task_code:'processDefineDiv_node_3',proc_inst_task_name:'省公司虚拟专家组'},
  5. {proc_inst_task_code:'processDefineDiv_node_5',proc_inst_task_name:'分公司咨询组'}
  6. ]
  7. this.oncedata.forEach((item, index) => {
  8. const ele = document.getElementById(item.proc_inst_task_code)
  9. ele.style.backgroundColor = '#10af10' // 在流转数据中的节点都改为绿色背景
  10. if (index === this.oncedata.length - 1) {
  11. ele.style.backgroundColor = '#f11818' // 最后一个节点是最终状态改为红色背景
  12. }
  13. })

在页面创建时实例化流程图

  1. this.jsPlumb = this.$jsPlumb.getInstance({
  2. Container: 'container', // 选择器id
  3. EndpointStyle: { radius: 0.11, fill: '#fff' }, // 端点样式
  4. // PaintStyle: { stroke: '#00ff00', strokeWidth: 2 }, // 绘画样式,默认8px线宽 #456
  5. // HoverPaintStyle: { stroke: '#1E90FF' }, // 默认悬停样式 默认为null
  6. // EndpointHoverStyle: { fill: '#F00', radius: 6 }, // 端点悬停样式
  7. // ConnectionOverlays: [ // 此处可以设置所有箭头的样式,因为我们要改变连接线的样式,故单独配置
  8. // ['Arrow', { // 设置参数可以参考中文文档
  9. // location: 1,
  10. // length: 10,
  11. // paintStyle: {
  12. // stroke: '#496def',
  13. // fill: '#496def'
  14. // }
  15. // }]
  16. // ],
  17. Connector: ['Straight', { gap: 1 }], // 要使用的默认连接器的类型:直线,折线,曲线等
  18. DrapOptions: { cursor: 'crosshair', zIndex: 2000 }
  19. },)

页面挂载完成后,绘制连接线

  1. const ins = this.jsPlumb
  2. ins.getAllConnections()
  3. ins.batch(() => {
  4. this.initAll()
  5. this.connectionAll()
  6. })
  7. this.switchContainer(true, true, false)
  8. this.oncedata.forEach((item, index) => {
  9. if (index < this.oncedata.length - 1) {
  10. const connection = this.jsPlumb.connect({ // 对流程数据中对应的节点连接线重新绘制
  11. source: item.proc_inst_task_code,
  12. target: this.oncedata[++index].proc_inst_task_code,
  13. overlays: [ ['Arrow', { width: 12,
  14. length: 10,
  15. location: 1,
  16. paintStyle: {
  17. stroke: '#10af10',
  18. fill: '#10af10'
  19. } }
  20. ] ]
  21. })
  22. connection.setPaintStyle({ stroke: '#10af10', strokeWidth: 2 })
  23. }
  24. })

methods中使用的主要方法

  1. initAll () { // 初始化所有节点
  2. this.init('processDefineDiv_node_2')
  3. this.init('processDefineDiv_node_6')
  4. const rl2 = this.list2
  5. for (let i = 0; i < rl2.length; i++) {
  6. this.init(rl2[i].nodeId)
  7. }
  8. },
  9. // 初始化规则使其可以连线、拖拽
  10. init (id) {
  11. const ins = this.jsPlumb
  12. const elem = document.getElementById(id)
  13. ins.makeSource(elem, {
  14. anchor: ['Perimeter', { anchorCount: 200, shape: 'Rectangle' }],
  15. allowLoopback: false,
  16. maxConnections: 1
  17. })
  18. ins.makeTarget(elem, {
  19. anchor: ['Perimeter', { anchorCount: 200, shape: 'Rectangle' }],
  20. allowLoopback: false,
  21. maxConnections: 1
  22. })
  23. },
  24. connectionAll () { 创建连接线
  25. const ins = this.jsPlumb
  26. ins.ready(() => { // 入口
  27. for (let i = 0; i < this.connlist.length; i++) {
  28. const conn = this.connlist[i]
  29. const connection = ins.connect({
  30. source: conn.sourceNodeId,
  31. target: conn.targetNodeId,
  32. overlays: [ ['Arrow', { width: 12,
  33. length: 10,
  34. location: 1,
  35. paintStyle: {
  36. stroke: '#496def',
  37. fill: '#496def'
  38. } }
  39. ] ]
  40. })
  41. connection.setPaintStyle({ stroke: '#496def', strokeWidth: 2 })
  42. }
  43. })
  44. },
  45. switchContainer (target, source, draggable) {
  46. const elem = document.getElementsByName('cell')
  47. const ins = this.jsPlumb
  48. ins.setSourceEnabled(elem, source)
  49. ins.setTargetEnabled(elem, target)
  50. ins.setDraggable(elem, draggable) // 是否支持拖拽
  51. }

参考文档:https://zhuanlan.zhihu.com/p/43642654

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

闽ICP备14008679号