【问题标题】:Retrieving the selected item in a paper-element dropdown检索纸张元素下拉列表中的选定项目
【发布时间】:2015-04-26 03:14:14
【问题描述】:

https://github.com/dart-lang/polymer-core-and-paper-examples/blob/master/web/paper_dropdown.htmlhttps://github.com/dart-lang/polymer-core-and-paper-examples/blob/master/web/paper_dropdown.dart 的示例之后,我有以下代码

已编辑

.html

<paper-dropdown-menu 
  label='Click to select..' 
  on-core-select='{{onCoreSelectCountryHandler}}'>
  <paper-dropdown class='dropdown'>
    <core-menu id='country' class='menu'>
      <template repeat='{{country in countries}}'>
        <paper-item>{{country.name}}</paper-item>
      </template>
    </core-menu>
  </paper-dropdown>
</paper-dropdown-menu>

.dart

final List<Country> countries = [
  const Country('Afghanistan', 'AF'),
  const Country('Åland Islands', 'AX')];

class Country {
  final String name;
  final String code;
  const Country(this.name, this.code);
}

void onCoreSelectCountryHandler(dom.CustomEvent e, var detail) {
    var detail = new JsObject.fromBrowserObject(e)['detail'];

  if (detail['isSelected']) {
  // DOES NOT WORK - HOW DO I GET THE SELECTION ATTEMPTED BELOW
  // The detail should be related to the Country class but 
   // I can't seem to relate it so I could get the selection.
  var kuntry = (detail['item'] as PaperItem).text;

}

如何使用 dart 代码检索下拉列表中的选定元素(正常显示)?

【问题讨论】:

    标签: dart dart-polymer paper-elements


    【解决方案1】:

    更新

    我认为这是最简单的方法

    void onCoreSelectCountryHandler(dom.CustomEvent e, var detail) {
      print(countries[$['country'].selected].name);
      // or if you really need to access the `<paper-item>` element
      print(detail['item'].text);
    }
    

    paper-dropdown 中没有 selected。将core-menu 包裹在提供selectedpaper-dropdown 中。


    - https://www.polymer-project.org/0.5/docs/elements/core-menu.htmlhttps://www.polymer-project.org/0.5/docs/elements/paper-dropdown-menu.html 的示例

    【讨论】:

    • 没问题,只是为了保持 Dart 区域保持良好状态:)
    【解决方案2】:

    简单地使国家列表可观察

    final List<Country> cuntries = toObservable[
      const Country('Afghanistan', 'AF'),
      const Country('Åland Islands', 'AX')}]
    

    选择被检索。

    我的疏忽。

    【讨论】:

    • 这就是我必须对代码进行的所有更改才能使其正常工作。你的答案 print(detail['item'].text);证实了这一点。如果我删除 toObservable 它仍然不会打印该项目。毕竟,我问这个问题是因为我的代码不起作用。
    • 我明白了。我无法想象这有什么关系。也许你在你的应用程序中以不同的方式使用它。例如,当您从服务器加载国家/地区或在加载应用程序后以任何其他方式修改列表内容时,您肯定需要toObservable
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多