2014-10-28 1 views
4

제 프로젝트에는 50 개가 넘는 양식이 있으며, 이들은 서로 similarar이며 동일한 DropDownChoice 구성 요소를 사용합니다. 별도의 Panel을 만들 수도 있는데, 여기서 내 DropDownChoice을 정의하고 그 후에 다른 양식에 Panel을 사용하게됩니까? 그렇지 않으면 어떻게 그 상황을 구현할 수 있습니까?
이름 (TextField)
(TextField)
도시 (DropDownChoice)Wicket 양식의 재사용 가능한 DropDownChoice

form2이있다 : 예를

form1를 들어

다음 필드가 있습니다 다음 필드 :
코드 (TextField)
금액 (TextField)
도시 (다시 같은 DropDownChoice) 나는 그 방법의 아름다운 솔루션을 만들고 싶어

.

답변

5

DropDownChoice을 미리 정의 된 매개 변수로 확장하는 것이 좋으며 DropDownChoice이 아닌 Panel이 아닌 것이 좋습니다.

이 방법의 적어도 두 가지 장점이 있습니다

  1. 당신이 Panel -approach 함께 제공으로 별도의 마크 업 파일을 만들 필요가 없습니다.
  2. DropDownChoice 메서드를 직접 사용할 수 있습니다. 그렇지 않으면 Panel의 메서드를 전달하거나 DDC의 getter 메서드를 구현해야합니다.

그래서,이 같은 더 나은 것 :

public class CityDropDownChoice extends DropDownChoice<City> // use your own generic 
{ 

    /* define only one constructor, but you can implement another */ 
    public CityDropDownChoice (final String id) 
    { 
     super (id); 

     init(); 
    } 

    /* private method to construct ddc according to your will */ 
    private void init() 
    {   
     setChoices (/* Your cities model or list of cities, fetched from somewhere */); 
     setChoiceRenderer (/* You can define default choice renderer */); 

     setOutputMarkupId (true); 

     /* maybe add some Ajax behaviors */ 
     add(new AjaxEventBehavior ("change") 
     { 
      @Override 
      protected void onEvent (final AjaxRequestTarget target) 
      { 
       onChange (target); 
      } 
     }); 
    } 

    /*in some forms you can override this method to do something 
     when choice is changed*/ 
    protected void onChange (final AjaxRequestTarget target) 
    { 
     // override to do something. 
    } 
} 

그리고 당신의 형태

단순히 사용
Form form = ...; 
form.add (new CityDropDownChoice ("cities")); 

이 방법이 귀하의 필요에 적합하다고 생각.

+1

주위를 돌리려면 드롭 다운에서 특별한 마크 업을 원한다면 '패널'을 선택하십시오. – biziclop

+0

@biziclop, 동의 함. –

관련 문제