聚合物1.0:在纸张菜单元素内使用模板dom-repeat来显示选择时的铁页 [英] Polymer 1.0: Using template dom-repeat inside paper-menu elements to display iron-pages on selection

查看:107
本文介绍了聚合物1.0:在纸张菜单元素内使用模板dom-repeat来显示选择时的铁页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Polymer 1.0,我创建了一个抽屉面板布局.在抽屉里,我有一个菜单,该菜单使用纸质菜单,并且纸质项目绑定到铁页上.我从带有Core-Pages的内容切换器中获取了此示例-Polycasts#09 并将其转换为使用聚合物1.0元素.在下面的代码中,您可以看到我的注释部分,其中纸质项目是硬编码的.效果很好.

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.

下一步是尝试使用<template is="dom-repeat">元素遍历任意数组菜单项来动态构建菜单.菜单呈现正确(我可以看到绑定到阵列的所有菜单项),但是我无法单击这些菜单项,也不会显示任何铁页.似乎用于attr-for-selected的data-category属性在<template is="dom-repeat">内部不起作用.

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

我可以通过什么方式使它工作? 删除attr-for-selected属性并使用索引工作切换页面,但是在我的情况下,依靠数组的索引不是一个选择.

In what ways can I get this to work? 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.

我的index.html如下:

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>

推荐答案

尝试从文档中调用<paper-item data-category$="{{item.itemNum}}" label$="{{item.itemNum}}">,它将调用paper-item.setAttribute('data-category',itemNum)而不是paper-item.data -category = itemNum.

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

这篇关于聚合物1.0:在纸张菜单元素内使用模板dom-repeat来显示选择时的铁页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆