【问题标题】:API call not triggered in onChange of dropdown selection下拉选择的 onChange 中未触发 API 调用
【发布时间】:2021-05-08 20:47:50
【问题描述】:

我在父页面中定义了下拉列表,在那里我想添加其他行为,使得只要选择从下拉列表中的选项时,会拨打一个呼叫以确定标志的值(isTall)然后使用此标志来确定是否显示附加文本。

ParentPage.java

private Person person;
private PropertyModel<CertapayContact> personModel = new PropertyModel<>( this, "person" );

// sub-component that sets the disclaimer text and the optional text I want to add
final Panel somePanel = new SomePanel( "SomePanel", personModel );
somePanel.setOutputMarkupId( true );
somePanel.setOutputMarkupPlaceholderTag( true );

// retrieve list of people

// Recipient Drop down
recipientDropDownChoice = new DropDownChoice<Person>( "Recipient", personModel, people
contacts, new PersonRenderer<Person>( personMap ))
    {
        @Override
        ...
    };

recipientDropDownChoice.getInternalComponent().add( new AjaxFormComponentUpdatingBehavior( "onchange" )
    {
        @Override
        protected void onUpdate( AjaxRequestTarget ajaxRequestTarget )
        {

            // re-render the page to show other selection-dependent text
            ajaxRequestTarget.addComponent( somePanel );
            ajaxRequestTarget.addChildren( somePanel, Component.class );
        }

    } );

add(somePanel);
add(recipientDropDownChoice);

SomePanel.java

public SomePanel( String id, IModel<Person> personModel )
{
    Person person = personModel.getObject();
    boolean isTall = apiCallToCheckIfTall( person );

    tallLabel = isTall ? new Label( "height", "Tall" ) : new Label( "height", "Short" );

    add(tallLabel);
}

在调试过程中,API 调用仅在页面首次加载时进行一次。在下拉列表中进行选择时,不会触发调用。我不太清楚为什么。

【问题讨论】:

    标签: java ajax wicket


    【解决方案1】:

    SomePanel 的构造函数只执行一次。

    您必须更改 SomePanel 始终显示最新数据的代码:

    public SomePanel( String id, IModel<Person> personModel)
    {
      tallLabel = new Label( "height", new LoadableDetachableModel() {
        pubic String getObject() {
          Person person = personModel.getObject();
          boolean isTall = apiCallToCheckIfTall( person );
      
          return isTall ? "Tall" : "Short";
        }
      });
    
      add(tallLabel);
    }
    

    【讨论】:

    【解决方案2】:

    您的活动名称有误,应该是

    new AjaxFormComponentUpdatingBehavior( "change" )
    

    【讨论】:

    • 嗯,谢谢您的回复。我有其他文本会在下拉选择更改时更新,所以我不确定这是否是问题。
    • 根据 Wicket 版本,“onchange”可能会起作用。根据您的描述,我了解到没有调用#onUpdate。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2019-03-04
    • 2012-02-20
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多