【问题标题】:Polymer 1.x: How to pre-process the items in a list or repeater?Polymer 1.x:如何预处理列表或中继器中的项目?
【发布时间】:2016-10-18 11:44:41
【问题描述】:

Here is the plunk.

谁能指出一种模式,例如,格式化(或预处理)进入转发器(如iron-list 或iron-data-table)的数据项?

换句话说,consider this plunk for example。假设我想为每个用户添加一个字段并将其显示在列表中;我们称它为namelength,其中:

item.user.namelength = item.user.name.first.length + item.user.name.last.length

我将如何(在 HTML 或 JS 中的什么位置以及使用什么模式)最好地处理此预处理任务?

内容-el.html
<base href="https://polygit.org/polymer+:master/iron-data-table+Saulis+:master/components/">
<link rel="import" href="polymer/polymer.html">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>

<link rel="import" href="iron-ajax/iron-ajax.html">
<link rel="import" href="paper-button/paper-button.html">

<link rel="import" href="iron-data-table/iron-data-table.html">

<dom-module id="content-el">
    <template>
        <style></style>

    <iron-ajax
      auto
      url="https://saulis.github.io/iron-data-table/demo/users.json" 
      last-response="{{users}}">
    </iron-ajax>
    <iron-data-table items="[[users.results]]">
      <data-table-column name="Picture" width="50px" flex="0">
        <template>
          <img src="[[item.user.picture.thumbnail]]">
        </template>
      </data-table-column>
      <data-table-column name="First Name">
        <template>[[item.user.name.first]]</template>
      </data-table-column>
      <data-table-column name="Last Name">
        <template>[[item.user.name.last]]</template>
      </data-table-column>
      <data-table-column name="Email">
        <template>[[item.user.email]]</template>
      </data-table-column>
    </iron-data-table>

    </template>

  <script>
    (function() {
      'use strict';
      Polymer({
        is: 'content-el',
            });
        })();
  </script>
</dom-module>

【问题讨论】:

    标签: polymer polymer-1.0 plunker iron-data-table polymer-1.x


    【解决方案1】:

    例如

    <data-table-column name="Name Length">
        <template>{{_len(item.user.name.first, item.user.name.last)}}</template>
    </data-table-column>
    

    ...

    (function() {
      'use strict';
      Polymer({
        is: 'content-el',
        _len: function(first, last) {
          return first.length + last.length;
        }
            });
        })();
    

    【讨论】:

      【解决方案2】:

      您可以使用 computed binding 通过添加您描述的 namelength 字段来预处理 user.results:

      // template
      <iron-data-table items="[[_getUserData(users.results)]]">
        ...
        <data-table-column name="Name Length">
          <template>[[item.user.namelength]]</template>
        </data-table-column>
      </iron-data-table>
      
      // script
      Polymer({
        is: 'content-el',
        _getUserData: function(items) {
          items.forEach(function(item) {
            var user = item.user;
            user.namelength = user.name.first.length + user.name.last.length;
          });
          return items;
        }
      });
      

      plunker

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多