Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How do I get the multifield count in a component AEM?

Avatar

Level 4

Actually, I need to get the multifield count in the component, and then I need to use that count in the HTL. Is there any way to get the multifield count?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Ameen_Dev 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.

 

e.g. If you want to display the whole list count

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

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

Thanks 

View solution in original post

3 Replies

Avatar

Employee Advisor

Hi @Ameen_Dev ,

 

You already might be using a model to get the multi data, that multi is returned as a list which you can iterate in HTL.

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

Either you can , make use of current item properties :

// iterate and check if thats the last item, if yes -> return the count
<sly data-sly-list.demo=“${demo123.items.listChildren}”> <sly data-sly-test=“${demoList.last}”> ${demoList.count} </sly>

Or you can declare a variable for itemList and use .length :

<sly data-sly-list.item="resource.listChildren">
    <sly data-sly-list.itemList="item.listChildren">
        ${itemList.length} 
    </sly>
</sly>

Hope this helps!

Thanks,

Milind

 

Avatar

Correct answer by
Community Advisor

@Ameen_Dev 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.

 

e.g. If you want to display the whole list count

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

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

Thanks