Iterating a map in sightly which have custom object as key | Community
Skip to main content
Level 4
October 16, 2015
Solved

Iterating a map in sightly which have custom object as key

  • October 16, 2015
  • 11 replies
  • 5820 views

Hi All,

(Similar issue to mine: http://coderissues.com/questions/31437688/how-to-traverse-a-map-having-keys-of-object-type-using-sightly )

I am having tough time iterating a custom map in sightly, previously I have iterated through normal map<String, String> many type in sightly and it worked perfectly fine.

My custom map Map<Product, List<CartEntry>>, As I am using Product as key to my map I have overridden the equals and hashcode method for my product class as below. I am able to iterate the map in normal java class and able to output the required values. But when I trying to iterate it in sightly it is failing and saying error as invalid key.

<div data-sly-list.parentEntry="${shoppingCart.cartItems}" data-sly-unwrap>

         <div data-sly-list.childEntry="${shoppingCart.cartItems[parentEntry]}" data-sly-unwrap>

                 ${childEntry.productCode}

        </div>

</div>

    @Override
    public int hashCode()
    {
        return this.code.hashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Product other = (Product) obj;
        if (this.code != other.getCode())
        {
            return false;
        }

        return true;
    }

Not sure how sightly has implemented data-sly-list? So it is possible to iterate this kind of map in sightly?

Any hints would be greatly appreciated, 

Kind regards,

Shehjad

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 Shehjadkhan

Hi All,

I was able to make it work, I just need to use entrySet. Below is the code

 

div data-sly-list.cartItemsEntrySet="${shoppingCart.cartItems.entrySet}" data-sly-unwrap>

         <div data-sly-list.childEntry="${cartItemsEntrySet.value}" data-sly-unwrap>

                 ${cartItemsEntrySet.key.productCode}

        </div>

</div>

Thanks!

Shehjad

11 replies

Level 3
March 3, 2016

It is my aem instance issue I guess.I am able to iterate simple and custom class map in my colleagues machine.I tried with your code as well but it dint worked in my machine.