cancel
Showing results for 
Search instead for 
Did you mean: 

Figure out which one is the last component on the slot

adiputera
Active Participant
392

Hi All,

Does anyone knows how to figure out which component is the last to be rendered in the slot?

Let's say for example in Slot1A-Homepage I have 10 banners, I can use sessionService to figure out which banner in which index by writing this code in the component's controller

Integer bannerIndex = sessionService.getAttribute("bannerIndex");
if (null == bannerIndex)
bannerIndex = 0;
bannerIndex++;
sessionService.setAttribute("bannerIndex", bannerIndex);
model.addAttribute("bannerIndex", bannerIndex);

and then in the component's JSP I just need to call "bannerIndex" attribute.

But using this I can't figure out how many banners are there, I can count it using javascript, but what I want to know is it possible to know the last component on the slot from the controller or jsp?

I already trying using beforeViewHandler, but it's executed even before coming to component's controller.

Accepted Solutions (1)

Accepted Solutions (1)

adiputera
Active Participant

Turns out Hybris already provides attributes for this.

All I need to do is get the attribute from the request on the component's jsp:

${elementPos} // element position
${isLastElement} // flag to determine if it's last element
${isFirstElement} // flag to determine if it's first element
${numberOfElements} // return the number of elements on the slot

If I need to get it from the backend (component's controller), then I can use:

Integer elementPos = request.getAttribute("elementPos");
Boolean isFirstElement = request.getAttribute("isFirstElement");
Boolean isLastElement = request.getAttribute("isLastElement");
Integer numberOfElements = request.getAttribute("numberOfElements");

.

Answers (1)

Answers (1)

alemasetto
Participant
0 Kudos

To count elements or check the position you could query the ordered relation ElementsForSlot.

For example, you could try to get the first element of this query (sorted by sequencenumber DESC so the element on top is the last one)

select {target} from {ElementsForSlot} where {source} = '?pkOfTheSlot' order by {sequencenumber} DESC 

Just an idea... I hope it helps

adiputera
Active Participant

Thanks for the input,

With this will disregard the restrictions though, if there's no restrictions in place, I can use this.

For example, I have total of 20 banners, but only 10 would be shown due to some restrictions and the last one to be rendered is banner 15, the result of the query will still return banner 20

adiputera
Active Participant

Just found out OOTB implementation for this, you can check my answer