Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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

@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

Community 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
Level 8

@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