2015-09-09 51 views
6

我有以下選擇一個菜單。但區分大小寫不起作用。當我按小A或大寫A時,它總是顯示小a(以先發生者爲準)。<p:selectOneMenu caseSensitive =「true」>似乎沒有任何效果

 <p:selectOneMenu id="tempSelect" caseSensitive="true"> 
         <f:selectItem itemLabel="0" itemValue="0"/> 
         <f:selectItem itemLabel="a" itemValue="a"/> 
         <f:selectItem itemLabel="A" itemValue="A"/> 
         <f:selectItem itemLabel="b" itemValue="b"/> 
     </p:selectOneMenu> 

我的primefaces版本是5.2。

當選擇框處於焦點。我按字母「a」或按大寫字母「A」,在兩種情況下,它只在框中顯示「a」(因爲它首先出現在列表中)。這是實際的行爲。

我的預期行爲是,當我按下「a」時,它會放上「a」,當我按下「A」時,它會將「A」放入框中。

我該怎麼辦?

+0

你能舉一個你的實際行爲和預期的例子嗎?你可以在按小A或大寫A時澄清你的意思,它總是顯示小(以先發生者爲準)。*?謝謝 –

+0

當選擇框在焦點。我按字母「a」或按大寫字母「A」,在兩種情況下,它只在框中顯示「a」(因爲它首先出現在列表中)。這是實際的行爲。 我的預期行爲是,當我按下「a」時,它將「a」,當我按下「A」時,它將「A」放入方框 –

+0

您可以嘗試使用自動完成http://www.primefaces.org/ showcase/ui/input/autoComplete.xhtml –

回答

6

Primefaces 5.2 documentation(頁430)說,關於大小寫敏感的選項:

定義是否過濾是區分大小寫。

因此,該選項僅適用於使用filter =「true」的情況,並且僅適用於在過濾器框中鍵入的值。

當您專注於SelectOneMenu控件並輸入一個值時,搜索將始終不區分大小寫,如您在source code of Primefaces (line 848)中所見。請注意,比較之前,文本將放置在LowerCase中。

return $(this).text().toLowerCase().indexOf(text.toLowerCase()) === 0; 

的一種方式(不是很優雅)你解決這個問題是重寫Primefaces功能負責此過濾器。請記住,在這種情況下,同一頁面中的其他SelectOneMenu控件也將區分大小寫。

因此,線848會變成這個樣子:

return $(this).text().indexOf(text) === 0; 

那或許應該考慮(如果你真的要覆蓋功能)是在line 842 of source code另一個細節,Primefaces丟棄有移位的所有條目鍵。

metaKey = e.metaKey||e.ctrlKey||e.shiftKey; 

因此,這條線也應該改變像波紋管,以方便大寫字母的條目:

metaKey = e.metaKey||e.ctrlKey; 

因此,考慮到這兩個變化,以及final version of Primefaces 5.2 (minified),解決的辦法是隻需添加以下代碼在SelectOneMenu之後的某處。

<script> 
    PrimeFaces.widget.SelectOneMenu.prototype.bindKeyEvents = function() { 
     var a = this; 
     this.focusInput.on("keydown.ui-selectonemenu", function(d) { 
      var c = $.ui.keyCode, b = d.which; 
      switch (b) { 
      case c.UP: 
      case c.LEFT: 
       a.highlightPrev(d); 
       break; 
      case c.DOWN: 
      case c.RIGHT: 
       a.highlightNext(d); 
       break; 
      case c.ENTER: 
      case c.NUMPAD_ENTER: 
       a.handleEnterKey(d); 
       break; 
      case c.TAB: 
       a.handleTabKey(); 
       break; 
      case c.ESCAPE: 
       a.handleEscapeKey(d); 
       break 
      } 
     }).on(
       "keyup.ui-selectonemenu", 
       function(g) { 
        var f = $.ui.keyCode, d = g.which; 
        switch (d) { 
        case f.UP: 
        case f.LEFT: 
        case f.DOWN: 
        case f.RIGHT: 
        case f.ENTER: 
        case f.NUMPAD_ENTER: 
        case f.TAB: 
        case f.ESCAPE: 
        case f.SPACE: 
        case f.HOME: 
        case f.PAGE_DOWN: 
        case f.PAGE_UP: 
        case f.END: 
        case f.DELETE: 
        case 16: 
        case 17: 
        case 18: 
        case 224: 
         break; 
        default: 
         var i = $(this).val(), c = null, h = g.metaKey 
           || g.ctrlKey; 
         if (!h) { 
          clearTimeout(a.searchTimer); 
          c = a.options.filter(function() { 
           return $(this).text() 
             .indexOf(i) === 0 
          }); 
          if (c.length) { 
           var b = a.items.eq(c.index()); 
           if (a.panel.is(":hidden")) { 
            a.selectItem(b) 
           } else { 
            a.highlightItem(b); 
            PrimeFaces.scrollInView(
              a.itemsWrapper, b) 
           } 
          } 
          a.searchTimer = setTimeout(function() { 
           a.focusInput.val("") 
          }, 1000) 
         } 
         break 
        } 
       }) 
    } 
</script> 

要進行測試,記得有每次擊鍵以清除已輸入的字母緩存,並開始一個新詞之間a timer of 1 second

+0

謝謝..工作:) –

2

雖然不是完美的解決方案,但您可以使用此解決方法。 使用過濾器。通過這種方式,你需要一個「點擊」,但它的工作原理。

<p:selectOneMenu id="tempSelect" caseSensitive="true" filter="true" filterMatchMode="startsWith"> <f:selectItem itemLabel="0" itemValue="0"/> <f:selectItem itemLabel="a" itemValue="a"/> <f:selectItem itemLabel="A" itemValue="A"/> <f:selectItem itemLabel="b" itemValue="b"/> </p:selectOneMenu>

相關問題