Обновить компонент родительской страницы при закрытии модального окна - PullRequest
0 голосов
/ 19 апреля 2011

У меня есть модальное окно, окно подтверждения имитации 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 ]

1 голос
/ 20 апреля 2011

Я решил проблему. Я даю решение, чтобы оно могло помочь будущим пользователям. Страница модального окна:

    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 и его геттер и сеттер. На этой странице я установил

                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);
                        }                            
                    }
                });

И вуаля. Спасибо.

0 голосов
/ 19 апреля 2011

Вам, вероятно, повезет больше, если вы будете использовать модели калитки вместо javascript. Что-то вроде этого: @Override<br /> protected void onSubmit(AjaxRequestTarget target, Form form) {<br /> // I am trying to refresh the component here<br /> userAssignmentInfoPanel.setVisible(false);<br /> target.add(userAssignmentInfoPanel);<br /> choice.getModelObject().setSelected(0);<br /> target.add(choice);<br /> scheduledTimeField.setModelObject("");<br /> deadLineField.setModelObject("");<br /> target.add(scheduledTimeField);<br /> target.add(deadLineField);<br /> }

...