Polymer2.0-使用类的多个铁崩溃

时间:2017-07-30 02:21:13

标签: polymer-2.x polymer-2.0

我正在尝试使用class而不是id

实现多次铁崩溃

问题中提到的解决方案 - Polymer Multiple Iron-Collapse有效,但我有更多的铁崩溃,我不想为相同的功能创建重复的代码

我尝试使用类而不是id

选择相应元素的不同选项

codepen- https://codepen.io/nagasai/pen/YxyqmE

HTML:

<head>
  <base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-collapse/iron-collapse.html">
   <link rel="import" href="paper-input/paper-input.html">
</head>
<body>

  <x-foo attr="{{text}}"></x-foo>

  <dom-module id="x-foo">
    <template id="collapse1">


<button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
  <div>Enter text collapse1</div>
</iron-collapse>


<button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
  <div>Enter text collapse2</div>
</iron-collapse>


 <button on-click="toggle">toggle collapse</button>
<iron-collapse class="collapse">
  <div>Enter text collapse2</div>
</iron-collapse>

    </template>
  </dom-module>
</body>

JS:

  class XFoo extends Polymer.Element {
    static get is() { return 'x-foo'; }

    static get properties() {
      return {};

    }
       toggle() {
         console.log(this);
  this.$.querySelector('.collapse').toggle();
}

  }
  customElements.define(XFoo.is, XFoo);

2 个答案:

答案 0 :(得分:0)

我建议你使用自定义属性。以下是工作解决方案:https://codepen.io/Sahero/pen/VzeWNd

HTML:

<head>
   <base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
   <link rel="import" href="polymer/polymer.html">
   <link rel="import" href="iron-collapse/iron-collapse.html">
   <link rel="import" href="paper-input/paper-input.html">
</head>
<body>     
  <x-foo attr="{{text}}"></x-foo>

  <dom-module id="x-foo">
    <template>      
      <button on-click="toggle" my-attribute="collapse1">toggle 
      collapse</button>
      <iron-collapse id="collapse1" class="collapse">
        <div>Enter text collapse1</div>
      </iron-collapse>   

      <button on-click="toggle" my-attribute="collapse2">toggle 
      collapse</button>
      <iron-collapse id="collapse2" class="collapse">
      <div>Enter text collapse2</div>
      </iron-collapse>

      <button on-click="toggle" my-attribute="collapse3">toggle 
      collapse</button>
      <iron-collapse id="collapse3" class="collapse">
      <div>Enter text collapse3</div>
      </iron-collapse>

    </template>
  </dom-module>
</body>

JS:

  class XFoo extends Polymer.Element {
    static get is() { return 'x-foo'; }

    static get properties() {
      return {};

    }
   toggle(e) {
     //console.log(this);
     var id = e.target.getAttribute('my-attribute');
     console.log(id);
     this.shadowRoot.querySelector('#'+id).toggle();
     //this.shadowRoot.querySelector('.collapse').toggle();
  }

  }
  customElements.define(XFoo.is, XFoo);

答案 1 :(得分:0)

<dom-repeat>项目实现此目的的一种方法是使用事件的currentTarget,以便您可以选择调用方法的元素。我有一个示例,其中有多个列表项(paper-listbox),带有一个右箭头图标,当iron-collapse项展开时,该图标变为向下箭头。在我的示例中,不需要任何课程,但您可以轻松拥有e.currentTarget.querySelector('.theClass')

示例:

<dom-repeat items={{list}}>
    <template>
        <paper-listbox on-click="toggle">
            <iron-icon id="icon" icon="icons:chevron-right"></iron-icon>
            <paper-item>{{item.title}}</paper-item>
            <iron-collapse class="collapse">
                <p>Content here . . . </p>
            </iron-collapse>
        </paper-listbox>
    </template>
</dom-repeat>

及其下的JS:

toggle(e) {
    // currentTarget selects the paper-listbox where the toggle method occurred.
    var collapseArea = e.currentTarget.querySelector('iron-collapse');
    var theIcon = e.currentTarget.querySelector('iron-icon');
    collapseArea.toggle();
    // toggle the icon for list item from right arrow to downward arrow
    (theIcon.getAttribute('icon') === 'icons:chevron-right') ? theIcon.setAttribute('icon', 'icons:expand-more') : theIcon.setAttribute('icon', 'icons:chevron-right');
}