0

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

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

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

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

但是,不幸的是,当我提交表单时,值没有传递。仅传递索引 0 之后的值。好郁闷啊,有人能帮帮我吗?传递给 setNullSelectionItemId 的值存在 100%。

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

ps这里是我的代码:

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);
4

2 回答 2

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));
于 2016-05-02T17:53:24.110 回答
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);
        }
    }
}
于 2016-09-06T17:40:51.900 回答