如何找到一个不大的列表的大小

huangapple go评论155阅读模式
英文:

How to find the size of a sightly list

问题

I need to check the size of list in sightly. I have tried resource.size, item.size, itemList.size, resource.listChildren.size, item.listChildren.size, but none of these works.

I'm planning to use Java USE-API, but I have no clue how to start.

<sly data-sly-list="resource.listChildren">
    <sly data-sly-list="item.listChildren">
        ${itemList.count} / #我需要这里的item的总数#
    </sly>
</sly>
英文:

I need to check the size of list in sightly. I have tried resource.size, item.size, itemList.size, resource.listChildren.size, item.listChildren.size, but none of these works.

I'm planning to use Java USE-API, but I have no clue how to start.

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

答案1

得分: 2

由于 Resource#listChildren 将返回一个 Iterator<Resource>,因此您将无法从那里获取子项列表的大小。

HTL 也不提供获取列表大小的方法,但它允许获取当前项的有趣属性

* index:从零开始的计数器(0..length-1);
* count:从一开始的计数器(1..length);
* first:在迭代的第一个元素为 true;
* middle:如果正在迭代的元素既不是第一个也不是最后一个,则为 true;
* last:在迭代的最后一个元素为 true;
* odd:如果计数为奇数,则为 true;
* even:如果计数为偶数,则为 true。
英文:

Since Resource#listChildren will return an Iterator<Resource>, you will not be able to get the size of the list of children from there.

HTL does not offer a way to get the list size either, however it allows getting 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

得分: 1

<sly data-sly-list.demo="${demo123.items.listChildren}"> <sly data-sly-test="${demoList.last}"> ${demoList.count}</sly>

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

英文:

<sly data-sly-list.demo=“${demo123.items.listChildren}”>
<sly data-sly-test=“${demoList.last}”> ${demoList.count}</sly>

Test condition-> demoList.last -> demoList.count

答案3

得分: 0

  1. 尝试为列表使用一个变量名。
  2. 如果你有一个嵌套列表(内部有列表),建议始终使用一个变量名。
<sly data-sly-list.item="resource.listChildren">
    <sly data-sly-list.itemList="item.listChildren">
        ${itemList.length} / #我需要这里的项目总数#
    </sly>
</sly>

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

英文:
  1. Try using a variable name for the list.
  2. If you have a list inside a list(nested list), it is always recommended to use a variable name.

> <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>

Try using length instead of size and count.
It works for me.

答案4

得分: 0

Java File:

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

HTL:

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

Java File:

private List&lt;MycustomObject&gt; myList = new ArrayList&lt;&gt;();

HTL:

&lt;sly data-sly-use.myCustomModel=&quot;my.custom.location.myCustomModel&quot;&gt; 
 ${myCustomModel.myList.size}

答案5

得分: 0

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

英文:

We can count number of items in sightly/HTL like -

&lt;sly data-sly-test.totalItems=&quot;
&lt;sly data-sly-test.totalItems=&quot;${0}&quot;&gt;&lt;/sly&gt;
&lt;sly data-sly-list.child=&quot;${resource.listChildren}&quot;&gt;
&lt;sly data-sly-test.totalItems=&quot;${ childList.count }&quot;&gt;&lt;/sly&gt;
&lt;/sly&gt;
&quot;&gt;&lt;/sly&gt; &lt;sly data-sly-list.child=&quot;${resource.listChildren}&quot;&gt; &lt;sly data-sly-test.totalItems=&quot;${ childList.count }&quot;&gt;&lt;/sly&gt; &lt;/sly&gt;

${ totalItems } - this will give you the number of items in the list.

答案6

得分: -2

在您的列表内,您可以使用像 ${itemList.count} 这样的东西,但这只能在 data-sly-list 内部工作:

<ul data-sly-list="${currentPage.listChildren}">
    <li>${item.name} 第 ${itemList.index + 1} 页 共 ${itemList.count} 页</li>
</ul>
英文:

Inside your list, you can use things like ${itemList.count}, but this only works inside data-sly-list:

<ul data-sly-list="${currentPage.listChildren}">

<li>${item.name} Page ${itemList.index} of ${itemList.count}</li>

</ul>

huangapple
  • 本文由 发表于 2020年7月30日 22:34:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63175443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定