Alexei Stepanov
Alexei Stepanov

Reputation: 71

Wicket local model in radio

I've solved one problem with radio and got new one. I have my own custome radioGroup component:

public class OperationDateRadioGroup extends RadioGroup<OperationDate> {

    private LocalDatePicker date;
    private LocalDatePicker from;
    private LocalDatePicker to;
    public OperationDateRadioGroup(String id, IModel<OperationDate> model) {
        super(id, model);
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
        operationDateType = getModelObject().getOperationDateType();

        date = new LocalDatePicker("date", new CompoundPropertyModel<>(getModel()).bind("date")) {

            @Override
            protected void onConfigure() {
                super.onConfigure();
                setEnabled(OperationDateRadioGroup.this.getModelObject().getOperationDateType() == OperationDateType.DATE);
            }
        };
        from = new LocalDatePicker("interval.from", new CompoundPropertyModel<>(getModel()).bind("interval.from")) {

            @Override
            protected void onConfigure() {
                super.onConfigure();
                setEnabled(OperationDateRadioGroup.this.getModelObject().getOperationDateType() == OperationDateType.INTERVAL);
            }
        };
        to = new LocalDatePicker("interval.to", new CompoundPropertyModel<>(getModel()).bind("interval.to")) {

            @Override
            protected void onConfigure() {
                super.onConfigure();
                setEnabled(OperationDateRadioGroup.this.getModelObject().getOperationDateType() == OperationDateType.INTERVAL);
            }
        };

        OperationDate operationDate = new OperationDate();
        operationDate.setOperationDateType(OperationDateType.DATE);
        Radio dateType = new Radio<>("dateType", Model.of(operationDate));
        OperationDate operationInterval = new OperationDate();
        operationInterval.setOperationDateType(OperationDateType.INTERVAL);
        Radio intervalType = new Radio<>("intervalType", Model.of(operationInterval));

        dateType.add(new AjaxFormSubmitBehavior("onchange") {
            private static final long serialVersionUID = -6001312248222404593L;

            @Override
            protected void onEvent(AjaxRequestTarget ajaxRequestTarget) {
                ajaxRequestTarget.add(date, from, to);
            }
        });

        intervalType.add(new AjaxEventBehavior("onchange") {
            private static final long serialVersionUID = -6001312248222404593L;

            @Override
            protected void onEvent(AjaxRequestTarget ajaxRequestTarget) {
                ajaxRequestTarget.add(date, from, to);
            }
        });

        add(dateType, intervalType);
        add(date.setOutputMarkupId(true));
        add(from.setOutputMarkupId(true));
        add(to.setOutputMarkupId(true));
        add(new AjaxFormChoiceComponentUpdatingBehavior() {
            @Override
            protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {

            }
        });
        getForm().add(new DateIntervalFormValidator(this, date, from, to));
    }
}

And FormComponentPanel where I put my radioGroup

public class OperationDatePanel extends FormComponentPanel<OperationDate> {
 ....
    @Override
    protected void onInitialize() {
        super.onInitialize();
        add(new OperationDateRadioGroup("operationDate", getModel()));
    }
}

The problem: when I'm choosing radio in radioGroup changing inner(radioGroup) component and outer(Page) model, I need to change only radioGroup model and change outer model by submit.

Upvotes: 0

Views: 63

Answers (1)

KarelH
KarelH

Reputation: 61

I think, that problem is because you pass outer(Page) model into your OperationDateRadioGroup constructor

add(new OperationDateRadioGroup("operationDate", /*Here you are passing PAGE model*/ getModel()));

and it works with this page model. You need to pass any other model to constructor and work with this model ..

Upvotes: 1

Related Questions