开发者

Setting attribute to the option of DropDownChoice

开发者 https://www.devze.com 2023-03-04 15:38 出处:网络
I have a DropDownChoice: DropDownChoice dateSpanChoice = new DropDownChoice(\"dateSpan\", new PropertyModel(getModel(), \"dateSpan\"), dateSpans, new IChoiceRenderer() {

I have a DropDownChoice:

        DropDownChoice dateSpanChoice = new DropDownChoice("dateSpan", new PropertyModel(getModel(), "dateSpan"), dateSpans, new IChoiceRenderer() {

            private static final long serialVersionUID = 10105L;

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

            @Override
            public Object getDisplayValue(Object object) {
                DateTime today = new DateTime();
                DateTime currentDate = new DateTime(today.getYear(), today.getMonthOfYear(), today.getDayOfMonth(), 0, 0, 0, 0);
                DateSpan dateSpan = (DateSpan) object;
                DateTime fromDate = dateSpan.getFromDate();
                DateTime toDate = dateSpan.getToDate();
                boolean currentDateIsEqualOrAfterFromDate = currentDate.isEqual(fromDate) | currentDate.isAfter(fromDate);
                boolean currentDateIsEqualOrBeforeToDate = currentDate.isEqual(toDate) | currentDate.isBefore(toDate);
                boolean currentDateBelongsToCurrentRange = currentDateIsEqualOrAfterFromDate & currentDateIsEqualOrBeforeToDate;
                if (currentDateBelongsToCurrentRange) {
                    return ((DateSpan) object).getDisplayValue() + " *";
                } else {
                    return ((DateSpan) object).getDisplayValue();
                }
            }
        }) {

            private static final long serialVersionUID = 10106L;

            protected CharSequence getDefaultChoice(final Object selected) {
                CharSequence charSequence = super.getDefaultChoice(selected);                   
                System.out.println("=============================================================");
                //System.out.println(charSequence);
                //THIS CHARSEQUENCE IS RENRING EMPTY STRING
                if(StringUtil开发者_JAVA技巧s.isBlank(charSequence.toString())) {
                    System.out.println("CHARSEQUENCE IS BLANK");
                } else {
                    System.out.println("CHARSEQUENCE IS NOT BLANK");
                }
                return charSequence;
            }
        };
        dateSpanChoice.setNullValid(false);

I am trying to add style="background-color: red" to the selected option. So I am overriding getDefaultChoice() but as you can see it is commented in code the CharSequence is empty.

Is there any way to set an attribute to a particular option of DropDownChoice?

Thanks and Regards.

Note: the DateTime is Joda time


You need Select/SelectOption instead of DropDownChoice for more complex uses of element.


Have a look at the behavior package in Wicket. You can use the AttributeAppender-class for instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号