Vaadin无法在0位置获得组合框字段值

时间:2016-04-29 08:14:02

标签: vaadin vaadin7 vaadin6

我正在尝试将Vaadin组合框中索引0处的字段设置为默认值,因此如果用户不选择任何内容,我可以避免出现错误消息。所以我希望而不是空白字段我在索引0处填充了字段。

我试图设置它并用它来管理它:

field.setNullSelectionAllowed(true);
field.setNullSelectionItemId(container.getIdByIndex(0));

所以我在索引0没有空值,而是我之前的索引1的值现在是索引0.这正是我想要和需要的,并且在组合框中看起来就像我想要的那样。

但是,不幸的是,当我提交表单时,价值没有通过。仅传递索引0之后的值。这太令人沮丧了,有人可以帮帮我吗?传递给setNullSelectionItemId的值存在100%。

如何从组合框中的0位置索引值?

P.S。这是我的代码:

public Field<?> buildAndBindComboBox(final String caption, final BeanItemContainer<?> container,
        final Object propertyId, final String title, final ValueChangeListener listener, final boolean nullAllowed,
        final boolean required, final boolean enabled, final boolean visible) {

    @SuppressWarnings("serial")
    ComboBox field = new ComboBox(caption, container) {
        // http://dev.vaadin.com/ticket/10544
        // - typing in ComboBox causes Internal Error
        private boolean inFilterMode;

        @Override
        public void containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent event) {
            if (inFilterMode) {
                super.containerItemSetChange(event);
            }
        }

        @Override
        protected List<?> getOptionsWithFilter(boolean needNullSelectOption) {
            try {
                inFilterMode = true;
                return super.getOptionsWithFilter(needNullSelectOption);
            } finally {
                inFilterMode = false;
            }
        }
    };

    field.setStyleName("comboBox");
    field.setInputPrompt("Select");
    if(defaultValue == true){
        field.setNullSelectionAllowed(false);
        field.setNullSelectionItemId(container.getIdByIndex(0).toString());
        //field.select(container.getIdByIndex(0));
        //field.setValue(container.getIdByIndex(0));
        //field.setRequired(false);
        defaultValue = false;
    } else {
        field.setNullSelectionAllowed(nullAllowed);
        field.setRequired(required);
    }
    field.setImmediate(true);
    field.setNewItemsAllowed(false);
    field.setFilteringMode(FilteringMode.CONTAINS);
    if (title != null) {
        field.setItemCaptionPropertyId(title);
    }
    //field.setNullSelectionAllowed(nullAllowed);
    //field.setRequired(required);
    field.setVisible(visible);

    if (listener != null) {
        field.addValueChangeListener(listener);
    }

    this.bind(field, propertyId);

    field.setEnabled(enabled);

    return field;
}

public void setDefaultValueFirstItem(boolean def){
   defaultValue = def;
}

这样绑定:

commitmentFeeBinder.setDefaultValueFirstItem(true);
commitmentFeeBinder.buildAndBindComboBox("No working day labels", noWorkingDays, "noWorkingDaysCF", "title", null, false, !transaCF, true, !transaCF);

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,Steffen Harbich建议如果你想在默认情况下选择第一项你是正确的,你应该禁用空选择并默认选择第一项。例如。这有效:

ComboBox cb = new ComboBox("", Arrays.asList("First", "Second", "Third"));
cb.setNullSelectionAllowed(false);
cb.select("First");

或者使用BeanItemContainer:

List<MyBean> beans = Arrays.asList(new MyBean("First"), new MyBean("Second"), new MyBean("Third"));
BeanItemContainer<MyBean> bic = new BeanItemContainer<>(MyBean.class, beans);
ComboBox cb = new ComboBox("", bic);
cb.setNullSelectionAllowed(false);
cb.select(bic.getIdByIndex(0));

答案 1 :(得分:0)

private void resetComboBoxToIndex(ComboBox combo, int index) {
    BeanItemContainer<Bean_ComboBox> items_combo = (BeanItemContainer<Bean_ComboBox>)combo.getContainerDataSource();
    if(items_combo != null && items_combo.size() > index) {
         Bean_ComboBox primerItem = items_combo.getIdByIndex(index);
         if(primerItem != null) {
                combo.select(primerItem);
        }
    }
}