javascript - mxGraph:来自外部按钮的单击事件

标签 javascript mxgraph

假设我有以下 html 文档:

  <div id="container" class="container">
       // here comes the mxGraph
  </div>
  <div class="button-container">
    <button id="myButton" type="button" name="button" onclick="myFunction()">Save</button>
  </div>
</body>

有没有办法在mxGraph中捕获myButton的点击事件?

最佳答案

是的,您可以捕获 mxgraph-div 之外的事件。

在 jquery $(document).ready 函数中执行以下操作:

  1. 声明该图有一个全局变量。
  2. 创建一个函数来加载 mx-graph div 内的图表
  3. 创建外部按钮点击事件的函数。
  4. 将函数点击事件绑定(bind)到第 3 步创建的函数
  5. 调用第 2 步函数在 div 内加载 mx-graph。
  6. 在第 3 步功能中,您可以访问图形对象以触发 xml 保存

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>


<ol class="breadcrumb">
<button id='genxml' class="btn  btn-outline-success btn-sm m-2" onclick="this.blur();">
				<span class="fas fa-fw fa-folder"></span> Download XML
</button>
</ol>
<div class="container">
		<div class="row">
      <div id="graphContainer" class="col-md-12"   style="float:left;position:absolute;overflow:scroll;width:80%;height:80vh;cursor:default;outline:1px solid red">
    </div>
    </div>
</div>

<script type="text/javascript">
		$(document).ready(function(){
     var graph;
     
     function downloadXML(){
				// access the Graph object here to save the mxUtils
         // Block of Code to get the mxGraph mxUtil in the function   
       // var node = encoder.encode(graph.getModel());
      //  mxUtils.popup(mxUtils.getPrettyXml(node), true);
     }
     
     function main(container){
        // Block of Code to load a mxGraph in the Container        
         /* graph = new mxGraph(container);
          graph.getModel().beginUpdate();
            var v1 = graph.insertVertex(parent, 'r', req,req_grp_geo.x+30, req_grp_geo.y+40, 160, 40,'part_group');
					var v2 = graph.insertVertex(parent, 'f', func,func_grp_geo.x+30, func_grp_geo.y+40, 160,40,'functional_group' );
          graph.getModel().endUpdate();*/
         
      }
      
      $('#genxml').on('click', function(){
        	downloadXML(); 	
       });
       
       main(document.getElementById('graphContainer'))
		
    
    });
    </script>

关于javascript - mxGraph:来自外部按钮的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026749/

相关文章:

javascript - 意外行为 : function vs functional component and animations

javascript - 在新的 Promise 中决心、拒绝和没有之间有什么区别?

Javascript View HTML 切换

javascript - 如何将svg文件转换为mxgraph的xml?

javascript - 如何以编程方式更改 mxCell 值?

javascript - 如何使用 Mxgraph 对 HTML 标签执行就地编辑?

javascript - mxGraph - 如果 mxCell 超过默认大小,如何自动将其调整为内容宽度

functional-programming - 全局命名空间中的 JavaScript 库函数 - 缺点?

javascript - 查找页面上的元素数量 (JQuery)

javascript - 如何在 mxGraph 中渲染折线元素?