我有一个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(StringUtils.isBlank(charSequence.toString())) {
                    System.out.println("CHARSEQUENCE IS BLANK");
                } else {
                    System.out.println("CHARSEQUENCE IS NOT BLANK");
                }
                return charSequence;
            }
        };
        dateSpanChoice.setNullValid(false);


我试图将style =“ background-color:red”添加到所选选项。因此,我重写了getDefaultChoice(),但是如您所见,它在代码中已注释,CharSequence为空。

有什么方法可以将属性设置为DropDownChoice的特定选项?

感谢致敬。

注意:DateTime是Joda时间

最佳答案

您需要Select / SelectOption而不是DropDownChoice来更复杂地使用元素。

关于java - 将属性设置为DropDownChoice的选项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5947078/

10-11 20:32