在下拉选择的 onChange 中未触发 API 调用 [英] API call not triggered in onChange of dropdown selection
问题描述
我在父页面中定义了一个下拉列表,我想在其中添加额外的行为,这样每当从下拉列表中选择一个选项时,就会进行调用以确定标志的值 (isTall
) 然后使用这个标志来决定是否显示额外的文本.
I have a dropdown defined in a parent page where I'd like to add additional behaviour such that whenever an option from a dropdown is selected, a call is made to determine the value of a flag (isTall
) and then use this flag to determine whether or not to show additional text.
ParentPage.java
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
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 调用仅在页面首次加载时进行一次.在下拉列表中进行选择时,不会触发调用.我不确定为什么.
Whilst debugging, the API call is made only once when the page first loads. When a selection is made in the dropdown, the call isn't triggered. I'm not really sure why.
推荐答案
SomePanel的构造函数只执行一次.
The constructor of SomePanel is executed once only.
您必须更改 SomePanel 始终显示最新数据的代码:
You have to change your code that SomePanel always shows up-to-date data:
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);
}
这篇关于在下拉选择的 onChange 中未触发 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!