Wicket DropDownChoice从值返回整数

时间:2018-03-27 07:51:35

标签: integer wicket dropdownchoice

在这里你看到了一个wicket DropDownChoice的工作示例,但这不是我想要的。 dropdownchoice的返回值应该是与该值匹配的整数。下拉选项的返回值为:

returnedLabel=123 (String)
returnedDDCValue=LabelValue [label=TestB, value=3] (LabelValue)

对于生产returnedDDCValue应该是一个整数,填充(整数)3。 我认为必须在ddc内完成转换。我试过IChoiceRenderer,但没有成功。 Plz帮助:)

干杯

克里斯

public class DDCTest extends Panel {

    private List<LabelValue> list4ddc;

    public DDCTest(String id) {
        super(id);
        Result result = new Result();

        final FeedbackPanel feedback = new FeedbackPanel("feedback");
        feedback.setOutputMarkupId(true);
        add(feedback);

        this.list4ddc = new ArrayList<LabelValue>();
        this.list4ddc.add(new LabelValue("TestA", 1));
        this.list4ddc.add(new LabelValue("TestB", 3));
        this.list4ddc.add(new LabelValue("TestC", 5));
        this.list4ddc.add(new LabelValue("TestD", 7));

        Form form = new Form("form");
        add(form);

        form.add(new TextField<String>("returnedLabel", new PropertyModel<String>(result,"returnedLabel")));

        form.add( new DropDownChoice("returnedDDCValue", new PropertyModel(result,"returnedDDCValue"), Model.of(list4ddc), new ChoiceRenderer("label", "value")));

        form.add(new AjaxButton("submit", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                info("["+result.toString()+"]");
                target.add(feedback);
            }
        });

    }

    public class Result implements Serializable {
        private String returnedLabel;
        private String returnedDDCValue;

        public Result() {
            super();
            this.returnedLabel = "";
            this.returnedDDCValue = null;
        }

        [ ... Getters and setters ... ]

    }

    public class LabelValue implements Serializable {
        private String label;
        private Integer value;

        public LabelValue(String label, Integer value) {
            super();
            this.label = label;
            this.value = value;
        }

        [ ... Getters and setters ... ]
    }
}

1 个答案:

答案 0 :(得分:1)

我想我已经做了一些非常类似于你需要的事情。它是一个自定义DDC,它使用选择的值来设置第三个模型:

public class DropDownChoiceForString<T> extends DropDownChoice<T> {

private IModel<String> targetModel;

public DropDownChoiceForString(String id, IModel<T> model, IModel<String> targetModel, 
        List<? extends T> choices,
        IChoiceRenderer<? super T> renderer) {
    super(id, model, choices, renderer);
    this.targetModel = targetModel;
}

protected DropDownChoiceForString(String id, IModel<T> model, IModel<String> targetModel) {
    this(id, model, targetModel, Collections.<T> emptyList(), null);
}

@Override
protected void onInitialize() {
    super.onInitialize();
    // load the initial choice.
    setModelObject(convertChoiceIdToChoice(targetModel.getObject()));
}

@Override
protected void onDetach() {
    super.onDetach();

    targetModel.detach();
}

@Override
protected void onModelChanged() {
    super.onModelChanged();

    T newSelection = getModelObject();

    int choiceIndex = getChoices().indexOf(newSelection);
    // update the string source with the selected value.
    targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, choiceIndex));
}}

它的工作方式与标准DDC类似,但是当其模型更改时,目标模型将使用新选项的值进行更新。