简体   繁体   English

自定义属性和选择/下拉菜单

[英]Custom properties and select/drop down menus

I'm customising mxGraph and am looking into custom properties.我正在自定义 mxGraph 并正在研究自定义属性。 I'm using the mxDraw template.我正在使用 mxDraw 模板。 Creating custom properties for a shape is easy enough such as the below (modifying diagrameditor.xml):为形状创建自定义属性非常简单,如下所示(修改 diagrameditor.xml):

        <add as="rectangle">
            <Rect label="Rectangle" href="" new_property="hi there">
                <mxCell vertex="1"> 
                    <mxGeometry as="geometry" width="80" height="40"/>
                </mxCell>
            </Rect>
        </add>

When using mxDraw these properties are immediately visible and all is well, except I want to be able to choose a value for this property using a drop-down menu.使用 mxDraw 时,这些属性立即可见并且一切正常,除了我希望能够使用下拉菜单为该属性选择一个值。

I'm having trouble finding an example of this elsewhere on the internet, and I'm not really sure what my options are here, or where to start.我在互联网上的其他地方找不到这样的例子,我不确定我的选择是什么,或者从哪里开始。

In my mind there are a few options, but am hoping I can get some guidance on where to even start here...在我看来,有几个选择,但我希望我能得到一些关于从哪里开始的指导......

  • Disregard custom properties like the above and instead use a separate DIV, an event listener for selected shape and use HTML forms to capture properties/values忽略上述自定义属性,而是使用单独的 DIV、选定形状的事件侦听器并使用 HTML forms 来捕获属性/值
  • Modify diagrameditor.xml like the above code snippet and somehow modify existing javascript library像上面的代码片段一样修改 diagrameditor.xml 并以某种方式修改现有的 javascript 库
  • Some sort of hybrid of the above 2?上述2的某种混合体?

Eventually the resulting graph XML/data will be POSTed to the web server for storage, which can then merge/correlate data accordingly.最终生成的图 XML/数据将被 POST 到 web 服务器进行存储,然后可以相应地合并/关联数据。

I know SO has some experienced mxGraph programmers so hoping to get some thoughts from those more experienced than I, so I can go and research.我知道 SO 有一些经验丰富的 mxGraph 程序员所以希望从那些比我更有经验的人那里得到一些想法,所以我可以 go 和研究。 I can't seem to find what I'm looking for so far (in terms of guidance, examples or others attempting a similar thing).到目前为止,我似乎找不到我正在寻找的东西(就指导、示例或其他尝试类似事情的人而言)。

Any help appreciated, thanks.任何帮助表示赞赏,谢谢。

I kept researching and found a solution that suits me.我不断研究并找到了适合我的解决方案。

  1. Update diagrameditor.xml and create custom data fields.更新 diagrameditor.xml 并创建自定义数据字段。 Example shown in my original post我的原始帖子中显示的示例
  2. Create a new DIV on the page在页面上创建一个新的 DIV
  3. Setup a listener for cell selection, add some HTML forms then use mxGraph calls to update the property values为单元格选择设置一个监听器,添加一些 HTML forms 然后使用 mxGraph 调用来更新属性值
  4. To be done: disable the right-click menu so it cannot be updated manually using the text field, to ensure inputs are valid待完成:禁用右键菜单,使其无法使用文本字段手动更新,以确保输入有效

Below is the starting code for the HTML page.下面是 HTML 页面的起始代码。

        <div id="customdata" style="position:relative;padding-left:100px;padding-top:10px;">
            <script>
            var selectedCell;

            mxGraph.prototype.addListener(mxEvent.CLICK, function(sender, event){

                selectedCell = event.getProperty('cell');

                //update DIV content example. Show cell ID and a drop-down to update a custom data field called 'new_property' (as created in diagrameditor.xml)
                var newhtml = "Cell ID is: ";
                newhtml += selectedCell.getId();

                newhtml += `<select><option id="selectmenu" value="one">one</option><option value="two">two</option></select>`
                newhtml += `<button onclick="updateCustomData();">save</button>`

                document.getElementById("customdata").innerHTML = newhtml;

                //Can also iterate through a multiple-selection and do more things, if need be
                /*
                for (var i = 0; i < sender.getSelectionCount(); i++) {
                    //example, alert showing the label of the cell at this array index i
                    alert(sender.getSelectionCells()[i].getAttribute('label', ''));
                }
                */
            });

            function updateCustomData() {
                selectedCell.setAttribute("new_property", document.getElementById("selectmenu").value);
            }
            </script>
            div content
        </div>

I hope this question can be left here to serve as potential help for others.我希望这个问题可以留在这里,作为对其他人的潜在帮助。 I'm still a new SO user, great community.我仍然是一个新的 SO 用户,很棒的社区。 Hope this can be my little contribution.希望这可以成为我的一点贡献。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM