Zay hf
Zay hf

Reputation: 139

primefaces autocomplete when cleared field giving previuos results of page on clicking submit button

I am using an autocomplete tag of prime faces which retrieves results from a database.

The problem is that when I submit the form leaving the autocomplete field empty the results I get on the page are those of the previous request (the previously selected autocomplete value) - it only gets cleared when I refresh the page.

I want that on each submit, without refreshing the browser page, if i clear out the value in the field using backspaces and submit the form it should give the correct result for this particular instance, not previous one.

I am also using some textfields in the jsf page form but those don't have this problem. Can anyone offer guidance as to how this problem can be corrected?

EDITED: Code:

<h:form>
  <h:dataTable id="Ressult" value="#{input.searchResults}" var="r">
    <h:column>#{r.ID}</h:column>
    <h:column>#{r.Name}</h:column>
  </h:dataTable>
  <tr>
    <td>Current Education Level</td>
    <td>
      <h:panelGrid styleClass="text-box">
        <p:autoComplete id="education" value="#{input.education}"
        completeMethod="#{input.getautocomplete}" var="a"
        itemLabel="#{a.Name}" itemValue="#{a}"
        converter="edConverter" forceSelection="true" />
        <p:column>#{a.Name} - #{a.id}</p:column>
      </h:panelGrid>
    </td>
  </tr>
  <tr>
    <td>City</td>
    <td>
      <h:selectOneMenu id="txtCity" styleClass="select-field"
      value="#{input.cityId}">
        <f:selectItem itemLabel=" Please Select" itemValue="0">
        </f:selectItem>
        <f:selectItems value="#{input.cities}"></f:selectItems>
      </h:selectOneMenu>
    </td>
  </tr>
  <tr>
    <td>Name of Person</td>
    <td>
      <h:inputText id="txtName" value="#{input.nameOfPerson}"
      styleClass="text-box"></h:inputText>
    </td>
  </tr>
  <h:commandButton id="btnSearch" value="Search"
  action="#{input.searching}">
    <f:ajax execute="@form" render="Ressult"></f:ajax>
  </h:commandButton>
</h:form>  

And here is the bean code:

public class Input 
{
    private Education education;
    private List<SelectItem> cities;
    private Integer cityId;
    private String nameOfPerson;
    private List<Results> searchResults;
    //getters and setters

    public String searching()
    {
         searchResults=dao.getSearchResults(cityId,education,nameOfPerson);
         return "success";
    }

    public void autocomplete(String query)
    {
        //AUTOCOMPLTE lIST CODE HERE
    }
}

By update, if you mean new results to be shown when new items selected, then yes - the form should be updated but autocomplete somehow takes the previously selected value and shows results according to that. At least until I refresh the page - only then is autocomplete's previous is removed.

Upvotes: 0

Views: 10319

Answers (6)

Peter Š&#225;ly
Peter Š&#225;ly

Reputation: 2933

Just process event to server. Works with forceselection="true"

<p:ajax event="itemUnselect" global="false" />

Upvotes: 0

abhishek
abhishek

Reputation: 1

To fix the subject issue you just remove the forceselection

or make it as false.

Upvotes: 0

Benjamin Fuentes
Benjamin Fuentes

Reputation: 693

fixed on 5.2, upgrade your primefaces jar

here the log issue

https://code.google.com/p/primefaces/issues/detail?id=7592

Upvotes: 0

Ahmet
Ahmet

Reputation: 968

You may have two things to do:

  1. Prevent the user from submission by pressing the Enter key by doing the following in your form:

    <h:form onkeypress="return event.keyCode != 13;">
    
  2. Using itemSelect/itemUnselect features provided to empty the field in the Bean:

    <p:ajax event="itemSelect"
        listener="#{autoCompleteBean.handleSelect}" global="false"
        update=":some:where" />
    
    <p:ajax event="itemUnselect"
        listener="#{autoCompleteBean.handleUnselect}" global="false"
        update=":some:where" />
    
    <!-- rest of your facelet stuff -->
    

In the Bean:

public void handleSelect(final SelectEvent event) {
    final Search search = (Search) event.getObject();
    // do your addition here
}

public void handleUnselect(final UnselectEvent event) {
    final Search search = (Search) event.getObject();
    // do your substraction here
}

Upvotes: 1

Olivier Masseau
Olivier Masseau

Reputation: 31

I had the same problem with my autocomplete widget. When I removed its id attribute it worked. Maybe a bug in Primefaces.

Upvotes: 3

Tankhenk
Tankhenk

Reputation: 634

Well if i understand your question correctly your list of auto completion is shown after the post. And you use your form to submit time after time to the same page.

Your bean looks a little bit odd. Because you're calling in the page the autocomplete method: getautocomplete but that one doesn't exists in your bean.

Use the autocomplete in this way:

<p:autoComplete id="education" value="#{input.education}"  completeMethod="#{input.autocomplete}" var="a" itemLabel="#{a.Name}" itemValue="#{a}" converter="edConverter" forceSelection="true" />

And in your bean:

public List<Education> autocomplete(String query)
{
        List<Education> educations = new ArrayList<Education>();
        //search with query in your dao something like:
        educations = dao.searchEducation(query);

        return educations;
}

Upvotes: 0

Related Questions