How do I get the multifield count in a component AEM? | Community
Skip to main content
Level 4
June 27, 2022
Solved

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

  • June 27, 2022
  • 2 replies
  • 2974 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ShaileshBassi

@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 

2 replies

milind_bachani
Adobe Employee
Adobe Employee
June 27, 2022

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

 

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
June 27, 2022

@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 

Ameen_DevAuthor
Level 4
June 29, 2022

Thanks for your solution. It worked as expected.