Newbie
Newbie

Reputation: 1724

Primefaces ajax handle empty input field

I have an autocomplete field and an output label to display the title of the selected module code

<p:row>
    <p:column>
        <p:outputLabel for="moduleTitle" value="Module Title: " />
    </p:column>
    <p:column colspan="2">
        <p:outputLabel id="moduleTitle" value="#{classroomBean.classroom.module.moduleTitle}"/>
    </p:column>
</p:row>

<p:row>
    <p:column>
        <p:outputLabel for="moduleCode" value="Module Code:" />
    </p:column>
    <p:column>
        <p:autoComplete id="moduleCode" value="#{classroomBean.classroom.module}" 
completeMethod="#{classroomBean.completeModule}" dropdown="true" 
forceSelection="true" 
converter="entityConverter" maxResults="15" required="true" 
requiredMessage="Module is required." var="module" itemValue="#{module}" itemLabel="#{module.moduleCode}">
            <p:ajax event="itemSelect" update="moduleTitle" />
        </p:autoComplete>
    </p:column>
</p:row>

This works fine, but what I want is clear the moduleTitle field when the autocomplete field is empty. Any idea? I tried to add <p:ajax event="keyup" listener="#{...}" /> but the backing bean method wasn't called. And I also have no idea on how to get the string length of autocomplete field from ajax.

Upvotes: 0

Views: 824

Answers (1)

Parkash Kumar
Parkash Kumar

Reputation: 4730

Add p:ajax with keyup event along with p:ajax of itemSelect event on your p:autoComplete component to update the respect fields as following:

<p:autoComplete id="moduleCode" value="#{classroomBean.classroom.module}"
    completeMethod="#{classroomBean.completeModule}" dropdown="true"
    forceSelection="true" converter="entityConverter" maxResults="15"
    required="true" requiredMessage="Module is required."
    var="module" itemValue="#{module}" itemLabel="#{module.moduleCode}">
    <p:ajax event="keyup" update="moduleTitle" />
    <p:ajax event="itemSelect" update="moduleTitle" />
</p:autoComplete>

Upvotes: 1

Related Questions