如何将选项附加到d3中的选择组合框中 [英] how to append option into select combo box in d3

查看:54
本文介绍了如何将选项附加到d3中的选择组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想将选项添加到选择组合框中,

I want to add the option into select combo box for example I have data like

[{ "NAME": "JONE", "ID": {"id1":123,"id2":124}}, { "NAME": "ANGEL", "ID": {"id1":125,"id2":127}}]` 

我想在选项中附加名称,到目前为止,我已经这样做了:

I want to append into options the "Name", I have done this so far:

dataset=d3.json("namees.json", function(data) {
    select = d3.select('body')
    .append('select')
    .attr('class','style');
    options = select
    .selectAll('option')


    .append('option')
            .text(data.forEach(function (d) {return d.Name}))



});

但是它不显示选择选项中的值

But it doesn't show the values in select option

推荐答案

您可以按照以下步骤进行操作:

You can do it like the following:


  1. 附加 select 元素

 var dropDown = d3.select("body").append("select")
    .attr("name", "name-list");


  • 个选项追加到您的 select 基于数据的元素

  • Append options to your select element based on data

    var options = dropDown.selectAll("option")
     .data(data)
     .enter()
     .append("option");
    


  • 设置文本 value 为您的选项

    options.text(function(d) {
    return d.NAME;
     })
       .attr("value", function(d) {
    return d.NAME;
    });
    


  • JSFiddle- https://jsfiddle.net/x4a8ejk6/

    JSFiddle - https://jsfiddle.net/x4a8ejk6/

    这篇关于如何将选项附加到d3中的选择组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆