Buddhika Ariyaratne
Buddhika Ariyaratne

Reputation: 2423

onkeydown event triggering clicking a command button

There are several primefaces inputtext controls and commandbuttons in a single form. Pressing the return ket on one inputtext need to activate the click of related commandbutton. How can I achieve that functionality? I tried to use onkeydown even, but could not find out the method.

Upvotes: 0

Views: 2996

Answers (2)

Buddhika Ariyaratne
Buddhika Ariyaratne

Reputation: 2423

As guided by RongNK, I changed my code as below and it serves my purpose perfectly.

Added CDATA to include JavaScript. Used \\ to escape : in id of JSF compoents

  <h:form id="frmEn">
                <script type="text/javascript" language="JavaScript">
                    //<![CDATA[    
                    function forDx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnDx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forIx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnIx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forMx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnMx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    function forRx(e) {
                        if (e.keyCode === 13) {
                            $('#frmEn\\:btnRx').click();
                            return false;
                        } else {
                            return true;
                        }
                    }
                    // ]]>
                </script> 

                <p:panel header="Encounter" >
                    <h:panelGrid columns="2" >
                        <p:panel header="Encounter Details" >

                            <h:panelGrid columns="4" >


                                <h:outputLabel value="Tests" ></h:outputLabel>
                                <p:autoComplete  id="txtIx"  value="#{encounterController.test }" completeMethod="#{encounterController.completeIx}"  styleClass="defaultTxt" onkeydown="return forIx(event)" >
                                </p:autoComplete>
                                <h:commandButton id="btnIx"  value="Add" action="#{encounterController.addTest()}">
                                    <f:ajax  execute="btnIx txtIx" render="tblIx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecIxs }" var="ix" id="tblIx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Tests"/>
                                        </f:facet>
                                        <h:outputLabel value="#{ix.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>






                                <h:outputLabel value="Diagnosis" ></h:outputLabel>
                                <p:autoComplete  id="txtDx"  value="#{encounterController.diagnosis }" completeMethod="#{encounterController.completeDx}"  styleClass="defaultTxt" onkeydown="return forDx(event)" />
                                <h:commandButton id="btnDx"  value="Add" action="#{encounterController.addDiagnosis()}" >
                                    <f:ajax  execute="btnDx txtDx" render="tblDx txtDx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecDxs }" var="dx" id="tblDx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Diagnoses"/>
                                        </f:facet>
                                        <h:outputLabel value="#{dx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>



                                <h:outputLabel value="Treatment" ></h:outputLabel>
                                <p:autoComplete  id="txtRx"  value="#{encounterController.rx}" completeMethod="#{encounterController.completeRx}"  styleClass="defaultTxt" onkeydown="return forRx(event)">
                                </p:autoComplete>

                                <h:commandButton id="btnRx"  value="Add" action="#{encounterController.addRx()}">
                                    <f:ajax  execute="btnRx txtRx" render="tblRx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecRxs }" var="rx" id="tblRx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Treatment"/>
                                        </f:facet>
                                        <h:outputLabel value="#{rx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>


                                <h:outputLabel value="Plan" ></h:outputLabel>
                                <p:autoComplete  id="txtMx"  value="#{encounterController.plan }" completeMethod="#{encounterController.completeMx}"  styleClass="defaultTxt" onkeydown="return forMx(event)">
                                </p:autoComplete>

                                <h:commandButton id="btnMx"  value="Add" action="#{encounterController.addPlanOfAction() }">
                                    <f:ajax  execute="btnMx txtMx" render="tblMx" />
                                </h:commandButton>
                                <p:dataTable value="#{encounterController.ecMxs}" var="mx" id="tblMx" >
                                    <p:column >
                                        <f:facet name="header" >
                                            <h:outputLabel value="Plan"/>
                                        </f:facet>
                                        <h:outputLabel value="#{mx.concept.name}"></h:outputLabel>
                                    </p:column>
                                </p:dataTable>


                                <h:outputLabel value="Details" ></h:outputLabel>
                                <h:inputTextarea value="#{encounterController.current.comments}" styleClass="defaultTxtArea"></h:inputTextarea>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>

                                <h:outputLabel value="Charges" ></h:outputLabel>
                                <h:inputTextarea value="#{encounterController.current.charge}" styleClass="defaultTxt"></h:inputTextarea>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>

                                <h:outputLabel value=""></h:outputLabel>
                                <h:commandButton value="Save" action="#{encounterController.saveSelected()}"></h:commandButton>
                                <h:outputLabel value="" ></h:outputLabel>
                                <h:outputLabel value="" ></h:outputLabel>
                            </h:panelGrid>


                        </p:panel>
                        <p:panel header="Patient Details" >
                            <h:panelGrid columns="2" >
                                <h:outputLabel value="Name"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.name}"></h:outputLabel>

                                <h:outputLabel value="Age"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.age}"></h:outputLabel>

                                <h:outputLabel value="Date of Birth"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.dob}">
                                    <f:convertDateTime pattern="dd MMMM yyyy" />
                                </h:outputLabel>

                                <h:outputLabel value="Sex"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.sex.name}"></h:outputLabel>

                                <h:outputLabel value="Address"></h:outputLabel>
                                <h:outputLabel value="#{encounterController.current.patient.person.address}"></h:outputLabel>

                            </h:panelGrid>
                        </p:panel>
                    </h:panelGrid>

                </p:panel>

            </h:form>

Upvotes: 1

Rong Nguyen
Rong Nguyen

Reputation: 4189

You can try(you need to find to detect enter key in javascript by yourself)

xhtml:

<p:input onkeydown="test()" />
<p:commandButton styleClass="foo" />

javascript:

function test(){
$('.foo').click();
}

You can see also: How-to-programmatically-trigger-onclick-event and How-to-refer-to-a-jsf-component-id-in-jquery

Upvotes: 1

Related Questions