Raw DataTable, lazy loading and CommandButton for each row

I have this simple page:

<h:form id="form"> <p:dataTable value="#{testBean.unitTypeModel}" var="elem" lazy="true" rows="10"> <p:column headerText="class">#{elem.class.simpleName}</p:column> <p:column headerText="code">#{elem.code}</p:column> <p:column headerText="description">#{elem.description}</p:column> <p:column headerText="action"> <p:commandButton action="test2" icon="ui-icon ui-icon-wrench" value="edit"> <f:setPropertyActionListener target="#{testBean.selection}" value="#{elem}"/> </p:commandButton> </p:column> </p:dataTable> <p:commandButton action="test2" icon="ui-icon ui-icon-wrench"/> </h:form> 

and the CommandButton inside the DataTable does not work, just refreshes the page. but it works outside.

if I change value and lazy as follows:

 <h:form id="form"> <p:dataTable value="#{testBean.unitTypeModel.load(0, 10, null, null, null)}" var="elem" lazy="false" rows="10"> <p:column headerText="class">#{elem.class.simpleName}</p:column> <p:column headerText="code">#{elem.code}</p:column> <p:column headerText="description">#{elem.description}</p:column> <p:column headerText="action"> <p:commandButton action="test2" icon="ui-icon ui-icon-wrench" value="edit"> <f:setPropertyActionListener target="#{testBean.selection}" value="#{elem}"/> </p:commandButton> </p:column> </p:dataTable> <p:commandButton action="test2" icon="ui-icon ui-icon-wrench"/> </h:form> 

CommanButton inside the DataTable works like a charm.

Does anyone know why?

this is mistake?

I am

  • Glassfish 3.1.2
  • JSF 2.1.11 (Mojarra)
  • PrimeFaces 3.4-SNAPSHOT
+6
source share
2 answers

found out that the lazy datamodel should be the same instance upon request of the postback, even a new instance with the same values ​​will not work. therefore, it should be provided, at least with the help of the @ViewScoped bean.

+7
source

Four years have passed since the publication of this question, but the problem still remains in PrimeFaces 6.0.

I am going to publish a workaround for those who do not want (or cannot) use ViewScoped beans.

The premise is: "you cannot put any" ajaxified "element in the lazy data-bound binding of RequestScoped stuff." Never. Keep in mind that anything that challenges ajax will never work.

So, the first step is to call ajax outside of the datatable. We will do this using RemoteComand. You can put this RemoteCommand somewhere outside the DataTable (inside the form, of course)

 <p:remoteCommand name="remoteCall" action="#{bean.doStuff()}"> </p:remoteCommand> 

Now all we need to do is call this RemoteCommand from the DataTable. I use a link to make a javascript call, but you can use a button or whatever:

 <p:column> <p:link value="#{item.id}" href="#" onclick="remoteCall([{name:'id', value:#{item.id}}])"> </p:link> </p:column> 

This link makes it possible to call the javascript function "remoteCall", which will make an ajax call to "bean.doStuff ()".

Please note that the onClick event not only contains a javascript call for "remoteCall", but also contains an array of parameters with one parameter named "id" with the value "# {item.id}". This will allow RemoteCommand to send the parameter named "id" to the backup bean.

Inside the doStuff method, you will need to get the id parameter value:

 public void doStuff () { String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"); } 
+1
source

Source: https://habr.com/ru/post/922651/


All Articles