微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在D3中添加html表单?

我正在尝试创建一个输入表单,该表单在单击节点时出现在d3定向力图中的节点上.用户输入的信息将更新节点的属性 – 例如名称将更改标题,角色是节点的颜色等.我已设法使用一个输入来描述具有以下功能一个属性

function nodeForm(d) {
    var p = this.parentNode;
    var el = d3.select(this);
    var p_el = d3.select(p);

    var form = p_el.append("foreignObject");

    var input = form
    .attr("width",300)
    .attr("height",100)
    .attr("class","input")
    .append("xhtml:form")
    .html(function(d) {return "<strong>Name:</strong>"}) 
    .append("input")
    .attr("value",function(d) {this.focus(); return d.name})
    .attr("style","width: 200px;")
    .on("keypress",function() {

           if (!d3.event)
            d3.event = window.event;

            //Prevents total update
            var e = d3.event;
            if (e.keyCode == 13){
            if (typeof(e.cancelBubble) !== 'undefined') // IE
                e.cancelBubble = true;
            if (e.stopPropagation)
                e.stopPropagation();
                e.preventDefault();

            var text = input.node().value;

            d.name = text;
            el.text(function(d) { return d.name; });

            }
        })
}

现在我在添加其他输入时遇到了麻烦.我希望可以在附加的html函数添加一个输入框(如下所示),但它不能识别这些值,输入框 – 虽然它确实出现 – 不允许输入任何内容.

.html(function(d) {return "<strong>Name:</strong> <input type = 'text' value = "d.name"> <br> <strong>Role:</strong> <input type = 'text' value = "d.role"> <br> <strong>Name:</strong> <input type = 'text' value = "d.tribe">"})

我对编程非常陌生,希望有人能指出我正确的方向.

我仍然无法使用弹出式输入框.使用各种方法(包括将它附加到节点组,使用.html附加它,调用html文件中的表单,包括它在mouseup函数中,使用工具提示等)以及我设法做的所有方法是得到相同的结果 – 我可以看到输入框但我无法编辑它们.我认为foreignObject正在工作,因为我可以在consol中看到它,但我似乎无法使其可编辑.我必须做一些根本错误的事情,我希望有人能指出我正确的方向.这是迄今为止完整代码的小提琴 – https://jsfiddle.net/VGerrard/91e7d5g9/2/

解决方法

你忘了在字符串和值之间添加,这可能会得到你想要做的事情

.html(function(d) {
    return "<strong>Name:</strong> <input type = 'text'
     value = " + d.name + "> <br> <strong>Role:</strong> <input type =
     'text' value = " + d.role + "> <br> <strong>Name:</strong> <input type
     = 'text' value = " + d.tribe + ">"
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐