how to append option into select combo box in d3

前端 未结 1 1219
余生分开走
余生分开走 2021-01-22 13:18

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\", \"I         


        
相关标签:
1条回答
  • 2021-01-22 13:49

    You can do it like the following:

    1. Append the select element

       var dropDown = d3.select("body").append("select")
          .attr("name", "name-list");
      
    2. Append options to your select element based on data

      var options = dropDown.selectAll("option")
       .data(data)
       .enter()
       .append("option");
      
    3. Set the text and value for your options

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

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

    0 讨论(0)
提交回复
热议问题