Polymer 1.0: Using template dom-repeat inside paper-menu elements to display iron-pages on selection

随声附和 提交于 2019-12-07 03:39:00

问题


Using Polymer 1.0, I have created a paper-drawer-panel layout. In the drawer I have a menu using paper-menu with paper-items which are bound to the iron-pages. I took this example from Content Switcheroo with Core-Pages -- Polycasts #09 and converted it to use the Polymer 1.0 elements. In the code below you can see my commented section in which the paper-items are hard-coded. This works fine.

My next step was to try and build my menu dynamically by using the <template is="dom-repeat"> element to iterate over an arbitrary array of menu items. The menu is rendered correctly (I can see all the menu items that are bound to the array), but I cannot click on the items and no iron-pages are displayed. It seems that the data-category attribute which is used for attr-for-selected is not working inside <template is="dom-repeat">.

In what ways can I get this to work? Edit: Removing the attr-for-selected attributes and switching the iron-pages using the index work, but relying on the index of the array is not an option in my situation.

My index.html is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>My Test</title>
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/polymer/polymer.html">
<link rel="import" href="bower_components/iron-icons/iron-icons.html">
<link rel="import" href="bower_components/iron-pages/iron-pages.html">
<link rel="import" href="bower_components/paper-drawer-panel/paper-drawer-panel.html">
<link rel="import" href="bower_components/paper-toolbar/paper-toolbar.html">
<link rel="import" href="bower_components/paper-header-panel/paper-header-panel.html">
<link rel="import" href="bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="bower_components/paper-item/paper-item.html">
<link rel="import" href="bower_components/paper-styles/paper-styles.html">
<link rel="import" href="bower_components/paper-menu/paper-menu.html">
<style>
</style>
</head>
<body>
<my-app></my-app>
<dom-module id="my-app">
<style>
</style>
<template>
<paper-drawer-panel>
  <paper-header-panel drawer class="fit">
    <paper-toolbar>
      <div>Drawer</div>
    </paper-toolbar>
    <paper-menu class="content fit" selected="{{page}}" attr-for-selected="data-category">
      <!-- This works -->
<!--
        <paper-item data-category="item1" label="item1">
            <span>John Smith</span>
        </paper-item>
        <paper-item data-category="item2" label="item2">
            <span>Jane Doe</span>
        </paper-item>
-->

      <!-- This does not work -->
      <template is="dom-repeat" items="{{names}}">
        <paper-item data-category="{{item.itemNum}}" label="{{item.itemNum}}">
          <span>{{item.first}}</span><span>{{item.last}}</span><span>{{item.itemNum}}</span>
        </paper-item>
      </template>

    </paper-menu>
  </paper-header-panel>
  <paper-header-panel main class="fit">
    <paper-toolbar>
      <paper-icon-button icon="menu" paper-drawer-toggle></paper-icon-button>
      <div class="flex">Main Content</div>
    </paper-toolbar>
    <iron-pages selected="{{page}}" attr-for-selected="data-category">
      <section data-category="item1">
        <h1>Item 1</h1>
        <div>Item 1 content...</div>
      </section>
      <section data-category="item2">
        <h1>Item 2</h1>
        <div>Item 2 content...</div>
      </section>
    </iron-pages>
  </paper-header-panel>
</paper-drawer-panel>
</template>
<script>
Polymer({
  is: "my-app",
  ready: function() {
    this.names = [{itemNum: "item1", first: "John", last: "Smith"}, {itemNum: "item2", first: "Jane", last: "Doe"}];
  }
});
</script>
</dom-module>
</body>
</html>

回答1:


Try <paper-item data-category$="{{item.itemNum}}" label$="{{item.itemNum}}">, from the docs, that will call paper-item.setAttribute('data-category', itemNum) instead of paper-item.data-category = itemNum.

https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#attribute-binding



来源:https://stackoverflow.com/questions/30561725/polymer-1-0-using-template-dom-repeat-inside-paper-menu-elements-to-display-iro

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