2012-08-22 3 views
0

DropDownChoice에 약간의 문제가 있습니다. Object 목록으로 DropDownChoice를 빌드합니다. 예를 들어 보자 다른 객체Wicket - DropDownChoice POJO 속성 모델

public FormModelObject 
    private String code; 

내가 easly ChoiceRender를 사용하여 DropDownChoice 선택 어떤 쇼 (설명 속성) 무엇을 얻을 수 (code 속성)을 구축 할 수 있습니다을

public MyClass 
    private String code; 
    private String description; 
[...] 

보다.

불행히도 DropDownChoice는 전체 Object (MyClass)를 반환하고 FormModelObject 내에 속성 코드를 설정할 수 없습니다.이 코드는 MyClass에서 toString() 메서드를 실행하기 만하면됩니다.

Ajax를 사용하지 않고 어떻게 얻을 수 있습니까?

감사합니다.

+0

을 왜 것? IModel 을 구현하는 DDC의 Backing Model이 있으며 간단히 객체와 코드 속성에 액세스 할 수 있습니까? – bert

답변

1

음, DropDownChoice의 onModelChanged() 메서드를 재정의하고 residenceZipCode를 새 값으로 업데이트 할 수 있습니다. 다음과 같음 :

DropDownChoice<City> drop = new DropDownChoice<City>("residenceZipCode",new Model(), cityList,new ChoiceRenderer<City>("name", "zipcode")){ 
    onModelChanged(){ 
     String newZipCode = (String)PropertyResolver.getValue("zipCode",getModelObject()); 
     PropertyResolver.setValue("residenceZipCode", res, newZipCode, new PropertyResolverConverter()); 
    } 

}; 

DropDownChoice에는 자체 모델이 있어야합니다.

1

모델 체인을 사용해보십시오. 양식의 변수 코드를 다음과 같이 작성된 속성 모델로 대체해야합니다.

//this is the model of your DropDownChoice 
Model<MyClass> myModel = new Model<MyClass>(); 
//... 

PropertModel myCode = new PropertModel(myModel, "code"); 

이제 폼의 코드 대신 myCode를 사용하십시오. 이제

class City { 
     private String zipCode; 
     private String name; 
    } 

    class Residence { 
     private String residenceZipCode; 
    } 

protected Residence res; 

public ResidenceForm() { 
    super("form", new CompoundPropertyModel<Residence>(res)); 

    List<City> cityList = someManager().loadAllCity(); 

    DropDownChoice<City> drop = new DropDownChoice<City>("residenceZipCode",cityList,new ChoiceRenderer<City>("name", "zipcode")); 
add(drop); 

:

0

그것의 몇 가지 코드를 작성 bettere. 거주지 또는 도시를 변경할 수 없으며 제출 버튼의 메소드 안에 코드를 쓸 수 없습니다.

레지던스 안에 우편 번호가 있어야합니다. 지금 얻은 것은 City.toString()과 같습니다.

1

DDC의 유형 인수를 살펴보십시오. 귀하의 경우 City이므로 DDC는 City 개체와 함께 작동합니다. 당신이 원하지 않는 경우

는 유형을 변경하고 컴파일러는 당신에게 길을 안내 할 것입니다 :

DropDownChoice<String> drop = new DropDownChoice<String>("residenceZipCode",zipCodesList,new CityChoiceRenderer()); 

class CityChoiceRenderer implements IChoiceRenderer<String> { 
    Object getDisplayValue(String zipCode) { 
    return zipCodeToName(zipCode); 
    } 

    String getIdValue(String zipCode, int index) { 
    return zipCode; 
    } 
}