Expand my Community achievements bar.

SOLVED

Sightly : How to Iterate through Map<String,Object>

Avatar

Level 2

Hi ,

I am not able to iterate the map in sightly. The map is having values as a bean object which i am not able to get in sightly.

I am able to get the keys directly but for values i understood there has to be a second loop since it is an object .

the code i have tried :

<sly data-sly-use.model="package.Model"/>

<sly data-sly-list.keyName="${model.map}">

<li>keyname = ${keyName}</li>

<li>keyvalue = ${model.map[keyName]}</li>

    <sly data-sly-list.key = "${model.map[keyName]}">

    BeanData= ${key}

                <ol><li>${key.text}<li></ol><br/>

    </sly>

</sly>

Here in my code keyname is printed on the page but keyvalue is not.

In java class the map is of type : map<String,Bean> and Bean class have the data members which i want to retrieve like text.

Please guide where am I going wrong.

I have already gone thru many links but couldn't find the solution.

Links referred:

Sightly : How to Iterate through Map&lt;String , Map&gt;

Iterating a map in sightly which have custom object as key

How to traverse a Map having Keys of Object Type using Sightly ?

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi Feike,

Thanks for the instant replies.

I made it work :

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample">

  <sly data-sly-list="${hashmap.map.keySet.iterator}">

     <li>key = ${item}</li>    

     <ul data-sly-test.beanObj="${hashmap.map[item]}">  //This should be data-sly-test, no need to iterate again

          <li> text = ${beanObj.text}</li>

     </ul>

   </sly>

</div>

View solution in original post

6 Replies

Avatar

Level 2

yes fieke i saw but i couldn't understand how will i get that specific data member from my bean class. Could you please help me understand.

I have a data member "text" in a bean class which i m returning using a sling model ->map.put(key,bean).

As per the link :

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample"

     data-sly-list="${hashmap.map.keySet.iterator}">

     ${item}

     <ul data-sly-list.aem="${hashmap.map[item].keySet.iterator}">

      <li>${aem} ${hashmap.map[item][aem]}</li>

     </ul>

</div>

How can i fetch my data member "text" using the inner loop?

Avatar

Employee

I would think this would work in your case ${hashmap.map[item].yourProp}

Avatar

Level 2

Thanks for the reply feike but  this ${hashmap.map[item].yourProp} is not working.

Avatar

Level 2

Infact it is not going inside the inner loop.And i tried printing the value of ${hashmap.hubarticle[item]} but nothing is getting printed.

This is what i have tried:

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample">

  <sly data-sly-list="${hashmap.map.keySet.iterator}">

<li>key = ${item}</li>

<li>value = ${hashmap.map[item]}</li> //This is not printing anything(This should be the beanObject logically)

<ul data-sly-list.aem="${hashmap.map[item].keySet.iterator}"> 

<li> text = ${hashmap.map[item].text}</li> //Here, "beanObject.customProp" should work but it is not printing

</ul>

   </sly>

</div>

Can you please have a look fieke?

Avatar

Correct answer by
Level 2

Hi Feike,

Thanks for the instant replies.

I made it work :

<div data-sly-use.hashmap="com.adobe.examples.htl.core.hashmap.HashMapExample">

  <sly data-sly-list="${hashmap.map.keySet.iterator}">

     <li>key = ${item}</li>    

     <ul data-sly-test.beanObj="${hashmap.map[item]}">  //This should be data-sly-test, no need to iterate again

          <li> text = ${beanObj.text}</li>

     </ul>

   </sly>

</div>