3

I have two autocomplete fields as below. I swap the autocomplete values using a command button. I need to persist the swapped values.

    <p:column colspan="2" rendered="#{empty contactController.selectedContact}">
         <p:autoComplete id="contactName1" value="#{newContactRs.contact}"
                         completeMethod="#{contactRelationshipController.completeContacts}"
                         var="contact" itemLabel="#{contact.fullName}"
                         itemValue="#{contact}" converter="#{contactConverter}"
                         forceSelection="true" size="35" scrollHeight="200"
                         panelStyle="width:10px;">
              <p:ajax event="itemSelect" update="contactName1" />
          </p:autoComplete>
          <p:commandButton icon="ui-icon-arrow-2-e-w" id="flipButton" 
                           styleClass="ui-panel-titlebar-icon" oncomplete="swapInput()"              update="addNewContactRelationshipPanel" immediate="true"/>
          <smith:contactSelector value="#{newContactRs.contact}" update=":addContactRelationshipForm:addNewContactRelationshipPanel" triggerId="existingContactSelector1" resultList="#{contactRelationshipController.selectAllContact1}"/>
   </p:column>

   <p:column colspan="2">
        <p:autoComplete id="contactName2" value="#{newContactRs.relatedContact}"
                        completeMethod="#{contactRelationshipController.completeContacts}"
                        var="contact" itemLabel="#{contact.fullName}"
                        itemValue="#{contact}" converter="#{contactConverter}"
                        forceSelection="true" size="35" scrollHeight="200"
                        panelStyle="width:10px;">
             <p:ajax event="itemSelect" update="contactName2" />
         </p:autoComplete>
    <smith:contactSelector value="#{newContactRs.relatedContact}" update=":addContactRelationshipForm:addNewContactRelationshipPanel" triggerId="existingContactSelector2" resultList="#{contactRelationshipController.selectAllContact2}"/>
    </p:column>

On clicking the commandbutton with id "flipButton" following javascript is called and it swaps the values of the autocomplete fields

function swapInput()
{
    var input_a =document.getElementById('addContactRelationshipForm:contactName1_input').value; 
    var input_b =document.getElementById('addContactRelationshipForm:contactName2_input').value;

    document.getElementById('addContactRelationshipForm:contactName1_input').value = input_b;
    document.getElementById('addContactRelationshipForm:contactName2_input').value = input_a;
}

After swapping the values am trying to persist the swapped values but original values are persisting instead of swapped values. I have to set the swapped values in the backing bean but am not sure how to do it, using javascript or primefaces.

2 Answers 2

1

You could use Preimefaces p:remoteCommand to execute managed bean methods. https://www.primefaces.org/showcase/ui/ajax/remoteCommand.xhtml

Sign up to request clarification or add additional context in comments.

Comments

0

Change the commandButton as

<p:commandButton icon="ui-icon-arrow-2-e-w" 
                 id="flipButton" 
                 styleClass="ui-panel-titlebar-icon" 
                 oncomplete="swapInput()" 
                 update="addNewContactRelationshipPanel" 
                 immediate="true" 
                 process="@this" 
                 actionListener="#{contactRelationshipController.setSwapInputFlag()}"/>

and then check for the swapInputFlag while persisting the autocomplete fields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.