2

ページにドロップダウンコンポーネントを追加しました。このドロップダウンの目的は、レンダリングされる入力フォームのタイプを変更することです。たとえば、フォームが異なれば、必須フィールド、編集可能フィールドなども異なります。

public final class Test extends WebPage
{

    CustomPanel currentPanel = new MeRequest("repeater",FormType.MIN);

    public Test(PageParameters parameters)
    {
        add(currentPanel);         

        DropDownChoice ddc = new DropDownChoice("panel", new PropertyModel(this, "selected"), panels, choiceRenderer);
        ddc.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println("changed");
                currentPanel = new MeRequest("repeater",FormType.PRO);
                target.add(currentPanel);
            }
        });
        add(ddc);
    }

さまざまなオプションを試しましたが、結果は限られています。唯一の本当の成功はモデルの更新ですが、私が本当にやりたいのは、コンポーネントの動作を変更することです。

私が欠けているものについて何か考えはありますか?

4

1 に答える 1

6

1)あるパネルを別のパネルに交換したい場合は、次のようにするだけです。

まず、元のパネルのマークアップIDを出力する必要があります。

currentPanel.setOutputMarkupId(true);

そして、ajaxイベントハンドラーに次のように記述します。

protected void onUpdate(AjaxRequestTarget target) {
    CustomPanel newPanel = new MeRequest("repeater", FormType.PRO);
    currentPanel.replaceWith(newPanel);
    currentPanel = newPanel;
    currentPanel.setOutputMarkupId(true);
    target.addComponent(currentPanel);
}

この場合、ドロップダウンの選択を変更するたびに、ページに新しいパネルを追加し、ページから古いパネルを削除します。

2)しかし、私はあなたの問題に対して少し異なるアプローチを提案します。パネルの構築ロジックをonBeforeRender()メソッドに移動する必要があります。

public class MeRequest extends Panel {

    private FormType formType;

    public MeRequest(String id, FormType formType) {
        super(id);
        this.formType = formType;

        // don't forget to output the markup id of the panel
        setOutputMarkupId(true);

        // constructor without construction logic
    }

    protected void onBeforeRender() {
        // create form and form components based on value of form type
        switch (formType) {
            case MIN:
                // ...
                break;
            case PRO:
                // ...
                break;
        }            

        // add form and form components to panel
        addOrReplace(form);
        form.add(field1);
        form.add(field2);
        // ...

        super.onBeforeRender();
    }

    public void setFormType(FormType formType) {
        this.formType = formType;
    }
}

そうすると、ajaxイベントでパネルのタイプのみを変更できるようになります。

protected void onUpdate(AjaxRequestTarget target) {        
    currentPanel.setFormType(FormType.PRO);
    target.addComponent(currentPanel);
}

したがって、元のパネルを再作成せずに再構築しました。

于 2012-10-17T20:47:46.610 に答える