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

Reply via email to