API 调用未在下拉选择的 onChange 中触发
API call not triggered in onChange of dropdown selection
我在父页面中定义了一个下拉菜单,我想在其中添加额外的行为,以便每当从下拉菜单中选择一个选项时,都会进行调用以确定标志的值(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 调用仅在页面首次加载时调用一次。在下拉列表中进行选择时,不会触发调用。我不太确定为什么。
你的活动名称有误,应该是
new AjaxFormComponentUpdatingBehavior( "change" )
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);
}
我在父页面中定义了一个下拉菜单,我想在其中添加额外的行为,以便每当从下拉菜单中选择一个选项时,都会进行调用以确定标志的值(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 调用仅在页面首次加载时调用一次。在下拉列表中进行选择时,不会触发调用。我不太确定为什么。
你的活动名称有误,应该是
new AjaxFormComponentUpdatingBehavior( "change" )
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);
}