0

I have a p:dataGrid with some images and want the user to select one of them

<h:form id="f1">
 <p:dataGrid var="image" value="#{bean.images}"
             columns="3" rows="1" paginator="true"
             paginatorTemplate="{PreviousPageLink} {PageLinks} {NextPageLink}">

  <h:graphicImage value="#{image.url}" width="200">
    <p:ajax event="click" listener="#{bean.selectImage(image.url)}"/>
  </h:graphicImage>

 </p:dataGrid>  
</h:form>

Unfortunately I get the method public void selectImage(String url) only invoked for the first element in the p:dataGrid (and it's possible to click this image multiple times). Im using PrimeFaces-4.0 and JBoss-7.2

Thor
  • 6,607
  • 13
  • 62
  • 96

1 Answers1

1

I am afraid <h:graphicImage> does not support ajax, you are probably better off wrapping it in a <p:commandLink>. But even though the only way I could force UICommand to invoke the right method was by using <f:setPropertyActionListener>

<p:dataGrid var="image" value="#{bean.images}"
         columns="3" rows="1" paginator="true"
         paginatorTemplate="{PreviousPageLink} {PageLinks} {NextPageLink}">

    <p:commandLink action="#{bean.selectImage()}" process="@this" update="@none">
      <h:graphicImage value="#{image.url}" width="200"/>
      <f:setPropertyActionListener target="#{bean.selectedImage}" value="#{image.url}" />
    </p:commandLink>

</p:dataGrid>  

BackingBean

public void selectImage()
{
    select(selectedImage);
    selectedImage = null;
}

private String selectedImage;
//Getter/Setter

BTW. you might want to read this post especially #4

Community
  • 1
  • 1
Kuba
  • 2,069
  • 23
  • 30
  • I cannot verify that h:graphicImage does not provide ajax. I tried your proposal using ``, but the problem is still the same: The method is only invoked if I click on the _first_ image! – Thor Feb 28 '14 at 10:06
  • have you tried setting selected image via `f:setPropertyActionListener` ? – Kuba Feb 28 '14 at 10:18
  • I believe Primefaces stated somewhere that their `` supported all components, but I would only rely on that to certain extent. – Kuba Feb 28 '14 at 10:20