首页 > 解决方案 > Mxgraph 如何以编程方式在形状内添加文本元素?

问题描述

我想在任何形状内添加一个文本元素。我可以使用以下代码以编程方式添加占位符,但无法移动仅能够更改样式的文本。我想标签应该移动到任何位置。

var applyBtn = mxUtils.button(mxResources.get('apply'), function()
    {
        try
        {
            ui.hideDialog.apply(ui, arguments);

            // Clones and updates the value
            value = value.cloneNode(true);
            var removeLabel = false;
            for (var i = 0; i < names.length; i++)
            {
                if (texts[i] == null)
                {
                    value.removeAttribute(names[i]);
                }
                else
                {
                    value.setAttribute(names[i], texts[i].value);
                    //console.log(names[i]+"==="+texts[i].value);
                    removeLabel = removeLabel || (names[i] == 'placeholder' &&
                        value.getAttribute('placeholders') == '1');
                }
            }

            // Removes label if placeholder is assigned
            if (removeLabel)
            {
                value.removeAttribute('label');
            }
            // set booth id here
            value.setAttribute('label',value.getAttribute('booth_number'));
            graph.getModel().setValue(cell, value);
        }
        catch (e)
        {
            mxUtils.alert(e);
        }
    });

输出

在此处输入图像描述

需要

所以我想添加文本元素,以便用户可以轻松移动位置

提前致谢!

标签: javascriptjquerymxgraph

解决方案


看起来设置graph.vertexLabelsMovable = true;是您正在寻找的。

基于这个例子(代码在这里


推荐阅读