在模态窗口关闭时刷新父页面的组件

时间:2011-04-19 16:42:46

标签: java wicket

我有一个模态窗口,模拟JavaScript确认框。根据按钮单击,取消按钮或确定按钮我想刷新父页面的某些组件。但不幸的是它没有用。从代码中可以看出,我想将两个文本字段的值设置为“”,隐藏面板并将选定的下拉选项设置为零。这种情况没有发生,组件仍然没有被刷新。

    public class ModalConfirmWindow extends WebPage {

    private ModalWindow modalConfirmWindow;
    private UserAssignmentInfoPanel userAssignmentInfoPanel;
    private DropDownChoice choice;
    private TextField scheduledTimeField;
    private DateTextField deadLineField;
    private int numberOfConflictingDays;

    public ModalConfirmWindow(ModalWindow modalConfirmWindow, UserAssignmentInfoPanel userAssignmentInfoPanel, DropDownChoice choice, TextField scheduledTimeField, DateTextField deadLineField, int numberOfConflictingDays) {
        this.modalConfirmWindow = modalConfirmWindow;
        this.userAssignmentInfoPanel = userAssignmentInfoPanel;
        this.choice = choice;
        this.scheduledTimeField = scheduledTimeField;
        this.deadLineField = deadLineField;
        this.numberOfConflictingDays = numberOfConflictingDays;
        add(new ModalConfirmWindowForm("form"));
    }

    private class ModalConfirmWindowForm extends Form {

        private static final long serialVersionUID = 10090L;

        public ModalConfirmWindowForm(String id) {
            super(id);

            add(new Label("value", " " + numberOfConflictingDays +" ").add(new SimpleAttributeModifier("style", "color: red")));
            add(new AjaxButton("cancelButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget target, Form form) {
                                            // I am trying to refresh the component here
                    String javaScript = "document.getElementById('"+ userAssignmentInfoPanel.getMarkupId() +"').display = 'none';" +
                                        "document.getElementById('"+ choice.getMarkupId() +"').options[0].selected = 'selected';" +
                                        "document.getElementById('"+ scheduledTimeField.getMarkupId() +"').value = \"\";" +
                                        "document.getElementById('"+ deadLineField.getMarkupId() +"').value = \"\";";                                       
                    //target.appendJavascript(javaScript);
                    //scheduledTimeField.add(new SimpleAttributeModifier("value", ""));
                    //target.addComponent(scheduledTimeField);
                    //modalConfirmWindow.close(target);
                    modalConfirmWindow.close(target);
                    target.appendJavascript(javaScript);
                }
            });
            add(new AjaxButton("okButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget target, Form form) {
                    modalConfirmWindow.close(target);
                }
            });
        }

    }
}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。我正在提供解决方案,以便它可以帮助未来的用户。 模态窗口页面是:

    public class ModalConfirmWindow extends WebPage {

    private Page parentPage;
    private ModalWindow modalConfirmWindow;    
    private int numberOfConflictingDays;

    public ModalConfirmWindow(Page parentPage, ModalWindow modalConfirmWindow, int numberOfConflictingDays) {
        this.parentPage = parentPage;
        this.modalConfirmWindow = modalConfirmWindow;        
        this.numberOfConflictingDays = numberOfConflictingDays;
        add(new ModalConfirmWindowForm("form"));
    }    

    private class ModalConfirmWindowForm extends Form {

        private static final long serialVersionUID = 10090L;

        public ModalConfirmWindowForm(String id) {
            super(id);

            add(new Label("value", " " + numberOfConflictingDays +" ").add(new SimpleAttributeModifier("style", "color: red")));
            add(new AjaxButton("cancelButton", this) {

                private static final long serialVersionUID = 10091L;

                @Override
                protected void onSubmit(AjaxRequestTarget target, Form form) {        
                    ((ParentPage)parentPage).setCancelButtonClicked(true);
                    modalConfirmWindow.close(target);
                }
            });
            add(new AjaxButton("okButton", this) {

                private static final long serialVersionUID = 10092L;

                @Override
                protected void onSubmit(AjaxRequestTarget target, Form form) {
                    ((ParentPage)parentPage).setCancelButtonClicked(false);
                    modalConfirmWindow.close(target);
                }
            });
        }

    }
}

这里ParentPage是生成模态窗口的页面。在ParentPage中我有一个字段isCancelButtonClicked及其getter和setter。在那个页面中我设置了

                modalConfirmWindow.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {

                    private static final long serialVersionUID = 10093L;

                    public boolean onCloseButtonClicked(AjaxRequestTarget target) {
                        ParentPage.this.isCancelButtonClicked = true;
                        return true;
                    }
                });
                modalConfirmWindow.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {

                    private static final long serialVersionUID = 10094L;

                    public void onClose(AjaxRequestTarget target) {
                        if (ParentPage.this.isCancelButtonClicked) {
                            String javascript = "document.getElementById('"+ scheduledTimeField.getMarkupId() +"').value = \"\";" +
                                                "document.getElementById('"+ deadLineField.getMarkupId() +"').value = \"\";" +
                                                "document.getElementById('"+ totalLabel.getMarkupId()+"').style.display = 'none';" +
                                                "document.getElementById('"+ choice.getMarkupId() +"').options[0].selected = 'selected';" +
                                                "document.getElementById('"+ choice.getMarkupId() +"').disabled = true;" +
                                                "document.getElementById('"+ userAssignmentInfoPanel.getMarkupId() +"').style.display = 'none';";
                            target.appendJavascript(javascript);
                        }                            
                    }
                });

瞧瞧。感谢。

答案 1 :(得分:0)

如果你使用wicket模型而不是javascript,你可能会有更好的运气。像这样的东西: @Override
protected void onSubmit(AjaxRequestTarget target, Form form) {
// I am trying to refresh the component here
userAssignmentInfoPanel.setVisible(false);
target.add(userAssignmentInfoPanel);
choice.getModelObject().setSelected(0);
target.add(choice);
scheduledTimeField.setModelObject("");
deadLineField.setModelObject("");
target.add(scheduledTimeField);
target.add(deadLineField);
}