Tap listener for polymer iron-list item?

痴心易碎 提交于 2019-12-06 06:32:54

I do this by encoding an array index in a list element id, then pulling the id out of a list item event target. Here is an example Polymer element that does this.

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/iron-list/iron-list.html">

<dom-module id="list-example">
  <style>
    :host {
      display: block;
    }

    #list-example {
      height: 100px;
    }
  </style>
  <template>

    <paper-material animated elevation="1">
      <iron-list id="list-example" items="[[data]]">
        <template>
          <div id="{{index2id(item.index)}}" on-mouseover="onMouseOverItem">{{item.name}}</div>
        </template>
      </iron-list>
    </paper-material>

  </template>
</dom-module>

<script>
  (function () {
    Polymer({
      is: 'list-example',

      ready: function() {
        for(var i = 0; i < this.data.length; i++) {
          this.data[i].index = i;
        }
      },

      index2id: function(index) {
        return "_" + index;
      },

      id2index: function(id) {
        return Number(id.substr(1));
      },

      onMouseOverItem: function(e) {
        console.log('on-mouseover list item:', this.data[this.id2index(e.target.getAttribute('id'))]);
      },

      properties: {
        data: {
          type: Array,
          value: [{name: 'A'}, {name: 'B'}, {name: 'C'},
                  {name: 'D'}, {name: 'E'}, {name: 'F'},
                  {name: 'G'}, {name: 'H'}, {name: 'I'}]
        }
      }
    });
  })();
</script>

Follow the model outlined on lines 154 and 184 of this demo. https://github.com/PolymerElements/iron-list/blob/master/demo/collapse.html

my-element.html
<iron-list items="[[items]]">
  <template>
    <my-list-item on-tap="_toggleMe"></my-list-item>
  </template>
</iron-list>
...
_toggleMe: function(e) {
  console.log(e.model.index);
}

The key is to place the event and listener method (toggleMe() in this case) inside the <template> of the iron-list. This allows the iron-list to register the array index.

I was having a similar issue and solved my problem using <array-selector> as follows:

<iron-list items="{{myarray}}" as="ref">
  <template>
    <div>
      <paper-checkbox on-tap="toggleSelection"></paper-checkbox>
      <span>{{ref.name}}</span>
    </div>
  </template>
</iron-list>
<array-selector id="arrsel" items="{{myarray}}"
                selected="{{selectedName}}" toggle></array-selector>

and myarray is an array of objects:

var myarray = [{name: "Alice"}, {name: "Ben"}, ...]

and the function toggleSelection is defined as follows:

toggleSelection: function(e) {
  console.log ("Selected index is " + e.model.index);
  console.log ("Selected name is " + e.model.ref);
  this.$.arrsel.select (e.model.ref);
  console.log ("Current selection: ", this.selectedName);
}

The field name ref after e.model.__ is the value of the as attribute of iron-list.

WARNING: The variable e.model is not officially documented on the Polymer 1.0 iron-list doc (https://elements.polymer-project.org/elements/iron-list), however I discovered it during my debugging session. I am assuming that e.model is a public property (the coding style of Polymer uses underscore prefix for private property such as: _scroll_Position) and it is not a candidate for deprecation.

I just solved my issue of here https://groups.google.com/forum/#!topic/polymer-dev/r9IsUKVnLVM. Reading this documentation https://www.polymer-project.org/1.0/docs/devguide/events.html.

I hope it helps you!

Wrap your iron-list with an iron-selector - this allows you get to get the row selected / tapped.

(NB: you might need to remove your custom indexAs="_id" attribute to get the right row index)

<iron-selector attr-for-selected="index" on-tap="_itemSelected">
    <iron-list id="projectList" items="[[projects]]" as="projLI" class="fit">
        <template>
            <div class="layout horizontal center" id="{{index}}">
                <!-- your row content here -->
            </div>
        </template>
    </iron-list>
</iron-selector>

Polymer method for row item selected:

_itemSelected: function (e) {
    console.log(e.target.id); // selected iron-list row index
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!