You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Toscano <ko...@gmail.com> on 2007/11/09 08:22:20 UTC

Enable and Disable using Ajax

Hello,

I found some questions related to this topic, but I couldn't make it to
work. 
I have two dropdowns, countries and regions. The easy thing is that I load
the regions when the country has been selected, and I use
AjaxFormComponentUpdatingBehavior for this and works.
The thing is that we don't have Regions for all the countries, so in that
cases I want to disable the dropdown.

This is the code I have:


// countryWork is the first dropdown
countryWork.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
            protected void onUpdate(AjaxRequestTarget target)
            {        target.addComponent(regionWork);            }
});

// and regionWork is the region

 IModel regionModelChoices = new AbstractReadOnlyModel()
        {
            public Object getObject(Component component)
            {
               if (professionalInfo.getCountryWork()!=null)
                     regions =
getRegionDaoInterface().getRegions(professionalInfo.getCountryWork().getCountryID());
                  if (regions.size()==0)
                           regionWork.setEnabled(false);
               else
                           regionWork.setEnabled(true);
               return regions;
            }
        };

          regionWork  = new DropDownChoice("regionWork", new Model(),
regionModelChoices,
          new ChoiceRenderer("regionName", "regionID"));
          regionWork.setOutputMarkupId(true);

The dropdown changes, but with one refresh delay. For example, I have
regions for Canada but not for Spain. If I change to Canada, nothing
happens, but the next change in the country will enable the dropdown.

Any ideas?

Thank you very much for your time,
Oskar
-- 
View this message in context: http://www.nabble.com/Enable-and-Disable-using-Ajax-tf4776221.html#a13662524
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Enable and Disable using Ajax

Posted by Toscano <ko...@gmail.com>.
Hi again,

That works fine for me! Now I understand how it should be.

Thank you very very much,
Oskar



Dmitry   Kandalov wrote:
> 
> On Friday 09 November 2007 12:24:12 Dmitry Kandalov wrote:
>> Probably that is because this component is already rendered.
> 
> I mean the component has been already checked for being enabled :)
> 
> 
> Dima
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Enable-and-Disable-using-Ajax-tf4776221.html#a13719466
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Enable and Disable using Ajax

Posted by Dmitry Kandalov <no...@gmail.com>.
On Friday 09 November 2007 12:24:12 Dmitry Kandalov wrote:
> Probably that is because this component is already rendered.

I mean the component has been already checked for being enabled :)


Dima

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Enable and Disable using Ajax

Posted by Dmitry Kandalov <no...@gmail.com>.
On Friday 09 November 2007 11:22:20 Toscano wrote:
> Hello,
>
> I found some questions related to this topic, but I couldn't make it to
> work.
> I have two dropdowns, countries and regions. The easy thing is that I load
> the regions when the country has been selected, and I use
> AjaxFormComponentUpdatingBehavior for this and works.
> The thing is that we don't have Regions for all the countries, so in that
> cases I want to disable the dropdown.
>
> This is the code I have:
>
>
> // countryWork is the first dropdown
> countryWork.add(new AjaxFormComponentUpdatingBehavior("onchange")
> {
>             protected void onUpdate(AjaxRequestTarget target)
>             {        target.addComponent(regionWork);            }
> });
>
> // and regionWork is the region
>
>  IModel regionModelChoices = new AbstractReadOnlyModel()
>         {
>             public Object getObject(Component component)
>             {
>                if (professionalInfo.getCountryWork()!=null)
>                      regions =
> getRegionDaoInterface().getRegions(professionalInfo.getCountryWork().getCou
>ntryID()); if (regions.size()==0)
>                            regionWork.setEnabled(false);
>                else
>                            regionWork.setEnabled(true);
>                return regions;
>             }
>         };
>
>           regionWork  = new DropDownChoice("regionWork", new Model(),
> regionModelChoices,
>           new ChoiceRenderer("regionName", "regionID"));
>           regionWork.setOutputMarkupId(true);
>
> The dropdown changes, but with one refresh delay. For example, I have
> regions for Canada but not for Spain. If I change to Canada, nothing
> happens, but the next change in the country will enable the dropdown.
>
> Any ideas?
>
> Thank you very much for your time,
> Oskar

Probably that is because this component is already rendered.

In general you shouldn't change components state inside a model because this 
code is called at render stage. In your case you can call 
regionWork.setEnabled() inside onUpdate(AjaxRequestTarget target).


Dima

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Enable and Disable using Ajax

Posted by Timo Rantalaiho <Ti...@ri.fi>.
On Thu, 08 Nov 2007, Toscano wrote:
>  IModel regionModelChoices = new AbstractReadOnlyModel()
>         {
>             public Object getObject(Component component)
>             {
>                if (professionalInfo.getCountryWork()!=null)
>                      regions =
> getRegionDaoInterface().getRegions(professionalInfo.getCountryWork().getCountryID());

You could extract a method of that monster :)
For example private boolean selectedCountryHasRegions(), or
even professionalInfo.getCountryWork().hasRegions(getRegionDao());

>                   if (regions.size()==0)
>                            regionWork.setEnabled(false);
>                else
>                            regionWork.setEnabled(true);

And instead of testing for size == 0 call isEmpty(), and the
formatting here is kind of unconventional as well (Eclipse
3.3 has the nice new possibility of adding Save actions such
as reformat code).

>           regionWork  = new DropDownChoice("regionWork", new Model(),
> regionModelChoices,
>           new ChoiceRenderer("regionName", "regionID"));
>           regionWork.setOutputMarkupId(true);A

regionWork = new DropDownChoice(...) {
    @Override
    public boolean isEnabled() {
        return selectedCountryHasRegions();
    }

or it it doesn't work

regionWork = new DropDownChoice(...) {
    @Override
    public void onBeforeRender() {
        super.onBeforeRender();
        setEnabled(selectedCountryHasRegions());
    }

Best wishes,
Timo

-- 
Timo Rantalaiho           
Reaktor Innovations Oy    <URL: http://www.ri.fi/ >

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org