DropDownChoice 设置选定的特定索引

发布于 2024-11-04 05:41:59 字数 847 浏览 1 评论 0原文

我有一个 DropDownChoice :

       DropDownChoice timePeriod = new DropDownChoice("timePeriod", Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

            private static final long serialVersionUID = 10102L;

            @Override
            public String getIdValue(Object object, int index) {
                return ((TimePeriod) object).getId() + "";
            }

            @Override
            public Object getDisplayValue(Object object) {
                return ((TimePeriod) object).getPeriodType();
            }
        });
        timePeriod.setNullValid(false);

我的问题是:

  1. 如何将所选索引设置为 2 或任何其他索引?
  2. 如何删除第一个默认空白选项?

谢谢。

I have a DropDownChoice :

       DropDownChoice timePeriod = new DropDownChoice("timePeriod", Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

            private static final long serialVersionUID = 10102L;

            @Override
            public String getIdValue(Object object, int index) {
                return ((TimePeriod) object).getId() + "";
            }

            @Override
            public Object getDisplayValue(Object object) {
                return ((TimePeriod) object).getPeriodType();
            }
        });
        timePeriod.setNullValid(false);

My question is:

  1. How to set the selected index to 2 or any other?
  2. How to remove first default blank option?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-11-11 05:42:01

DropDownChoice 有一个接受默认值的构造函数。
或者在您的代码中,您可以添加 timePeriod.setModel(Model.of(new TimePeriod(2, "Bi-Weekly")))

我猜 TimePeriod 有正确的 #equals() 和 #hashCode() 实现。

关于空白选项:参见 org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.isNullValid()

DropDownChoice has a constructor which accepts the default value.
Or in your code you can add timePeriod.setModel(Model.of(new TimePeriod(2, "Bi-Weekly")))

I guess TimePeriod has proper #equals() and #hashCode() implementations.

About the blank option: see org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.isNullValid()

几度春秋 2024-11-11 05:42:01

托加莫斯勋爵和马丁感谢你们俩。我做了一个小测试。而且它工作得很好。正如 Torgamus 勋爵所指出的,

@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
public class MyPage extends WebPage {

    public MyPage() {       
        add(new MyForm("form"));
    }

    private class MyForm extends Form {

        public MyForm(String id) {
            super(id);
            setOutputMarkupPlaceholderTag(true);
            setModel(new Model(new FormModel()));
            final DropDownChoice timePeriod = new DropDownChoice("timePeriod", new PropertyModel(getModel(), "timePeriod"), Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

                private static final long serialVersionUID = 10102L;

                @Override
                public String getIdValue(Object object, int index) {
                    return ((TimePeriod) object).getId() + "";
                }

                @Override
                public Object getDisplayValue(Object object) {
                    return ((TimePeriod) object).getPeriodType();
                }
            });
            timePeriod.setNullValid(false);
            add(timePeriod);
            timePeriod.setOutputMarkupPlaceholderTag(true);
            timePeriod.add(new AjaxFormComponentUpdatingBehavior("onChange") {

                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    MyForm form = (MyForm) timePeriod.getParent();
                    FormModel formModel = (FormModel) form.getModelObject();
                    formModel.setTimePeriod(new TimePeriod(4, "Monthly"));
                    form.setModel(new Model(formModel));
                    target.addComponent(form);
                }
            });
        }

        @Override
        public void onSubmit() {
            System.out.println(getModelObject());
        }

    }
    private class FormModel implements Serializable {
        private TimePeriod timePeriod = new TimePeriod(2, "Bi-Weekly");

        public FormModel() {

        }

        public TimePeriod getTimePeriod() {
            return timePeriod;
        }

        public void setTimePeriod(TimePeriod timePeriod) {
            this.timePeriod = timePeriod;
        }

        @Override
        public String toString() {
            return "FormModel [timePeriod=" + timePeriod + "]";
        }

    }

    private class TimePeriod implements Serializable {
        private int id;
        private String periodType;

        public TimePeriod(int id, String periodType) {
            super();
            this.id = id;
            this.periodType = periodType;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getPeriodType() {
            return periodType;
        }

        public void setPeriodType(String periodType) {
            this.periodType = periodType;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + id;
            result = prime * result + ((periodType == null) ? 0 : periodType.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            TimePeriod other = (TimePeriod) obj;
            if (!getOuterType().equals(other.getOuterType()))
                return false;
            if (id != other.id)
                return false;
            if (periodType == null) {
                if (other.periodType != null)
                    return false;
            } else if (!periodType.equals(other.periodType))
                return false;
            return true;
        }

        private LoginPage getOuterType() {
            return LoginPage.this;
        }

        @Override
        public String toString() {
            return "TimePeriod [id=" + id + ", periodType=" + periodType + "]";
        }

    }
}

上述代码是为其他用户提供的,因为它可能会有所帮助,我编写它是为了测试目的,因此所有类都写在一个 .java 文件中,尽管这是不可取的。

谢谢。

Lord Torgamus and martin-g thank you both of you. I did a small test. And it is working perfectly. As Lord Torgamus indicated,

@SuppressWarnings({ "unchecked", "rawtypes", "serial" })
public class MyPage extends WebPage {

    public MyPage() {       
        add(new MyForm("form"));
    }

    private class MyForm extends Form {

        public MyForm(String id) {
            super(id);
            setOutputMarkupPlaceholderTag(true);
            setModel(new Model(new FormModel()));
            final DropDownChoice timePeriod = new DropDownChoice("timePeriod", new PropertyModel(getModel(), "timePeriod"), Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {

                private static final long serialVersionUID = 10102L;

                @Override
                public String getIdValue(Object object, int index) {
                    return ((TimePeriod) object).getId() + "";
                }

                @Override
                public Object getDisplayValue(Object object) {
                    return ((TimePeriod) object).getPeriodType();
                }
            });
            timePeriod.setNullValid(false);
            add(timePeriod);
            timePeriod.setOutputMarkupPlaceholderTag(true);
            timePeriod.add(new AjaxFormComponentUpdatingBehavior("onChange") {

                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    MyForm form = (MyForm) timePeriod.getParent();
                    FormModel formModel = (FormModel) form.getModelObject();
                    formModel.setTimePeriod(new TimePeriod(4, "Monthly"));
                    form.setModel(new Model(formModel));
                    target.addComponent(form);
                }
            });
        }

        @Override
        public void onSubmit() {
            System.out.println(getModelObject());
        }

    }
    private class FormModel implements Serializable {
        private TimePeriod timePeriod = new TimePeriod(2, "Bi-Weekly");

        public FormModel() {

        }

        public TimePeriod getTimePeriod() {
            return timePeriod;
        }

        public void setTimePeriod(TimePeriod timePeriod) {
            this.timePeriod = timePeriod;
        }

        @Override
        public String toString() {
            return "FormModel [timePeriod=" + timePeriod + "]";
        }

    }

    private class TimePeriod implements Serializable {
        private int id;
        private String periodType;

        public TimePeriod(int id, String periodType) {
            super();
            this.id = id;
            this.periodType = periodType;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getPeriodType() {
            return periodType;
        }

        public void setPeriodType(String periodType) {
            this.periodType = periodType;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + getOuterType().hashCode();
            result = prime * result + id;
            result = prime * result + ((periodType == null) ? 0 : periodType.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            TimePeriod other = (TimePeriod) obj;
            if (!getOuterType().equals(other.getOuterType()))
                return false;
            if (id != other.id)
                return false;
            if (periodType == null) {
                if (other.periodType != null)
                    return false;
            } else if (!periodType.equals(other.periodType))
                return false;
            return true;
        }

        private LoginPage getOuterType() {
            return LoginPage.this;
        }

        @Override
        public String toString() {
            return "TimePeriod [id=" + id + ", periodType=" + periodType + "]";
        }

    }
}

The above code is provided for other users as it might be helpful and I wrote it for testing purpose so all the classes are written in one .java file although it is not advisable.

Thank you.

空城仅有旧梦在 2024-11-11 05:42:00

您应该能够使用 PropertyModel 设置所选索引,而不是对下拉列表的值进行硬编码。我目前无法测试,但它会像

String timePeriodValue = "Bi-Weekly";

DropDownChoice timePeriod = new DropDownChoice("timePeriod",
  new PropertyModel(this, "timePeriodValue"),
  Arrays.asList(/* your options */),
  new IChoiceRenderer() {
    // ...
// Again, this is pseudocode only

作为奖励,设置默认值的行为本身应该消除空白选项问题。

You should be able to set the selected index by using a PropertyModel instead of hard-coding the values of the dropdown. I can't test at the moment, but it would be something like

String timePeriodValue = "Bi-Weekly";

DropDownChoice timePeriod = new DropDownChoice("timePeriod",
  new PropertyModel(this, "timePeriodValue"),
  Arrays.asList(/* your options */),
  new IChoiceRenderer() {
    // ...
// Again, this is pseudocode only

As a bonus, the very act of setting a default value should eliminate the blank option problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文