javascript - 单元格如何监听 mxGraph 中的移动或调整大小事件?
问题描述
我正在 mxGraph 中移动/调整单元格大小。我想我应该使用一些 mxEvent,例如MOVE_CELLS, CELLS_MOVED, RESIZE_CELLS, CELLS_RESIZED, CHANGE, RESIZE
,... 但它不适用于我的代码。细胞不听那些事件。
我正在使用这段代码,它与LABEL_CHANGED
mxEvent 配合得很好(当单元格值发生任何变化时会触发此事件)。单元格侦听LABEL_CHANGED
事件,其值将显示到控制台。
graph.addListener(mxEvent.LABEL_CHANGED, function (sender, evt) {
var cell = evt.getProperty("cell");
console.log(cell.value);
});
<html>
<head>
<title>Hello, World! example for mxGraph</title>
<!-- Sets the basepath for the library if not in same directory -->
<script type="text/javascript">
mxBasePath = '../src';
</script>
<!-- Loads and initializes the library -->
<script type="text/javascript" src="../src/js/mxClient.js"></script>
<!-- Example code -->
<script type="text/javascript">
function main(container)
{
// Checks if the browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
// Disables the built-in context menu
mxEvent.disableContextMenu(container);
// Creates the graph inside the given container
var graph = new mxGraph(container);
// Enables rubberband selection
new mxRubberband(graph);
var parent = graph.getDefaultParent();
//My codes go here
graph.addListener(mxEvent.LABEL_CHANGED, function (sender, evt) {
var cell = evt.getProperty("cell");
console.log(cell.value);
});
graph.getModel().beginUpdate();
try
{
var v1 = graph.insertVertex(parent, null, 'Hello,', 20, 20, 80, 30);
var v2 = graph.insertVertex(parent, null, 'World!', 200, 150, 80, 30);
var e1 = graph.insertEdge(parent, null, '', v1, v2);
}
finally
{
graph.getModel().endUpdate();
}
}
};
</script>
</head>
<body onload="main(document.getElementById('graphContainer'))">
<div id="graphContainer"
style="position:relative;overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;">
</div>
</body>
</html>
我的问题是如何在移动或调整单元格大小时向控制台显示单元格值?(注意在这里检查 mxEvent API:https ://jgraph.github.io/mxgraph/docs/js-api/files/util/mxEvent-js.html#mxEvent )
解决方案
我解决了我的问题。CELLS_MOVED, CELLS RESIZED
与getProperties
功能一起工作,而不是getProperty
一个。
graph.addListener(mxEvent.CELLS_MOVED, function (sender, evt) {
var cell = evt.getProperties("cell");
console.log(cell.cells[0].geometry.x);//Show the new x_geometry
});
推荐阅读
- go - 尽管设置了 GOPATH,但 echo $GOPATH 什么也不返回
- php - 无法在 Majove PHP 7.1.19 上找到/安装 mcrypy 无法
- methods - 函数文档中的类似参数的数组
- r - 找到多边形的质心
- python - Pandas 过滤多个“包含”而不是一个单元格,而是整个列
- matlab - 在 MATLAB 中使用 fgetl
- python - AWS Elastic Beanstalk - 会话不可用错误
- node.js - EC2 runInstances 函数不接受 SourceDestCheck 选项
- javascript - 当我删除一个矩形时,我怎样才能删除它的字母
- security - 将文件描述符“泄露”给客户端的安全隐患