Conditionally update selection list when value in input field changes in a row of p:dataTable

StackOverflow https://stackoverflow.com/questions/23466011

  •  15-07-2023
  •  | 
  •  

Question

I need to update selection list based on value in input field. Because of the update delay I do not want to update the selection list if it already has correct options. Shouldn't be too difficult I thought and tried this:

<p:inputText value="#{bean.inputValue}"> 
      <p:ajax update="#{bean.updateElement}">               
</p:inputText> /
<h:selectOneMenu id="so" value="#{bean.soValue}">
      <f:selectItem />
      <f:selectItems value="#{bean.options}" var="option" itemLabel="#{option}"/>
</h:selectOneMenu>

But it does not work because bean.updateElement does not get called when value in input field changes.

OK I need to use p:remoteCommand then. I was sure it would work with this:

<p:ajax oncomplete="updateSelect()"/>

..

<p:remoteCommand name="updateSelect" update="dt:so" oncomplete="alert('done')" />

The fields are in a p:dataTable id="dt" row. I get the alert but bean.options is still not called. If I change it to update="dt" it works and the whole datatable is updated but that's not what I want. I also tried with so form:dt:so and :form:dt:so.

Then I tried a couple of more options I could find:

RequestContext.getCurrentInstance().update("dt:so");

..

FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("dt:so");

but nothing happens. Tried also without dt:.

Edit:

I got one step closer this way:

<p:ajax update="@this #{bean.updateElement}"/>

Now bean.updateElement gets called every time I change input field value but the select component does not update until the next time I change the input field value. bean.updateElement returns "so" or "" depending on input field value.

Edit 2:

Using this the selected option stays when input field is modified:

<f:ajax execute="so" render="so" partialSubmit="true" />

I also tried with <p:ajax process="so" update="so /> but the options are not any more updated. Keeping the selected option was one of my goals so this is good but could be better still.

How can I make this very simple thing work? My setup is this:

  • Tomcat 7.0.53
  • Mojarra 2.2.7
  • primefaces-5.0.jar
  • JRE 1.8.0_05
Was it helpful?

Solution 2

I'm using this ajax line:

<p:ajax partialSubmit="true" oncomplete="checkIt(#{iIndex})" />

This calls my JS function passing the row index to it. The function will check if the select list needs to be updated. If it does then I click this button from JavaScript:

<p:commandButton id="btnUpdateList" update="so" partialSubmit="true" style="display:none"/>

This way it works but list update is a bit slow, maybe because it goes to server two times.

OTHER TIPS

I am not sure, what your method updateElement does, but in

<p:ajax update="#{bean.updateElement}">

the attribute update should contain ids of the components to be updated. So maybe this would work for you:

<p:ajax listener="#{bean.updateElement}" update="so">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top