2016-05-10 37 views
1

我显示两个阵列 - 用一个又一个的无序列表(ul)聚合物的dom-repeat聚合物 - 启动指数DOM的重复

<ul> 
    <template is="dom-repeat" items="{{array1}}" index-as="position_id"> 
    <li on-tap="select1" class$="{{_selectedStyle(selectedId, position_id)}}"> 
     {{item.fileName}} 
    </li> 
    </template> 
    <template is="dom-repeat" items="{{array2}}" index-as="position_id"> 
    <li on-tap="select2" class$="{{_selectedStyle(selectedId, position_id}}"> 
     {{item.fileName}} 
    </li> 
    </template> 
</ul> 
... 
_selectedStyle: function (selectedPosId, posId) { 
    if (selectedPosId && posId){ 
    return (selectedPosId === posId) ? "selected" : ""; 
    } 
} 

我要对所选项目应用类,但明显无法正常工作,因为对于dom重复模板selectedIdposition_id都是相同的(均从0开始)。理想情况是在第二个dom-repeat模板上有偏移量,但似乎不支持此功能。这种情况下最好的解决方法是什么?

回答

2

也仅仅是通过偏移(的array1长度)和_selectedStyle()添加它们(见下面的注释):

<li on-tap="select2" class$="{{_selectedStyle(selectedId, position_id, array1.length}}"> 
    {{item.fileName}} 
</li> 
+0

我可能是错的,但我认为在功能参数不支持表达式。我尝试了数组长度和数字 - 在这种情况下,函数“_selectedStyle”根本不被调用。 – streetturtle

+0

对不起,可能是。我从一段时间以来没有使用聚合物。您需要创建一个可以传递两个值并获得结果的函数。也许你可以改变'_selectedStyle(...)'接受3个参数并添加第二个和第三个参数。 –

+1

是的,我喜欢这样。我添加了第三个偏移量的参数。 '_selectedStyle'函数现在是这样的:'return(selectedPosId === posId +(offset || 0))? “选中”:“”;'无论如何谢谢! – streetturtle