2014-02-19 42 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">