Hi,

preferably your dto would hold a batch instance instead of the name only.

In your case you have to do the mapping somewhere, the renderer is a good place for that.
Move it into its own class file so it won't hurt so much :P.

Have fun
Sven


On 01.07.19 14:45, Zbynek Vavros wrote:
Hi,

I don't have any specific use-case for this but I'm interested on how to do
this properly.

There is a DropDownChoice that displays list of Batches.
Now the display option should be "id" and "name" concated together and
the DropDownChoice selection model will be only the name.

Batch is simple entity/DTO with Long id and String name, something like:

public class Batch implements Serializable {
     private Long id;
     private String name;
}

DropDownChoice is added to form that has CompoundPropertyModel for example
like this:

public class ResultDto implement Serializable {
     private String batchName;
}

the only solution I can think of is custom IChoiceRenderer but looks nasty

     List<Batch> batches = Lists.newArrayList();
     List<String> names
=batches.stream().map(Batch::getName).collect(Collectors.toList());

     add(new DropDownChoice<>(" batchName ", names, new
IChoiceRenderer<String>() {
             @Override
             public Object getDisplayValue(String name) {

                 Batch batch1 = batches.stream().filter(batch ->
name.equalsIgnoreCase(batch.getName())).findFirst().get();

                 return batch1.getId() + " - " + batch1.getName();
             }

             @Override
             public String getIdValue(String object, int index) {
                 return String.valueOf(index);
             }

             @Override
             public String getObject(String id, IModel<? extends List<?
extends String>> choices) {
                 return batches.get(Integer.valueOf(id)).getName()
             }
         }));

Surely there has to be a better way to do this!

Zbynek


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to