2017-06-26 3 views
0

複数の場所で使いたい設定が1つあります。私は取得し、整数値を設定する必要があります:0,1,2、-1。表示された文字列は保存されません。Wicket - DropDownChoiceでSelectOptionを使用する方法?

HTMLの断片:ジャワの

<select wicket:id="thingPref"> 
    <option>1</option> 
    <option>2</option> 
</select> 

フラグメント。

import org.apache.wicket.extensions.markup.html.form.select.SelectOption; 
import org.apache.wicket.markup.html.form.DropDownChoice; 

private SelectOption thingPreference; 

private void buildCommon() 
{ 
    thingPreference = new SelectOption(Integer.toString(setting()), new Model<String>("dummy")); 
    List<SelectOption> prefChoices = new ArrayList<SelectOption>(); 
    prefChoices.add(new SelectOption<String>("0", new Model<String>("Current Thing"))); 
    prefChoices.add(new SelectOption<String>("1", new Model<String>("One Prior Thing"))); 
    prefChoices.add(new SelectOption<String>("2", new Model<String>("Two Prior Things"))); 
    prefChoices.add(new SelectOption<String>("-1", new Model<String>("All Things"))); 
    DropDownChoice<SelectOption> prefselect = new DropDownChoice<SelectOption>("thingPref", 
      new PropertyModel<SelectOption>(this, "thingPreference"), prefChoices) 
    { 
     private static final long serialVersionUID = 1L; 

     protected boolean wantOnSelectionChangedNotifications() 
     { 
      return true; 
     } 

     protected void onSelectionChanged(final String newSelection) 
     { 
      System.out.format("onSelectionChanged(%s)%n", newSelection); 
     } 
    }; 
    prefselect.setNullValid(false); 
    prefselect.setRequired(true); 
    add(prefselect); 
} 

開発者ツールは、私は解決策は、レンダラを追加するのと同じくらい簡単であることを見ていない

<option value="-1">All Things</option> 

を必要とするとき、この

<option value="3">[SelectOption [Component id = -1]]</option> 

ようなHTMLを示しており、私がいることを確認していません設定には列挙型を使用する必要があります。私はWicket 7.6を使用しています。何か案は?

答えて

0

org.apache.wicket.extensions.markup.html.form.select.SelectOptionはないorg.apache.wicket.markup.html.form.DropDownChoiceで、org.apache.wicket.extensions.markup.html.form.select.Selectして使用する必要があります。

DropDownChoiceまたはSelectのいずれかを使用できます。どちらの場合も、カスタムIChoiceRendererが必要です。

Selectは、すべてのオプションをさらに詳細に制御する必要がある場合は、DropDownChoiceより優先する必要があります。 DropDownChoiceはPOJOのリストで動作します。

0

カスタムレンダラを適用してデータオプションを表示しました。

private static final List<Integer> TermSettings = Arrays.asList(0, 1, 2, -1); 

ChoiceRenderer<Integer> choiceRenderer = new ChoiceRenderer<Integer>() 
{ 
    private static final long serialVersionUID = 1L; 

    @Override 
    public Object getDisplayValue(Integer value) 
    { 
     switch (value) 
     { 
      case 0: 
       return "Current Term"; 
      case 1: 
       return "One Prior Term"; 
      case 2: 
       return "Two Prior Terms"; 
      case -1: 
       return "All Terms"; 
      default: 
       throw new IllegalStateException(value + " is not mapped!"); 
     } 
    } 

    @Override 
    public String getIdValue(Integer object, int index) 
    { 
     Integer idvalue = TermSettings.get(index); 
     String strvalue = String.valueOf(idvalue); 
     return strvalue; 
    } 
}; 
関連する問題