jjkl
jjkl

Reputation: 403

API call not triggered in onChange of dropdown selection

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

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);
}

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.

Upvotes: 0

Views: 133

Answers (2)

svenmeier
svenmeier

Reputation: 5681

The constructor of SomePanel is executed once only.

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);
}

Upvotes: 1

svenmeier
svenmeier

Reputation: 5681

Your event name ist wrong, it should be

new AjaxFormComponentUpdatingBehavior( "change" )

Upvotes: 0

Related Questions