从聚合物核心列表中获取所选项目的索引
Getting the index of a selected item from a polymer core-list
我正在尝试使用键盘键滚动一个简单的核心列表。
这是核心列表:
post-list.html
<core-list data="{{posts}}" selectionEnabled="true" selection="{{selectedPost}}" on-core-select="{{selectedHandler}}" fit >
<template repeat>
<post-card post="{{model}}" index="{{index}}" ></post-card>
</template>
我需要获取所选 post-card 的索引,递增它并将其传递给 selectItem 和 scrollToItem 核心列表的方法。
我面临的问题是...如何获取索引?
我搜索了一种简单的方法来在核心列表和核心选择中都获得它,但没有成功。
不幸的是,core-list 中的 core-list 属性 "selection" 是当前选定记录的数据(因此我无法从中获取索引属性)。
我错过了什么吗?
是否有任何解决方案不涉及直接在 post-card 组件中处理索引?
谢谢。
Core-list 项目是 core-selection,它处理 selection 事件。您可以尝试使用 core-select 事件,您可以获得 selected 项目的索引。所以它可能是这样的
selectedHandler: function(e, detail, sender) {
var i=this.$.selection.indexOf(detail.item);
this.scrollToItem(i+1);
}
我正在尝试使用键盘键滚动一个简单的核心列表。
这是核心列表:
post-list.html
<core-list data="{{posts}}" selectionEnabled="true" selection="{{selectedPost}}" on-core-select="{{selectedHandler}}" fit >
<template repeat>
<post-card post="{{model}}" index="{{index}}" ></post-card>
</template>
我需要获取所选 post-card 的索引,递增它并将其传递给 selectItem 和 scrollToItem 核心列表的方法。
我面临的问题是...如何获取索引?
我搜索了一种简单的方法来在核心列表和核心选择中都获得它,但没有成功。
不幸的是,core-list 中的 core-list 属性 "selection" 是当前选定记录的数据(因此我无法从中获取索引属性)。
我错过了什么吗? 是否有任何解决方案不涉及直接在 post-card 组件中处理索引?
谢谢。
Core-list 项目是 core-selection,它处理 selection 事件。您可以尝试使用 core-select 事件,您可以获得 selected 项目的索引。所以它可能是这样的
selectedHandler: function(e, detail, sender) {
var i=this.$.selection.indexOf(detail.item);
this.scrollToItem(i+1);
}