Search code examples
drop-down-menukotlinvaadinselectionvaadin8

Vaadin 8 NativeSelect change data representation of list


I am using Vaadin 8 NativeSelect as a dropdown for users to make selections. Each selection refers to an object instance that holds multiple pieces of data and I only want the dropdown to display one piece of identifying information pertaining to a single variable in that object however it doesn't seem like NativeSelect can handle Model and Presentation separately in an obvious way.

data class Consultant(
    val name: String,
    val id: Long
)

val consultantSelection = NativeSelect<Consultant>().apply { 
    setItems(consultantDao.getAll())
    caption = "Select Consultant"
}

In my code, I create a NativeSelect handling a type of Consultant which then creates a dropdown menu that simply unpacks the contents of the respective Consultant objects as each selectable. I would like to only display the name of the Consultant in the selection.

Is there a way to achieve this?


Solution

  • You have to provide a setItemCaptionGenerator for this. Otherwise Vaadin falls back to the .toString() implementation.

    There is a whole section about dealing with things around that in Vaadin Documentation