2014-02-19 93 views
4

方案:我使用了自動完成的組件和infront我使用了一個命令鏈接與搜索圖像。Primefaces自動完成:如何在點擊命令鏈接後將焦點設置爲自動完成

我的目的是在點擊commandlink後在自動完成中顯示光標。

我的代碼如下:

自動完成:

<p:autoComplete id="senderAutocomplete" widgetVar="senderInfo" 
                value="#{customerReviewLazyDataModel.customerReviewVO.senderVO}" 
                completeMethod="#{customerReviewLazyDataModel.searchOrganizations}" 
                var="senderVO" 
                itemValue="#{senderVO}" converter="organizationConverterForCustomerReview" 
                size="60"> 

commandlink

<p:commandLink oncomplete="setFocus()" ajax="true"> 
                 <p:graphicImage value="/app-resources/themes/yob/images/search.png" style="margin-bottom: -4px;"></p:graphicImage> 
                </p:commandLink> 

js函數

function setFocus(){ 
     document.getElementById("senderAutocomplete").focus(); 
} 

有沒有解決這個問題的方法?

回答

0

您用於獲取輸入焦點的DOM元素的ID是錯誤的。首先你必須考慮表單的ID。此外,p:autoComplete的輸入的id具有後綴_input。因此,使用這樣的:

document.getElementById("formId:senderAutocomplete_input").focus(); 

您還應該設置process="@this"commandButton,因爲你只是用它來集中autoComplete

+1

謝謝。你的答案解決了我的問題。我用'document.getElementById(「senderAutocomplete_input」)。focus();'這和它工作得很好。 – Shruti

0

我解決了這個問題,添加一個css樣式類,並使用jquery(它包含在Primefaces中)來設置焦點。

<p:autoComplete id="senderAutocomplete" widgetVar="senderInfo" ... 

*styleClass="focusme"* 
/> 

而且commandlink會是這樣:

<p:commandLink *oncomplete="$('.focusme').focus()"* ajax="true">