4

In wicket IChoiceRenderer for DropDownChoice is used like :

IChoiceRenderer renderer = new IChoicerRenderer() {
    public Object getDisplayValue(Object object) {
        return ((Country) object).getName();
    }

    public String getIdValue(Object object, int index) {    
        return ((Country) object).getId() + "";
    }
};

countries.setChoiceRenderer(renderer);

The specification of the IChoiceRenderer class state that:

Renders one choice. Separates the 'id' values used for internal representation from 'display values' which are the values shown to the user of components that use this renderer.

The description of getDisplayValue() is:

Get the value for displaying to an end user.

That means it helps to display the name of the country. Right?

And the description of getIdValue() is:

This method is called to get the id value of an object (used as the value attribute of a choice element) The id can be extracted from the object like a primary key, or if the list is stable you could just return a toString of the index.

What does it mean?

In general id property of the models of various wicket component like DropDownChoice here, is of the type Long. Is getIdValue() helps to sort it?

Or helps to generate id tag for HTML?

What is concept of the aforesaid "Primary Key"?

Thanks and Regards.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

2 Answers2

4

Imagine that the objects will be put in a map, where the id is the key and the value is the object you want it to refer to. If two of your objects share the same id, or if the id of an object changes, your map won't work properly.

This is what they mean by saying it should be a primary key.

By the way, you don't have to implement IChoiceRenderer from scratch in simple situations, in your case you can use new ChoiceRenderer( "name", "id" );

biziclop
  • 48,926
  • 12
  • 77
  • 104
1

In a drop down list you will have items that are key value pairs. So using your countries example, consider the following mapping of country to country code:

Key             Value
---------------------
Afghanistan     AF
Aland Islands   AX
Albania         AL
Algeria         DZ
American Samoa  AS
Andorra         AD
Angola          AO 

If the user selects Algeria then the key DZ gets used to uniquely identify their choice. So if the main object is a Person with a countryOfCitizenship property, that property would be set to the Country with the id DZ. Wicket uses the id to set the selection from the drop down as the value for the property. It also uses the id to determine which value to select from the drop down when the page is displayed for an object that has that property set.

laz
  • 28,320
  • 5
  • 53
  • 50