0

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.

jjkl
  • 353
  • 6
  • 15

2 Answers2

1

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);
}
svenmeier
  • 5,681
  • 17
  • 22
  • Thanks for the suggestion. I have a follow up question I posted here: https://stackoverflow.com/questions/67475764/parameterizing-strings-in-a-model-not-working. Would appreciate your thoughts. – jjkl May 10 '21 at 18:25
0

Your event name ist wrong, it should be

new AjaxFormComponentUpdatingBehavior( "change" )
svenmeier
  • 5,681
  • 17
  • 22
  • Hmm, thanks for your response. I have other text that updates when dropdown selection changes so I'm not sure if this is the issue. – jjkl May 09 '21 at 16:28
  • Depending on the Wicket version "onchange" might work. From you description I understood that #onUpdate is not called. – svenmeier May 09 '21 at 20:14