简体   繁体   English

Google Dart向DropDown添加元素

[英]Google Dart Add Elements to DropDown

I'm attempting to dynamically add elements to an HTML dropdown via Google Dart. 我正在尝试通过Google Dart向HTML下拉菜单动态添加元素。 I'm not having much luck adding the elements the standard way by adding children with a map or string from an original JSON feed. 我通过标准方式通过添加带有原始JSON提要中的映射或字符串的子元素来添加元素的运气并不好。 Maybe there's another way? 也许还有另一种方法? Ignore the selectedIndex because that's for debugging purposes. 忽略selectedIndex,因为这是出于调试目的。 See the HTML and Dart code below and please let me know if you need more information. 请参阅下面的HTML和Dart代码,如果您需要更多信息,请告诉我。

<select id="asset" class="titilium" selectedIndex="{{currentIndex}}" value="{{dropDownValue}}" on-change="{{changedHandler}}" required>
                <option selected disabled>Select an existing set of query results:</option>
                <option value="Recent Choices">Recently Choices</option>
</select>

Dart attempt(s): 飞镖尝试:

void loadDropDown()
  {
    dropDownURL = "";
    print(dropDownURL);

    var request = HttpRequest.getString(dropDownURL).then(onDropDownLoaded);
        request.catchError(handleFailure);
  }

 void onDropDownLoaded(String response)
 {
   var jsonString = response;
   var dropDownValue = shroot.querySelector("#asset");
   String displayName = "";

   Map jsonObject = JSON.decode(jsonString) as Map;
       dropDownList = jsonObject["serviceResponseValue"] as List<Map>;

   LinkedHashMap<String, Map> dataMap = new LinkedHashMap<String, Map>();
       for(var d in dropDownList)
       {

         HashMap rowMap = new HashMap();

//         String domainId = d["targetAsset"]["id"];
//         dataMap[domainId] = rowMap;
//         rowMap["id"] = domainId;

         //rowMap["displayName"] = d["filterInputParameters"]["displayName"];

         //dropDownValue.children.add(rowMap["displayName"] = d["filterInputParameters"]["displayName"]);
         displayName = rowMap["displayName"] = d["displayName"];
         //dropDownValue.children.add(rowMap["displayName"] = d["displayName"]);

         print(d);
         print(displayName);
       }
 }

The children of a SelectElement are OptionElements. SelectElement的子级是OptionElements。 Therefore you need to give in OptionElements. 因此,您需要提供OptionElements。

void onDropDownLoaded(String response)
{
  SelectElement dropDown = querySelector("#asset");

  Map jsonObject = JSON.decode(response) as Map;
  List<Map> dropDownList = jsonObject["serviceResponseValue"] as List<Map>;

  for(Map d in dropDownList)
  {
    dropDown.children.add(new OptionElement(data: d['displayName'], value: d['id']));
  }
}

The code, takes every object out of the parsed JSON objects "serviceResponseValue" and creates a new OptionElement where the data that is shown to the user is the text behind the key "displayName", which in your example turns out to be Domains registered during the last 36 hours , and for the value, which you use to identify an option from the others, I have chosen the "id" text, which will give you in your html an option that will look like: 该代码将每个对象从已解析的JSON对象“ serviceResponseValue”中取出,并创建一个新的OptionElement,其中显示给用户的数据是键“ displayName”后面的文本,在您的示例中,结果是Domains registered during the last 36 hours ,对于该值(用于从其他选项中标识一个选项),我选择了“ id”文本,该文本将在您的html中为您提供一个类似于以下内容的选项:

<option value="8a49861a47b12b7c0147b12b7fe60000">Domains registered during the last 36 hours</option>

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

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