【问题标题】:Iron-data-table fails to populate data from databindingIron-data-table 无法从数据绑定中填充数据
【发布时间】:2016-07-27 07:01:39
【问题描述】:

The following code taken from this demo works in my app:

https://saulis.github.io/iron-data-table/demo/index.html
<template is="dom-bind">
  <iron-ajax
      url="https://saulis.github.io/iron-data-table/demo/users.json"
      last-response="{{users}}"
      auto>
  </iron-ajax>
  <iron-data-table id="items"
                   selection-enabled 
                   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>

但是当我将数据源从 iron-ajax 调用更改为内部数据绑定 {{items}} 时,它不起作用。

项目列表.html
<template is="dom-bind">
  <iron-data-table id="items"
                   selection-enabled
                   data-source="[[items]]">
    <data-table-column name="Comment" width="50px" flex="0">
      <template>[[item.comment]]</template>
    </data-table-column>
    <data-table-column name="Total">
      <template>[[item.total]]</template>
    </data-table-column>
    <data-table-column name="Merchant">
      <template>[[item.merchant]]</template>
    </data-table-column>
  </iron-data-table>
</template>

我希望表格中填充数据。相反,表格和列标题会呈现,但数据不会填充表格。

有几点: 我正在使用 Firebase 来存储数据。和polymerfire/firebase-query 元素(在所示元素的父数据绑定元素中)获取{{items}} 数据。

我应该朝哪个方向尝试解决这个问题?

编辑

以下描述了我为items 变量设置的方式和内容:

我的 Firebase 内部的数据结构如下。

https://console.firebase.google.com/project/my-app/database/data
my-app
 |
 - users
    |
    - userid-123
       |
       - nodeid-abc
       |  |- comment: "foo"
       |  |- merchant: "bar"
       |  |- date: "2016-07-02"
       |  |- total: 123
       - nodeid-xyx
          |- comment: "baz"
          |- merchant: "bat"
          |- date: "2016-07-15"
          |- total: 999

我通过 Polymerfire firebase-query 父元素中的元素将这些数据提取到我的应用程序中。

概述-page.html
<firebase-query
    id="query"
    app-name="my-app"
    path="/users/[[user.uid]]"
    data="{{items}}">
</firebase-query>
<items-list id="list"
    items="[[items]]"
    filters="[[filters]]">
</items-list>

我知道{{items}} 数据正在进入list-items 元素,因为我设置了一个按钮来将数据记录到控制台。

项目列表.html
<button on-tap="show">Show</button>
show: function() {
  console.log('items', this.items);
},

当我按下 Show 按钮时,控制台会记录以下内容:

控制台日志
items
0: Object
   $key: "nodeid-abc"
   comment: "foo"
   merchant: "bar"
   date: "2016-07-02"
   total: 123
1: Object
   $key: "nodeid-xyx"
   comment: "baz"
   merchant: "bat"
   date: "2016-07-15"
   total: 999

有趣的是,当我为工作代码示例记录相同的信息时:

show: function(){
  console.log('users', this.users);
}

我明白了:

控制台日志
users undefined

去图吧。显然,有些事情我不明白。看起来{{items}} 数据绑定在iron-data-table 呈现后注册,并且由于某种原因,数据绑定在iron-data-table 中注册后未在iron-list 元素中更新。

编辑2

这是items-list 元素的整个dom-module 定义。

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/iron-data-table/iron-data-table.html">
<link rel="import" href="../bower_components/iron-data-table/default-styles.html">

<dom-module id="items-list">
  <template>
    <button on-tap="show">Show</button>
    <template is="dom-bind">
      <iron-data-table items="[[items]]">
        <data-table-column name="Comment">
          <template>[[item.comment]]</template>
        </data-table-column>
        <data-table-column name="Merchant">
          <template>[[item.merchant]]</template>
        </data-table-column>
      </iron-data-table>
    </template>
  </template>
  <script>
    (function() {
      'use strict';
      Polymer({
        is: 'items-list',
        properties: {
          items: {
            type: Array,
            notify: true,
          },
        },
        show: function() {
          console.log('items', this.items);
        },
      });
    })();
  </script>
</dom-module>

【问题讨论】:

  • 嗨!我是后一个例子,你能说明你是如何设置items变量的吗?
  • @SauliTähkäpää:感谢您提出的澄清问题。我想我在回复中提供了很多细节。如果您想要更多,请告诉我。我很乐意提供。
  • 你能给我提供dom-module 元素的items-list 声明吗?
  • @SauliTähkäpää:我刚刚在 Edit2 中添加了它作为代码 sn-p。

标签: datatable datagrid polymer polymer-1.0 iron-data-table


【解决方案1】:

您需要删除额外的&lt;template is="dom-bind"&gt; 元素,它将&lt;iron-data-table&gt; 封装在单独的绑定范围中。不需要dom-bind,因为您有来自dom-module 的绑定范围

我假设您从演示示例中复制了 dom-bind 元素,因为没有包裹在 &lt;iron-data-table&gt; 周围的自定义元素,所以需要它们。您可以在此处查看更多信息:https://www.polymer-project.org/1.0/docs/devguide/templates#dom-bind

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多