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?
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
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.
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.