首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示铁表中可见的最后一项?

如何显示铁表中可见的最后一项?
EN

Stack Overflow用户
提问于 2016-11-21 09:14:54
回答 2查看 379关注 0票数 0

我正在创建聊天页面。我正在用铁表更新信息。每次我向array iron-list发送消息时,都会添加到该模板中。现在,我希望在底部显示最后一个附加消息。目前,用户必须滚动到底部并查看该消息。

代码语言:javascript
复制
// Polymer
  properties: {
    messageItem: { type: Array, notify: true, value: function() { return []; } },
  },

# Html
<iron-list items="[[messageItem]]" as="item">
  <template>...</template>
</iron-list>

推送代码:

代码语言:javascript
复制
this.push('messageItem', {name: 'bot', message: event.data});

我应该采取哪些必要步骤来实现这一目标?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-21 10:39:41

在将项添加到列表并允许其呈现后,可以将<iron-list>.scrollTop设置为它的scrollHeight。为此,您可以使用Polymer.RenderStatus.afterNextRender()

代码语言:javascript
复制
// template
<iron-list id="list">...</iron-list>

// script
_addItem: function() {
  this.push('items', ...);
  Polymer.RenderStatus.afterNextRender(this, () => {
    this.$.list.scrollTop = this.$.list.scrollHeight;
  });
}

代码语言:javascript
复制
HTMLImports.whenReady(() => {
  Polymer({
    is: 'x-foo',
    properties: {
      items: {
        type: Array,
        value: () => [0,1,2]
      }
    },
    _addItem: function() {
      this.push('items', this.items.length);
      this._scrollToBottom();
    },
    _scrollToBottom: function() {
      Polymer.RenderStatus.afterNextRender(this, () => {
        this.$.list.scrollTop = this.$.list.scrollHeight;
      });
    }
  });
});
代码语言:javascript
复制
<head>
  <base href="https://polygit.org/polymer+1.7.0/components/">
  <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-list/iron-list.html">
</head>
<body>
  <x-foo></x-foo>

  <dom-module id="x-foo">
    <template>
      <style>
        iron-list {
          border: solid 1px red;
          height: 100px;
        }
      </style>
      <button on-tap="_addItem">Add item</button>
      <iron-list id="list" items="[[items]]">
        <template>
          <div>[[item]]</div>
        </template>
      </iron-list>
    </template>
  </dom-module>
</body>

码页

票数 1
EN

Stack Overflow用户

发布于 2016-11-21 09:36:27

我没有使用铁列表,但据我所知,messageItem是一个对象数组,因此您可以根据对象的一个属性值(例如索引)对其元素进行排序。我可以从他们的文档中看到,他们有一个您可以添加的索引属性。这意味着,您可以使用索引对降序对象数组进行排序,因此可以执行以下操作:

代码语言:javascript
复制
messageItem.sort(function(index1, index2){return index1-index2});

哪里

代码语言:javascript
复制
index1 = messageItem[i].index; and index2 = messageItem[i-1].index;

这是我最好的主意:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40716462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档