【问题标题】:Vaadin can't get combo box field value at 0 positionVaadin 无法在 0 位置获取组合框字段值
【发布时间】:2016-08-24 07:17:35
【问题描述】:

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

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

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

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

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

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

附言这是我的代码:

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

【问题讨论】:

  • 您的通讯箱里还有哪些其他物品?在 vaadin 中,您通常不使用索引,而是直接使用对象
  • 您可以禁用空选择,并通过组合框的 setValue 初始选择索引 0 处的项目。
  • Steffen Harbich 已经尝试过,但不起作用。我尝试了 select()、setValue()、各种组合 - 没有任何效果。安德烈,我有其他组合框和文本框,没有组合框,除了这个应该选择默认值。看来我必须有空白值并选择它,否则它将不起作用..我不敢相信在 Vaadin 中获取这个简单的数据是如此困难。
  • 我已经用代码更新了我的问题,你能帮忙吗?我已经尝试了每一个选项,试图在索引 0 位置设置默认值
  • 对我来说,这看起来像是您正在绑定空选择。只需将默认值放在您绑定到的对象上。

标签: vaadin vaadin7 vaadin6


【解决方案1】:
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);
        }
    }
}

【讨论】:

  • 您不仅应该抛出一些代码,还应该解释问题是什么以及您是如何解决的。这将使您的回答对原始发帖人和未来的访问者更有价值。
  • 好的。我会尽力。使用此方法,您可以选择索引指示处的组合框的一项作为参数。被认为是 Java Swing 中的 combobox.selectedIndex(n)
【解决方案2】:

如果我正确理解了您的问题,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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多