简体   繁体   English

在 dom-repeat 模板内的纸按钮中执行事件

[英]Perform event in the paper-button inside a dom-repeat template

I have the following element:我有以下元素:

<dom-module id="my-blogentry">
<template>
    <style>
        :host {
            display: block;
        }
        .div-general{
      </style>
    <div class="div-general">
        <template is="dom-repeat" items="{{entries}}">
            <paper-material elevation="1" animated="true" class="paper-material-presentation">
                <div class="div-date-left">
                    <span>{{item.date}}</span>
                </div>
                <div>
                    <iron-image src$="../../images/{{item.image}}" style="width:128px; height:128px;" sizing="cover"></iron-image>
                    {{item.resume}} 
                    <paper-button id="bt_readmore" on-tap="toggle">Read More</paper-button>
                    <iron-collapse id="collapse">
                      <div>{{item.content}}</div>
                    </iron-collapse>
                </div>
            </paper-material>
        </template>
    </div>
</template>
<script>
    Polymer({
        is: 'my-blogentry',

        ready: function() {
            this.entries = [
                {id: '1', title: ‘xxx.', date: 'Tuesday, March 8, 2016', image:'im-blogdefault.png', resume:’xxx’,content:’xxxx’}
            ];
          },

        toggle: function () {
            this.$$('#collapse').toggle();
        }
    });

</script>

If you could check on the element code I have an iron-collapse element inside the <template dom-repeat> and a button.如果你可以检查元素代码,我在<template dom-repeat>和一个按钮中有一个iron-collapse元素。 My proposal is the user could tap on the button and toggle the button to open the collapse element.我的建议是用户可以点击按钮并切换按钮以打开折叠元素。

The problem is that the code that I have work fine with open the first item, but when I have more items on the list and I press the second item button the first and second collapse is open too.问题是我的代码可以很好地打开第一个项目,但是当我在列表中有更多项目并且我按下第二个项目按钮时,第一个和第二个折叠也会打开。

I was searching other posts here in the forum and I found this:我在论坛里搜索其他帖子,我发现了这个:
Documentation This example modify the items in the model but no the DOM that it is what I need (toggle my iron-collapse element). 文档这个例子修改了模型中的项目,但没有修改我需要的 DOM(切换我的铁折叠元素)。

I'll appreciate any help on that.我将不胜感激。

This is because in your code all <iron-collapse> get the same id="collapse" .这是因为在您的代码中,所有<iron-collapse>获得相同的id="collapse"

You can either add some value from entries to the <paper-button> and the <iron-collapse> or try to get the DomRepeatModel from the buttons click event to find the right collapse.您可以从entries<paper-button><iron-collapse>添加一些值,或者尝试从按钮单击事件中获取DomRepeatModel以找到正确的折叠。

Example code using an unique id property from entries (I just assumed it has one):使用entries唯一id属性的示例代码(我只是假设它有一个):

<paper-button ident$="{{entries.id}}" on-tap="toggle">Read More</paper-button>
<iron-collapse ident$="{{entried.is}}">
    toggle: function (event) {
        this.$$('[ident="' + event.target.getAttribute('ident') + '"]').toggle();
    }

Here is the solution, I assigned and the id to the iron-collapse identifier and the key is using the symbol $ in the id attribute:这是解决方案,我将 id 分配给了 Iron-collapse 标识符,关键是在 id 属性中使用了符号$

<template is="dom-repeat" items="{{entries}}">
            <paper-material elevation="1" animated="true" class="paper-material-presentation">
                <div class="div-date-left">
                    <span>{{item.date}}</span>
                </div>
                <div class="div-entry-resume">
                    <iron-image src$="../../images/{{item.image}}" style="width:128px; height:128px;" sizing="cover"></iron-image>
                    {{item.resume}} 
                    <iron-collapse id$="collapse{{item.id}}">
                      <div>{{item.content}}</div>
                    </iron-collapse>
                    <div class="div-entry-bottom">
                        <paper-button id$="bt_readmore" on-tap="toggle">Read More</paper-button>
                    </div>
                </div>
            </paper-material>
        </template>

and the polymer function:和聚合物功能:

toggle: function (event, detail, sender) {
            var id = event.model.item.id;
            var selector = '#collapse' + id;
            this.$$(selector).toggle();
        }

Thanks to @Günter Zöchbauer for your help on this.感谢 @Günter Zöchbauer 在这方面的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM