How to check the size of list in sightly?

Avatar

Avatar
Contributor 2
Level 4
Dinu_Arya
Level 4

Likes

38 likes

Total Posts

184 posts

Correct reply

16 solutions
Top badges earned
Contributor 2
Applaud 25
Validate 50
Validate 25
Validate 10
View profile

Avatar
Contributor 2
Level 4
Dinu_Arya
Level 4

Likes

38 likes

Total Posts

184 posts

Correct reply

16 solutions
Top badges earned
Contributor 2
Applaud 25
Validate 50
Validate 25
Validate 10
View profile
Dinu_Arya
Level 4

15-10-2015

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Post:

Hi,

I need to check size of the list in sightly. I need to display UI only when it has more than 2 items in list. Any idea?

Thanks,

Arya.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

15-10-2015

Depending on the methods of your list-object you can do something like this:

<div data-sly-test="${yourList.size > 2}">

...

</div>

Answers (11)

Answers (11)

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

15-10-2015

hi arya,

You can just the methods from the object, whether that is List, Array etc.

So if you have .size(), you can use .size. If you have .length() or .count(). You can use .length or .count.

Hope this helps.

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> 

--
Feike

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

18-09-2017

Use: ${myWCMUseObject.allColumns.size}

Avatar

Avatar
Boost 1
Level 1
mattm65985934
Level 1

Likes

2 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
mattm65985934
Level 1

Likes

2 likes

Total Posts

1 post

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
mattm65985934
Level 1

06-09-2016

This answer is no longer valid. As far as I can tell, the only way to accomplish this is to create another method in the initialized bean that returns the size of the list.

Avatar

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
hemantbellani-O
Level 2

18-09-2017

// The WCMUse class' method that gives me an ArrayList

public List<String> getAllColumns() {

  List<String> allColumns = new ArrayList<String>();

  for (int i = 1; i <= 10; i++) {

    allColumns.add("Test" + "-" + i);

  }

  return allColumns;

  }

 

  # sightly code that I am trying out ....

<sly data-sly-list.myList="${myWCMUseObject.allColumns}">

  <div data-sly-test="${myList.size > 5}" data-sly-unwrap>

  Heloo!!!!!

        </div>

</sly>

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

18-09-2017

Can you share a snippet of your code?

Avatar

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
hemantbellani-O
Level 2

17-09-2017

It is an ArrayList object

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

15-09-2017

What is myList for kind of object?

Avatar

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 2
hemantbellani-O
Level 2

Likes

11 likes

Total Posts

34 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
hemantbellani-O
Level 2

15-09-2017

Hi Felke.

On trying the snippet as below:

    <div data-sly-test="${myList.size > 5}" data-sly-unwrap>

  Heloo!!!!!

        </div>

We get an error as:

Invalid types in comparison. Comparison is supported for Number types only.

Could you please throw some light on why this could be the case?

Thanks,

Hemant Bellani

Avatar

Avatar
Give Back 5
Level 3
Tyler_Maynard
Level 3

Likes

11 likes

Total Posts

57 posts

Correct reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile

Avatar
Give Back 5
Level 3
Tyler_Maynard
Level 3

Likes

11 likes

Total Posts

57 posts

Correct reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back
Ignite 5
Ignite 3
View profile
Tyler_Maynard
Level 3

19-01-2017

I also find it very helpful to use the Ternary Operator in conjunction with the size/length to optionally add classes to an element. Just another front-end tip.

<div class="image-grid-item ${(properties['imageTiles'].length > 3 && rowList.count == 3 ) ? 'image-grid-item_bigger' : ''}"> </div>

Avatar

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile

Avatar
Give Back 50
Employee
Feike_Visser1
Employee

Likes

247 likes

Total Posts

970 posts

Correct reply

221 solutions
Top badges earned
Give Back 50
Give Back 5
Give Back 3
Give Back 25
Give Back 10
View profile
Feike_Visser1
Employee

07-09-2016

It indeed depends what kinds of object you are using for the list object.

Avatar

Avatar
Contributor 2
Level 4
Dinu_Arya
Level 4

Likes

38 likes

Total Posts

184 posts

Correct reply

16 solutions
Top badges earned
Contributor 2
Applaud 25
Validate 50
Validate 25
Validate 10
View profile

Avatar
Contributor 2
Level 4
Dinu_Arya
Level 4

Likes

38 likes

Total Posts

184 posts

Correct reply

16 solutions
Top badges earned
Contributor 2
Applaud 25
Validate 50
Validate 25
Validate 10
View profile
Dinu_Arya
Level 4

15-10-2015

Hi Feike,

Thanks. I have gone through sightly tutorials. But I didn't find this attribute. Actually we have size() for java.util.ArrayList. Can I use all of List methods like mylist.<ListMethod> ? Where can I find these tutorials? I have seen only count,first,last,index etc.

 

Thanks,

Arya.