ComboBox with DataProvider

Hi,

I have a String, and would like to provide it to ComboBox. Here are my codes:

ListDataProvider dp = DataProvider.ofCollection( Arrays.asList( databases ) );
ComboBox s = new ComboBox( "Select: " );
s.setDataProvider( dp );

The select showed all the options in text. However, when I selected, the element value I got is number like 1, 2, 3, … How can I get the text as value?

Best regards,
Joey

Hi Joey. Did you notice that ComboBox has the API that you can just set an array of items to it ?

eg.

String[] data = getDataForComboBox();

ComboBox<String> comboBox = new ComboBox<>();
comboBox.setItems(data); // this creates a list data provider internally, you don't need to do it yourself

comboBox.getDataProvider(); // this would return the DP if you need it for something

Hi, Pekka,

Yes, the “databases” in my sample code is String. I also used the method of setItems, but the result is the same. I set id of the ComboBox as “selecteddatabase”, and got into console of browser debugger, and run jQuery(“#selecteddatabase”).val(). The result showed a number like 1, 2,… It seems that ComboBox doesn’t set text value as NativeSelect. Any advice? I need the element value set to be text in order to submit the form.

Best regards,
Joey

Oh, I thought you were referring to the server side value … Which version of the vaadin-combo-box you have there (or which platform version) ? I think it used to be so that value property was the index of the item in the items array, so you should use the selectedItem property instead of the value property.

But now if I look at the API docs for the https://vaadin.com/components/vaadin-combo-box/html-api/elements/Vaadin.ComboBoxElement which has the latest version (included in Vaadin 12.0.0.beta1), it says that the value should be a string, so either it has changed or there is something else in play …

The vaadin-core version I am using is 11.0.1. Do I need to upgrade to 12.0.0?

Hi,

I couldn’t find downloadable Vaadin 12 in Maven repository. Any advices where I can download them?

Best regards,
Joey