【问题标题】:How to find the size of a sightly list如何找到一个视觉列表的大小
【发布时间】:2020-07-30 14:34:32
【问题描述】:

我需要仔细检查列表的大小。我试过resource.size、item.size、itemList.size、resource.listChildren.size、item.listChildren.size,但这些都不起作用。

我打算使用 Java USE-API,但我不知道如何开始。

<sly data-sly-list="resource.listChildren">
    <sly data-sly-list="item.listChildren">
        ${itemList.count} / #I need the total count of item here#
    </sly>
</sly>

【问题讨论】:

标签: java list api aem sightly


【解决方案1】:

由于Resource#listChildren 将返回Iterator&lt;Resource&gt;,您将无法从那里获取子列表的大小。

HTL 也不提供获取列表大小的方法,但它允许获取interesting properties of the current item

* index: zero-based counter (0..length-1);
* count: one-based counter (1..length);
* first: true for the first element being iterated;
* middle: true if element being iterated is neither the first nor the last;
* last: true for the last element being iterated;
* odd: true if count is odd;
* even: true if count is even.

【讨论】:

    【解决方案2】:

    &lt;sly data-sly-list.demo=“${demo123.items.listChildren}”&gt; &lt;sly data-sly-test=“${demoList.last}”&gt; ${demoList.count}&lt;/sly&gt;

    测试条件-> demoList.last -> demoList.count

    【讨论】:

      【解决方案3】:
      1. 尝试为列表使用变量名。
      2. 如果列表中有一个列表(嵌套列表),则始终建议使用变量名。
      <sly data-sly-list.item="resource.listChildren">
          <sly data-sly-list.itemList="item.listChildren">
              ${itemList.length} / #I need the total count of item here#
          </sly>
      </sly>
      

      尝试使用长度而不是大小和计数。 它对我有用。

      【讨论】:

        【解决方案4】:

        Java 文件:

        private List<MycustomObject> myList = new ArrayList<>();
        

        HTL:

        <sly data-sly-use.myCustomModel="my.custom.location.myCustomModel"> 
         ${myCustomModel.myList.size}
        

        【讨论】:

          【解决方案5】:

          我们可以像-

          <sly data-sly-test.totalItems="${0}"></sly>
          <sly data-sly-list.child="${resource.listChildren}">
              <sly data-sly-test.totalItems="${ childList.count }"></sly>
          </sly>
          

          ${ totalItems } - 这将为您提供列表中的项目数。

          【讨论】:

            【解决方案6】:

            在您的列表中,您可以使用 ${itemList.count} 之类的东西,但这仅适用于 data-sly-list:

          • ${item.name} 页 ${itemList.index},共 ${itemList.count}
          • 【讨论】:

            • count 只是一个从 1 开始的计数器,不会显示项目总数
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-02-18
            • 2015-03-11
            • 1970-01-01
            • 1970-01-01
            • 2019-05-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多