3

I need to create a wicket dropdownchoice component, I can replace default "Choose One" text to "All" by set

nullValid=All
null=All

in its properties file. I also want to set the value of "All" to -1, but could not get it done.

<select>
<option selected="selected" value>All</option>
<option value="1">Not Started</option>
<option value="2">In Progress</option>
<option value="3">Complete</option>
</select>

what i want is

<select>
<option selected="selected" value="-1">All</option>
<option value="1">Not Started</option>
<option value="2">In Progress</option>
<option value="3">Complete</option>
</select>
William S
  • 33
  • 1
  • 4

2 Answers2

3

For this you have to setup the DropDownChoice to not allow null value and add -1 to your list of allowed/possible values. Finally you have to instantiate it with: new DropDownChoice("compId", Model.of(-1), Arrays.asList(-1, 1, 2, 3), choiceRenderer), i.e. -1 should be set as the default model object, so it is selected in the markup.

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • Worked like a charm. Thanks – William S Jul 13 '17 at 14:39
  • In this way, the display is correct, But after form is submitted, the value in the dropdown is not taken. The value is always null. The first model seems causes the problem. – William S Jul 14 '17 at 03:09
  • There must be something wrong in the submit process. Do you see a proper value submitted for the select element ? – martin-g Jul 14 '17 at 08:28
1

I'm not sure whether you got martin point . I am going to give some simple solution which will resolve your issue. let's assume you have SelectionOption class which you have key and value

public class SelectOption {
    private String key;
    private String value;

    public SelectOption(String key, String value) {
        this.key = key;
        this.value = value;
    }
     // getter and setter
}

Create a simple list of selectOptions and pass to the dropdownchoice

List<SelectOption> selectOptions = new ArrayList<>();
        selectOptions.add(new SelectOption("-1","ALL"));
        selectOptions.add(new SelectOption("1","Not Started"));
        selectOptions.add(new SelectOption("2","In Progress"));
        selectOptions.add(new SelectOption("3","Completed"));

//Simply override choiceRender option to show key and value . I have override getDefaultChoice to remove select one(Its my convenient) since you removed already .

        add(new DropDownChoice("selectOption", selectOptions,new ChoiceRenderer<SelectOption>("value","key"){
            @Override
            public Object getDisplayValue(SelectOption object) {
                return object.getValue();
            }

            @Override
            public String getIdValue(SelectOption object, int index) {
                return object.getKey();
            }
        }){
            @Override
            protected CharSequence getDefaultChoice(String selectedValue) {
                return "";
            }
        });

Output:

<select wicket:id="selectOption" name="selectOption">
<option value="-1">ALL</option>
<option value="1">Not Started</option>
<option value="2">In Progress</option>
<option value="3">Completed</option>
</select>
soorapadman
  • 4,451
  • 7
  • 35
  • 47