从聚合物核心列表中获取所选项目的索引

时间:2021-10-16 22:57:27

I'm trying to scroll a simple core-list with a keyboard key.
This is the 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>

I need to get the index of the selected post-card, increment it and pass it to the selectItem and scrollToItem methods of the core-list.

我需要获取所选明信片的索引,将其递增并将其传递给核心列表的selectItem和scrollToItem方法。

The problem I'm facing is... how do i get the index?
I searched for an easy way to get the it both in core-list and in core-selection without success.

我面临的问题是......我如何获得索引?我搜索了一个简单的方法,在核心列表和核心选择中都没有成功。

Unfortunately the core-list attribute "selection" from core-list is the data of the current selected record (so I can't get the index attribute from it).

不幸的是,核心列表中的核心列表属性“选择”是当前所选记录的数据(因此我无法从中获取索引属性)。

Am I missing something? Is there any solution that doesn't involve handling the index directly in the post-card component?

我错过了什么吗?是否有任何解决方案不涉及直接在卡后组件中处理索引?

Thanks.

1 个解决方案

#1


Core-list item is core-selection which handling selection event. You could try to use core-select event which you could get the index of selected item. So it could be like this

核心列表项是处理选择事件的核心选择。您可以尝试使用core-select事件,您可以获取所选项目的索引。所以它可能是这样的

selectedHandler: function(e, detail, sender) {
     var i=this.$.selection.indexOf(detail.item);
     this.scrollToItem(i+1);
   }

#1


Core-list item is core-selection which handling selection event. You could try to use core-select event which you could get the index of selected item. So it could be like this

核心列表项是处理选择事件的核心选择。您可以尝试使用core-select事件,您可以获取所选项目的索引。所以它可能是这样的

selectedHandler: function(e, detail, sender) {
     var i=this.$.selection.indexOf(detail.item);
     this.scrollToItem(i+1);
   }