有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何用JSF实现JQuery确认对话框

当我在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>

你能帮我实现这个例子吗


共 (1) 个答案

  1. # 1 楼答案

    这里是我使用的一种方法

    你需要两个按钮

    第一个按钮将被隐藏,并在确认对话框中单击Yes后被调用。这个隐藏按钮将调用服务器端方法,并使用f:ajax执行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"); 
                     } 
                 }
             });
        }
    }
    

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

    但是我真的强烈建议你用h:head代替head,用<h:panelGroup代替div。。。看看我之前的例子jQuery Dialog in JSF