【问题标题】:How to implement JQuery confirm dialog with JSF如何使用 JSF 实现 JQuery 确认对话框
【发布时间】:2012-05-26 06:23:14
【问题描述】:

当我按下按钮进入 JSF 页面时,我想使用 JQuery 对话框来确认操作执行(在我的情况下是确认删除行)。 我发现这段 JQuery 代码运行良好:

<html>
    <head>
        <title>jQuery UI Example Page</title>
        <link type="text/css" href="css/custom-theme/jquery-ui-1.8.18.custom.css" rel="stylesheet" />   
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
        <script type="text/javascript">
            $(function(){

                // Accordion
                $("#accordion").accordion({ header: "h3" });

                // Tabs
                $('#tabs').tabs();


                // Dialog           
                $('#dialog').dialog({
                    autoOpen: false,
                    width: 600,
                    buttons: {
                        "Ok": function() { 
                            $(this).dialog("close"); 
                        }, 
                        "Cancel": function() { 
                            $(this).dialog("close"); 
                        } 
                    }
                });

                // Dialog Link
                $('#dialog_link').click(function(){
                    $('#dialog').dialog('open');
                    return false;
                });

                // Datepicker
                $('#datepicker').datepicker({
                    inline: true
                });

                // Slider
                $('#slider').slider({
                    range: true,
                    values: [17, 67]
                });

                // Progressbar
                $("#progressbar").progressbar({
                    value: 20 
                });

                //hover states on the static widgets
                $('#dialog_link, ul#icons li').hover(
                    function() { $(this).addClass('ui-state-hover'); }, 
                    function() { $(this).removeClass('ui-state-hover'); }
                );

            });
        </script>
        <style type="text/css">
            /*demo page css*/
            body{ font: 62.5% "Trebuchet MS", sans-serif; margin: 50px;}
            .demoHeaders { margin-top: 2em; }
            #dialog_link {padding: .4em 1em .4em 20px;text-decoration: none;position: relative;}
            #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
            ul#icons {margin: 0; padding: 0;}
            ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
            ul#icons span.ui-icon {float: left; margin: 0 4px;}
        </style>    
    </head>
    <body>

        <!-- Dialog NOTE: Dialog is not generated by UI in this demo so it can be visually styled in themeroller-->
        <h2 class="demoHeaders">Dialog</h2>
        <p><a href="#" id="dialog_link" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-newwin"></span>Open Dialog</a></p>

        <!-- ui-dialog -->
        <div id="dialog" title="Dialog Title">
            <p>Dialog Test</p>
        </div>

    </body>
</html>

问题是我需要从这个按钮调用对话框到 Java Server Faces 页面:

<h:commandButton value="Delete" action="#{bean.deleteid}" >
    <f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>

你能帮我实现这个例子吗?

【问题讨论】:

    标签: java javascript jsf jsf-2 jquery-ui-dialog


    【解决方案1】:

    一个更简单的方法是不使用 jquery dailog 来确认消息,只需在 onclick 中使用普通的 javascript 确认框,无需重新发明轮子:

    <h:commandButton value="Delete" action="#{bean.deleteid}" onclick="return confirm('Are you sure you want to delete this?')">
        <f:ajax render="@form" execute="@form"></f:ajax>
    </h:commandButton>
    

    你必须添加 return 否则jsf会忽略用户在确认框中选择的内容,所以如果用户说点击取消,删除功能仍然会被执行。

    【讨论】:

      【解决方案2】:

      这是我使用的一种方法

      你需要两个按钮

      第一个将被隐藏并在确认对话框中单击Yes 后被调用,此隐藏按钮将调用服务器端方法并使用@987654326 执行render @

      <h:commandButton id="myHiddenButtonID" value="DeleteHiddenButton" action="#{bean.deleteid}" style="display:none">
          <f:ajax render="@form" ></f:ajax>
      </h:commandButton>
      

      现在到将打开对话框的按钮,此按钮还将使用execute="@form" 将表单提交到服务器(例如,如果您有选择列)

      <h:commandButton value="Delete">
          <f:ajax execute="@form" onevent="openDialogFunc()"></f:ajax>
      </h:commandButton>
      

      现在到js函数的实现

      function openDialogFunc(data){
          if (data.status === 'success') {
              $('#dialog').dialog({
                   autoOpen: false,
                   width: 600,
                   buttons: {
                       "Ok": function() { 
                           $("#myHiddenButtonID").click();
                           $(this).dialog("close"); 
                       }, 
                       "Cancel": function() { 
                           $(this).dialog("close"); 
                       } 
                   }
               });
          }
      }
      

      请注意,只有在单击“确定”对话框按钮时,才会执行隐藏按钮 $("#myHiddenButtonID").click();,否则不会发生任何事情...

      但我真的强烈建议你使用h:head 而不是head 和&lt;h:panelGroup 而不是div ...看看我之前的例子...jQuery Dialog in JSF

      【讨论】:

      • 感谢您的回答。您确定没有其他可能的解决方案直接调用ajax而不使用隐藏按钮?
      • INMO 你真的最好这样做,另一种选择是编写一些 js 代码来模拟 f:ajax 所做的相同的事情......我还没有看到“模仿 f :ajax with js 不止一次或两次”,所以隐藏按钮是最佳和安全的解决方案...
      • 我只是对如何实现这一点感兴趣。你能给我看一个快速的代码 sn-p 吗?
      • google for jsf.ajax.request google.com/… ,但是查看以下相关问题(尝试使用它的原因)stackoverflow.com/questions/9912735/… 可以查看推荐使用 f:ajax 的 cmets ...
      【解决方案3】:

      如果你必须调用它说点击命令按钮然后使用onclick事件

      <h:commandButton value="Delete" action="#{bean.deleteid}" onclick="return myjavascriptmethod">
          <f:ajax render="@form" execute="@form"></f:ajax>
      </h:commandButton>
      

      然后您可以在对话框中检查确认条件,点击 Okay 发送事件。

      根据评论编辑:

      当你不想使用 div 时,我只是使用了面板 Grid,你可以这样做:

      xhtml

      <h:panelGrid id="panelGridAsDialogTest" style="display:none;">
              <h:outputLabel value="I Am a dialog test" />
          </h:panelGrid>
      var alreadyValidated = false;
      function testJQueryDialog(buttonReference){
          if(!alreadyValidated) {                
          $('#panelGridAsDialogTest').dialog({
                  autoOpen: true,
                  width: 600,
                  buttons: {
                      "Ok": function(event) { 
                          $(this).dialog("close"); 
                          alreadyValidated = true;
                          jQuery(buttonReference).trigger("click");
                      }, 
                      "Cancel": function(event) { 
                          event.preventDefault();
                          $(this).dialog("close"); 
                      } 
                 }
             });
          }
          return alreadyValidated;
      }
      

      如果您想坚持使用 div 但让您的代码正常工作,您可以使用上面给出的相同 javascript,并将 id 替换为 div id。

      【讨论】:

      • 我添加了onclick 属性并且它有效。但是无论我点击什么按钮,代码都会被执行。我必须如何编辑代码才能停止精确?
      • 点击对话框中的“否”后,添加 event.preventDefault() 来解决您的问题
      • 所以在您的情况下,您的取消代码将如下所示: "Cancel": function(event) { $(this).dialog("close"); event.preventDefault(); } }
      • 它不工作。在我按下按钮后,该行被删除。该对话框不会阻止删除该行。
      • 但是我想问一个更重要的问题:如何删除这个 div &lt;div id="dialog" title="Dialog Title" style="display:none"&gt; &lt;p&gt;Dialog Test&lt;/p&gt; &lt;/div&gt; 并通过 onclick="return myjavascriptmethod" 传递对话框的内容?现在,如果我删除 div,对话框将不起作用。
      猜你喜欢
      • 2010-10-27
      • 2014-04-08
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2015-12-20
      • 2011-05-24
      相关资源
      最近更新 更多