代码之家  ›  专栏  ›  技术社区  ›  Noah Martin

如何在ESC按钮上调用托管bean方法?

  •  1
  • Noah Martin  · 技术社区  · 11 年前

    当我从键盘上按ESC键时,我想从我的托管bean中执行一个方法。我读过其他一些帖子,但没有一篇对我的应用程序有效。也许我没有把脚本放在页面的正确位置。。我把它放在了dhe af:document上面(它是一个ADF应用程序),也放在了af:document。这是JS代码:

    <af:resource type="javascript">
          $(document).keyup(function (e) {
              if (e.which == 27) {
                  document.getElementById('cb3').click();
              }
          });
        </af:resource>
    

    “cb3”是我的页面上的一个按钮的ID,该按钮从我的bean调用该方法。我不知道直接调用该方法的其他方法。 知道吗?

    3 回复  |  直到 11 年前
        1
  •  2
  •   Amr Gawish    11 年前

    你应该有这样的东西

    <af:document title="Press ESC to do some action" id="d1">
      <f:facet name="metaContainer">
        <af:resource type="javascript">
           function onKeyPress(evt){
             var _keyCode = evt.getKeyCode();
             if (_keyCode == AdfKeyStroke.ESC_KEY ){    
                  var button = AdfPage.PAGE.findComponentByAbsoluteId('cb1');
                  AdfActionEvent.queue(button,true);
                  evt.cancel();
             }
         }
        </af:resource>
       </f:facet>
       <af:commandButton text="Execute when ESC key is pressed" clientComponent="true" id="cb1" actionListener="#{someScope.someFunction}" />
       <af:clientListener method="onKeyPress" type="keyPress"/>
     </af:document>
    

    这将为文档创建一个客户端侦听器,该侦听器将在任何按键上执行,它将侦听ESC键,如果发现它将执行按钮执行的任何操作!

        2
  •  1
  •   Noah Martin    11 年前

    我感谢@Gawish的回复,因为它帮助我找到了解决方案。我无法使用该解决方案,因为ADF 11g中的clientListener中没有类型:“keyPress”。然而,我确实喜欢这样,而且效果很好:

    window.onkeyup = function (e) {
              if (e.keyCode == 27) {
                  var button = AdfPage.PAGE.findComponentByAbsoluteId('cb3');
                  AdfActionEvent.queue(button, true);
                  e.cancel();
              }
          }
    

    请注意,结尾的e.cancel()是必须的!

        3
  •  0
  •   Divy Kumar    9 年前

    还有另一种解决方案: 添加 <af:clientListener method="handleESCaction" type="popupCanceled"/>

    在您的adf组件中,但请确保在组件中添加clientComponent=true。