如何根据前一个组合框的内容更改组合框的内容?

发布于 2024-12-09 09:41:25 字数 74 浏览 0 评论 0 原文

我有一个包含两个组合框的页面。我想以一种方式制作它们,当我更改第一个组合框时,从数据库获取的第二个组合框的内容也会更改。我该怎么做呢?

I have a page containing two combo boxes. I want to make them in a way that when I change the first combo box the content of the second combo box, which is got from database, is changed. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-12-16 09:41:25

执行此类操作的通常方法是通过 AJAX。您可以轻松地将 AJAX 行为添加到第一个 DropDownChoice 中,以便填充/刷新第二个 DropDownChoice 的选项。

假设您正在使用 IModel 来获取两个 DropDownChoice 的选择。获取第二个 DropDownChoice 选项的 IModel 将使用第一个 DropDownChoice 的 ModelObject(因为它是依赖的)。

private DropDownChoice ddcCountry;
private DropDownChoice ddcCity;
//...

IModel countriesModel = new LoadableDetachableModel(){
   @Override
   protected Object load() {
      return myService.getCountries();
   }
};
IModel citiesModel = new LoadableDetachableModel(){
   @Override
   protected Object load() {
      if (ddcCountry.getModelObject() != null){
          return myService.getCities(ddcCountry.getModelObject());
      }
      else { return new ArrayList(); }
   }
};
ddcCountry = new DropDownChoice("country", null, countriesModel);
ddcCity = new DropDownChoice("city", null, citiesModel);

您可以附加 AjaxFormComponentUpdatingBehavior 到第一个 DropDownChoice。这将在

例如,对于包含国家和城市的 2 个相关选择:

ddcCity.setOutputMarkupId(true);
ddcCountry.add(new AjaxFormComponentUpdatingBehavior(){
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        // here, ddcCountry's ModelObject has been already updated.
        ddcCity.setModelObject(null); // clear selection 
        if (target != null) { 
            // Adding the ddc to the AjaxRequestTarget will write
            // it back to the ajax response with new options pulled 
            // from the choices model.
            target.addComponent(ddcCity);
        }
    }
}

如果您没有使用 IModels 进行选择(即在构造函数中使用 List 对象),您将只需要在 onUpdate 方法中获取新的 List,并使用 ddcCity href="http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/AbstractChoice.html" rel="nofollow">setChoices()< /a>.您可以使用 getComponent() 方法。

protected void onUpdate(AjaxRequestTarget target) {
    // here, ddcCountry's ModelObject has been already updated.
    List cities = myService.getCities(getComponent().getModelObject());
    ddcCity.setChoices(cities);
    ddcCity.setModelObject(null); // clear selection 
    if (target != null) { 
        target.addComponent(ddcCity);
    }
}

如果您想支持没有 javascript 的用户,您应该添加一个默认禁用处理的提交按钮(也许在

有关其他参考,请参阅此 DropDownChoice 示例 Wicket wiki 页面,您可能会找到“ Ajax”部分很有趣。

The usual way to perform this kind of operations is through AJAX. You can easily add an AJAX Behavior to the first DropDownChoice in order to populate/refresh the choices of the second DropDownChoice.

Let's say you are using IModels to get the choices of both DropDownChoices. The IModel that gets the choices for the second DropDownChoice would be using the first DropDownChoice's ModelObject (because it is dependent).

private DropDownChoice ddcCountry;
private DropDownChoice ddcCity;
//...

IModel countriesModel = new LoadableDetachableModel(){
   @Override
   protected Object load() {
      return myService.getCountries();
   }
};
IModel citiesModel = new LoadableDetachableModel(){
   @Override
   protected Object load() {
      if (ddcCountry.getModelObject() != null){
          return myService.getCities(ddcCountry.getModelObject());
      }
      else { return new ArrayList(); }
   }
};
ddcCountry = new DropDownChoice("country", null, countriesModel);
ddcCity = new DropDownChoice("city", null, citiesModel);

You can attach an AjaxFormComponentUpdatingBehavior to the first DropDownChoice. That will output an onchange HTML event handler on the <select> tag, such that it will update that DropDownChoice's model object with the selected value, and then will call your Behavior's onUpdate(). In the onUpdate() method, you will only have to add the second DropDownChoice to the AjaxRequestTarget to write it back through the ajax response with the updated options. Remember to use setOutputMarkupId(true) on all components you'll be adding to the AjaxRequestTarget.

For instance, for 2 dependent selects with countries and cities:

ddcCity.setOutputMarkupId(true);
ddcCountry.add(new AjaxFormComponentUpdatingBehavior(){
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        // here, ddcCountry's ModelObject has been already updated.
        ddcCity.setModelObject(null); // clear selection 
        if (target != null) { 
            // Adding the ddc to the AjaxRequestTarget will write
            // it back to the ajax response with new options pulled 
            // from the choices model.
            target.addComponent(ddcCity);
        }
    }
}

In case you're not using IModels for your choices (i.e. using List objects in the constructors), you'd just need to get the new List in the onUpdate method, and set it to ddcCity with setChoices(). You can get the Component the Behavior is bounded to with the getComponent() method.

protected void onUpdate(AjaxRequestTarget target) {
    // here, ddcCountry's ModelObject has been already updated.
    List cities = myService.getCities(getComponent().getModelObject());
    ddcCity.setChoices(cities);
    ddcCity.setModelObject(null); // clear selection 
    if (target != null) { 
        target.addComponent(ddcCity);
    }
}

In case you want to support users without javascript, you should add a submit button (maybe in a <noscript> tag?) with default from processing disabled, and perform that same logic in the button's onSubmit.

For additional reference, see this DropDownChoice Examples Wicket wiki page, you might find the "Ajax" section interesting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文