2014-04-29 87 views

回答

3

您應該使用第一個選項的值來確定第二個值的值。

我這個例子中,我選擇使用觸發Ajax更新並執行值更改的AjaxFormComponentUpdatingBehavior。我提供了第二個DropDownChoice與第一個選定國家的聯邦國家的簡單示例。

請注意,我在這個例子中使用了wicket 1.4,不過在新版本中不應該有太大的不同。

// two countries 
    final String aut = "AUT"; 
    final String ger = "GER"; 

    // their states 
    final List<String> autStates = Arrays.asList(new String[] { "V", "T", "S", "W" }); 
    final List<String> gerStates = Arrays.asList(new String[] { "NRW", "B", "BW" }); 

    // mapping, you should really get this data from a service or have a constant 
    final Map<String, List<String>> countryToState = new HashMap<String, List<String>>(2); 
    countryToState.put(aut, autStates); 
    countryToState.put(ger, gerStates); 

    // the container to send back via ajax 
    final WebMarkupContainer cont = new WebMarkupContainer("cont"); 
    cont.setOutputMarkupId(true); 
    add(cont); 

    final Model<String> stateModel = new Model<String>(); 
    final DropDownChoice<String> countries = new DropDownChoice<String>("countries", new Model<String>(), new ArrayList<String>(countryToState.keySet())); 
    final DropDownChoice<String> states = new DropDownChoice<String>("states", stateModel, new LoadableDetachableModel<List<String>>() { 

     @Override 
     protected List<String> load() { 
      final String country = countries.getModelObject(); 
      final List<String> list = countryToState.get(country); 

      return list != null ? list : new ArrayList<String>(0); 
     } 
    }); 

    countries.add(new AjaxFormComponentUpdatingBehavior("onchange") { 

    @Override 
    protected void onUpdate(AjaxRequestTarget target) {   
        // just add the container to see the results 
     target.addComponent(cont); 
    } 

    }); 

    cont.add(countries); 
    cont.add(states); 
+0

非常感謝你!!!這就是我想知道的一切!:) – evilkorona

0

第一DropDownChoice應該更新ModelDropDownChoice基於並添加第二DDC到AjaxRequestTarget

This constructor接受該模型,並且可以存儲對其的引用以便能夠對其進行更改。

+0

這一個? DropDownChoice(String id,IModel <?extends List > choices)我沒有得到它的工作方式,例如,第一個ddc有1,2和3個選項。當我選擇1,在第二個ddc我想看到(例如)4,5,6選項。當我選擇2時,我想看到7,8,9。這些選項將在哪裏初始化?謝謝:) – evilkorona

+0

好吧,你剛纔寫到了,雖然在自然語言中... ...在處理數值變化的方法中,你應該檢查 - 它當前選定的值是1,其他DDC模型包含4 5 6,如果2 - 包含7 8 9. –

+0

好的,謝謝!我現在就試試吧! :D – evilkorona

相關問題