Search code examples
dartdart-polymerpaper-elements

Retrieving the selected item in a paper-element dropdown


I have the following code following the examples at https://github.com/dart-lang/polymer-core-and-paper-examples/blob/master/web/paper_dropdown.html and https://github.com/dart-lang/polymer-core-and-paper-examples/blob/master/web/paper_dropdown.dart

EDITED

.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;

}

How do I retrieve the selected element in the dropdown (that displays normally) using dart code?


Solution

  • update

    I think this is the easiest way

    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);
    }
    

    old

    There is no selected in paper-dropdown. Wrap a core-menu within the paper-dropdown which provides selected.

    see
    - https://www.polymer-project.org/0.5/docs/elements/core-menu.html and the example at https://www.polymer-project.org/0.5/docs/elements/paper-dropdown-menu.html