4

Wicket 中是否有一些东西可以做两个下拉选择,以便第一个 doprdownchoice 的第一个选择会更改第二个的所有选项?

4

2 回答 2

5

您应该使用第一个选项的值来确定第二个选项的值。

在这个示例中,我选择使用AjaxFormComponentUpdatingBehavior触发 Ajax 更新并执行值更改的 a。我提供了一个简单的例子,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);
于 2014-04-29T12:28:49.690 回答
0

第一个DropDownChoice应该更新Model第二个DropDownChoice基于并将第二个 DDC 添加到AjaxRequestTarget.

此构造函数接受模型,您可以存储对它的引用以便能够更改它。

于 2014-04-29T00:25:20.407 回答