2015-06-30 71 views
2

我們有一個p:inputText元素,它應該爲各種選項顯示覆蓋面板。 (它是一種全局搜索,所以你可以勾選類別來搜索)Primefaces:輸入文字/覆蓋面板/保留焦點

用戶通常單擊文本框,開始打字,然後看着屏幕再次

的問題是:一旦覆蓋面板顯示,文本框失去了重點。

<p:inputText id="searchItem"></p:inputText> 
<p:overlayPanel id="gsOverlay" for="searchItem" my="left top" 
at="left bottom" dynamic="true" 
onShow="resizeGSOverlay();"> 

所以我試圖用

<p:overlayPanel id="gsOverlay" for="searchItem" my="left top" 
at="left bottom" dynamic="true" 
onShow="PrimeFaces.focus('globalSearchForm:searchItem'); resizeGSOverlay();"> 

然而,爲了解決這個問題,通過立即回到集中在「搜索」 inputtext,有一瞬間,當inputfield失去重心,導致搜尋失蹤第一個特徵。

我可以顯示覆蓋面板,而不會使輸入文字失去焦點嗎? (覆蓋面板內每個組件將點擊後重心轉回,這是速度不夠快 - 只是最初的重點回是慢)

回答

2

剛剛發現的「聖盃」:

默認爲overlayPanel是:

PrimeFaces.widget.OverlayPanel.prototype.applyFocus = function(){ 
     this.jq.find(':not(:submit):not(:button):input:visible:enabled:first').focus(); 
    } 

所以,我只是把下面的JavaScript之後,包括primefaces資源,那麼這將覆蓋默認的實現:

<script type="text/javascript"> 
    PrimeFaces.widget.OverlayPanel.prototype.applyFocus = function() { 
     if (this.id == "globalSearchForm:gsOverlay") 
      return; 
     else 
      this.jq.find(':not(:submit):not(:button):input:visible:enabled:first').focus(); 
    } 
</script> 

因此 - 一旦可見,覆蓋面板中的任何元素都不會聚焦。奇蹟般有效。


更新:使用 代理模式(http://api.jquery.com/Types/#Proxy_Pattern)似乎是一種更可靠的解決方案,因爲它避免了需要複製原來實行的內容,這可能會在未來Primefaces版本之一是不同的:

<script type="text/javascript"> 
    (function() { 
     var proxied = PrimeFaces.widget.OverlayPanel.prototype.applyFocus; 

     PrimeFaces.widget.OverlayPanel.prototype.applyFocus = function(){ 
      if (this.id == "globalSearchForm:gsOverlay") 
       return; 

      return proxied.apply(this, arguments); 
     }; 
    })(); 
</script> 
+2

作爲PF源的聖盃......很棒...更多人應該這樣做。 – Kukeltje