Search code examples
jsfdynamicforeachprimefacesdatascroller

Primefaces dataScroller with dynamic id for child component


I'm trying to enhance a portion of code in my application. I'm currently listing a set of comments with a code like this:

   <c:forEach items="{myBean.items}" var="it" varStatus="st">
    <p:panel id="child_#{st.index}">...</p:panel>
    ...
   </c:forEach>

this allows me to update every single panel referring to it's ID which using <c:forEach/> I can assume is unique. But this is somehow a rigid way of doing it because it obligues me to load the whole set of items at once. I would like to implement something like <p:dataScroller> in order to make it lazy and been able to load on demand, but if I use it something like this:

   <p:dataScroller value="{myBean.items}" rowIndexVar="st" var="it">
    <p:panel id="child_#{st}">...</p:panel>
    ...
   </p:dataScroller>

I get a "Cannot find component for expression child_ ..." error, which I understand shows up because the value of 'st' is not available yet in the JSF phase in which the panel component is rendered. Is there any possible way to achieve this? Either still using c:forEach o adapting my code with <p:dataScroller/> ?

Thanks.


Solution

  • p:dataScroller is a UIData component and will take care of creating unique ids for its children by prefixing a row index. So you can simply use <p:panel id="child">. In the rendered HTML the id will be something like formId:dataScrollerId:0:child for the first row, formId:dataScrollerId:1:child for the second row, and so on.