0

我对 Dart 很陌生,并且试图知道如何在如下所述的情况下获取列表中元素的当前索引:

1)当从数据库中获取对象列表并通过 JSON 发送到客户端 Dart Web 应用程序时,我运行服务器 2)在 Dart 客户端网页上,我想将该列表的元素布局在表格中,但在一种 mod3 方式,即每三列超过 N 行。

所以我想使用 Polymer 模板扫描列表,获取当前元素,将其添加到表中(即编写 HTML 标记),然后检查当前元素 mod3 (%3) 的索引是否 = 为零。

我的问题是我不知道如何获取该列表当前元素索引

任何帮助将不胜感激!

4

1 回答 1

1

An easy way to access the index of the current iterator being used in your polymer repeat statement, is to use the enumerate filter.

<polymer-element name="my-element">
  <template>
    <table>
      <tbody template repeat="{{item in myList | enumerate}}">
        <template if="{{item.index % 3 == 0}}">
          <tr>
        </template>
      <td>item.value</td>
        <template if="{{item.index % 3 == 0}}">
          </tr>
        </template>
      </tbody>
    </table>
  </template>
</polymer-element>

However this is not totally completed as there were issues in the past in which you could not use a <template> within a table element. I'm not 100% sure if that is still the case or not (as I have been unable to locate any definitive information stating resolution or not). An alternative is to use <div>'s and classes to layout your data.

Simply use some CSS to set the div's style to table, table-row, or table-cell respectively. See CSS Display Property on W3Schools for more information. The <div> solution would permit templates as required.

于 2013-10-28T12:56:22.410 回答