on 2023 Apr 04 11:03 AM
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.
Request clarification before answering.
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");
.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Just found out OOTB implementation for this, you can check my answer
User | Count |
---|---|
21 | |
19 | |
3 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.