2012-04-27 147 views
4

我有一個DropDownChoice問題。我必須預先選擇一個項目,但是我找到的每個教程和示例都只考慮原始類型的列表。檢票口 - DropDownChoice與對象選中

我有一個Object的列表。

class myObject { 
    private String name; 
    private String surname; 
    [setter and getter] 
} 

在其他類

List<MyObject> myList = some_data_retrieve(); 
MyObject defaultValue = some_simple_data_retrieve(); 

使用以下constuctor建立DropDownChoice IM:

final DropDownChoice<T> ddc = new DropDownChoice<T>(id, data, new ChoiceRenderer<T>(choiceRendererExpression, choiceRendererIdExpression)); 

這樣:

final DropDownChoice<myObject> ddc = new DropDownChoice<myObject>("wicket_id", myList, new ChoiceRenderer<myObject>("name", "surname")); 

現在。在每個教程/例子中,他們使用另一個構造函數和一個Model。例如:

private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] { 
     "Google", "Bing", "Baidu" }); 
private String selected = "Google"; 
DropDownChoice<String> listSites = new DropDownChoice<String>(
     "sites", new PropertyModel<String>(this, "selected"), SEARCH_ENGINES); 

我已經試過這樣的事情來模擬那種呼叫:

final DropDownChoice<myObject> ddc = new DropDownChoice<myObject>("wicket_id", new PropertyModel<myObject>(this,"defaultValue"),myList, new ChoiceRenderer<myObject>("name", "surname")); 

但我得到的是一個錯誤:

No get method defined for class: package$WicketPage expression: defaultValue 

請幫我undersand 。

感謝

回答

8

這意味着你需要在頁面或組件添加一個getter和你的「默認值」的制定者,你要添加的DropDownChoice。

public class MyPage extends WebPage { 

    private MyObject defaultValue; 

    public MyPage(PageParameters pageParameters) { 
     super(pageParameters); 

     defaultValue = some_simple_data_retrieve(); 
     List<MyObject> myList = some_data_retrieve(); 

     add(new DropDownChoice<myObject>(
         "wicket_id", 
         new PropertyModel<MyObject>(this,"defaultValue"), 
         myList, 
         new ChoiceRenderer<MyObject>("name", "surname") 
     );   
    } 

    public MyObject getDefaultValue() { 
     return defaultValue; 
    } 

    public void setDefaultValue(MyObject defaultValue) { 
     this.defaultValue = defaultValue; 
    } 
} 
+0

構造函數中的第二個參數是所選元素的模型。不是默認值的模型。這是,我怎麼理解檢票。 – drdrej 2015-01-21 14:06:29

0

PropertyModel是這類問題的好選擇。 MyObject是一個對象,並有一個字符串name。我已經覆蓋其中的toString()方法名稱和它正常工作。我建議使用此方法。

topicDropDown = new DropDownChoice<MyObject>("wicktID", new PropertyModel<MyObject>  (this.object, "exp"), new LoadableDetachableModel<List<MyObject>>() { 
     @Override 
     protected List<MyObject> load() { 
      return top.getAllObjects(); 

     }