Why duplicates in ListSelect with New Items Allowed?

Hi,

I use a ListSelect component with NewItemsAllowed in my Vaadin 7.2.0 application, and note that an item already in the list can be added one more time to it, why?

Noticed that the same happens on the Vaaadin Demo page in the Demo>User Interface>Data Input>Multiple value>List select when setting the New Items Allowed flag!

How to configure the ListSelect in such a way that duplicates are not allowed?

Regards,
Gerard

It depends on what container you use, and more specifically what ItemId type the container uses. You can intercept the new item with your own NewItemHandler, regardless of container:

public void setNewItemHandler(AbstractSelect.NewItemHandler newItemHandler)

Hi Thomas,

Thanks for your answer, but now I’m sort of stuck, sorry!

I use the default container that is used by the ListSelect component!

Can you elaborate a bit more about how to tackle this by a simple example?

How to use this setNewItemHandler method?

Regards,
Gerard

It all depends on the ItemId that you give to the container. The ItemId isn’t necessarily what you see as the item caption; for instance, the default new item handler (that you are using), uses the given String as an Item Id. But if you do this:

Item i = select.addItem(); i.getItemProperty("someprop").setValue("Option 3"); select.setItemCaptionPropertyId("someprop"); The caption of this item can be anything, even something that is identical to another item. The important part (the part Vaadin checks) is the ItemId, which in this case is an auto-generated integer. I suspect this is what happens in the demo: you can add an option once (because that string isn’t an itemid yet), but not twice. The original items use something else as ItemIds.

To make sure the select works like you want it to, you need to create a NewItemHandler. The addNewItem() method is called when the user adds an item; it is up to you to check readOnly, duplicates, container type, etc, and add the item to the select. You can check the default implementation for some sample code (DefaultNewItemHandler).

Hi Thomas,

Thanks for your explanation!

After introducing my NewItemHandler class and overrding the addNewItem() method it now works correctly!

Regards,
Gerard