Java Wicket:在DropDownChoice项目选择事件上更新模型

Java Wicket:在DropDownChoice项目选择事件上更新模型,java,scala,wicket,Java,Scala,Wicket,我需要在选择后立即更新模型或对象 下拉选择项 下面是我正在使用的代码: add(new ListView[Company]("listCompanies", listData) { override protected def onBeforeRender() { // // ... super.onBeforeRender() } def populateItem(item: ListItem[Company]) = {

我需要在选择后立即更新模型或对象 下拉选择项

下面是我正在使用的代码:

add(new ListView[Company]("listCompanies", listData) {

    override protected def onBeforeRender() {
      //
      // ...
      super.onBeforeRender()
    }

    def populateItem(item: ListItem[Company]) = {
      var company = item.getModelObject()

      //...

      val listClients: java.util.List[Client] = clientControler.listClients


      item.add(new DropDownChoice("clientSelection", listClients,new ChoiceRenderer[Client]("name")))
在具有Company对象属性的Listview中, 选择DropDownChoice的name属性后,模型 将使用选定的客户名称更新公司

我怎样才能做到这一点


谢谢

我认为您可以覆盖已更改的选项。但您还需要覆盖wantOnSelectionChangedNotifications,以返回true以使其正常工作。像这样的

    DropDownChoice<Client> dropDownChoice = new DropDownChoice("clientSelection", listClients) {
        @Override
        protected boolean wantOnSelectionChangedNotifications() {
            return true;
        }

        @Override
        protected void onSelectionChanged(Object newSelection) {
            // Do something here when selection is changed
        }
    };

您需要添加更新行为:

    add(new DropDownChoice<Client>("clientSelection", listClients)
                .add(new AjaxFormComponentUpdatingBehavior("onchange") {

                    private static final long serialVersionUID = 1L;

                    @Override
                    protected void onUpdate(AjaxRequestTarget target) {

// update your model here
// then you need to add model to target
                        target.add();
                    }
                }));

我认为您可以在DropDownChoice上使用AjaxEventBehavior:

item.add(new DropDownChoice("clientSelection", listClients,new ChoiceRenderer[Client("name")).add(new AjaxEventBehavior("change") {
        @Override
        protected void onEvent(AjaxRequestTarget target) {
            // TODO Auto-generated method stub
        }
    }))