How can I dynamically add items into paper-dropdown-menu?

不问归期 提交于 2019-12-14 03:49:15

问题


I tried adding it with like dropdownMenu.appendChild(menuItem) but as I expected this doesn't work. I couldn't find information about this on Polymer's guides nor other similar questions on here.

Is that possible? If so, how?

paper-dropdown-menu: https://elements.polymer-project.org/elements/paper-dropdown-menu


回答1:


In Polymer, recommended way of manipulating the DOM is by manipulating the data:

  • put the list of menu items in array: var items_array = [....];

-create the menu as:

<paper-dropdown-menu label="Your favourite pastry">
  <paper-listbox class="dropdown-content">
    <template is="dom-repeat" items="{{items_array}}">
      <paper-item>{{item}}</paper-item>
    </template>
  </paper-listbox>
</paper-dropdown-menu>
  • adding and removing elements in items_array will affect the menu immediately.



回答2:


Found out the proper way from their docs:

Should select the Polymer element with: Polymer.dom(parent).querySelector(selector)

And append with: Polymer.dom(parent).appendChild(node)



来源:https://stackoverflow.com/questions/38919186/how-can-i-dynamically-add-items-into-paper-dropdown-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!