Now getting much closer - but not 100% there yet. 

Code with 3 dropdowns for area / country / region now looks like below. 

I don't set the model for the Panel, like:  

    setModel(((WholSession) Session.get()).getAreaModel());
                
I have 3 models - one for each dropdown - and not sure how to add them all
or combine them (or if this is possible).

I'm setting each of the models in the session.  The area list is always the
big master list of top level areas, and intially the others are empty, so: 

        private List<Area> areaList = ((WholApplication)
getApplication()).getAreas();
        private List<Area> countryList = Collections.EMPTY_LIST;
        private List<Area> regionList = Collections.EMPTY_LIST;

Then in the onchange in the AjaxFormComponentUpdatingBehavior I can do: 

        protected void onUpdate(AjaxRequestTarget target) {
             // Reset the country dropdown when the area changes
             Area area = ((AreaChoiceModel) areasDDC.getModel()).getArea();     
                
             List<Area> lst = new ArrayList<Area>(area.getAreas());             
        
             countriesDDC.setChoices(lst);                      
             ((WholSession) Session.get()).setCountryList(lst);
             countriesDDC.clearInput();
             regionsDDC.clearInput();
             target.addComponent(countriesDDC);
        }

which is manually setting the countryDDC choices by getting the countries
from the selected Area. 

This all works - selections survive page refreshes, going backwards and
forwards from the results page to the home page, etc. 

But the current problems are: 

1.  if I select an area + country + region combo - eg, N America - USA -
Alabama and sumbit, then change to a country with no recorded regions - eg,
N America - Bermuda - the region box clears OK with 

  regionsDDC.clearInput();       

but the region model in the session sticks as the previously stored Alabama. 
Obviously, I'm not setting the region model in the session correctly, and
I'm not sure how to make this automatic - hence the bits of hard coded stuff
I've resorted to to get this far. 

2.  The "please choose" options go missing after the first time I select
things - eg, selecting N America defaults to Bermuda. 

I suspect these issues can be solved if I can work out how to get multiple
models registered in the session so they update automatically like the
examples provided earlier.  




public class WholSession extends WebSession {

        private IModel areaModel = new AreaChoiceModel();
        private IModel regionModel = new AreaChoiceModel();
        private IModel countryModel = new AreaChoiceModel();
        
        private List<Area> areaList = ((WholApplication)
getApplication()).getAreas();
        private List<Area> countryList = Collections.EMPTY_LIST;
        private List<Area> regionList = Collections.EMPTY_LIST;
        
        public WholSession(Request request) {
                super(request);
        }

        protected void onDetach() {
                areaModel.detach();
                regionModel.detach();
                countryModel.detach();
        }

        public IModel getAreaModel() {
                return areaModel;
        }

        public List<Area> getAreaList() {
                return areaList;
        }

        public void setAreaList(List<Area> areaList) {
                this.areaList = areaList;
        }

        public IModel getRegionModel() {
                return regionModel;
        }

        public IModel getCountryModel() {
                return countryModel;
        }

        public List<Area> getCountryList() {
                return countryList;
        }

        public void setCountryList(List<Area> countryList) {
                this.countryList = countryList;
        }

        public List<Area> getRegionList() {
                return regionList;
        }

        public void setRegionList(List<Area> regionList) {
                this.regionList = regionList;
        }

        public void setRegionModel(IModel regionModel) {
                this.regionModel = regionModel;
        }
}



public class AreaChoiceModel extends Model {

        private Area area; 
        
        public AreaChoiceModel() {
        }
        
        public AreaChoiceModel(Area area) {
                logger.debug("AreaChoiceModel " + area);
                this.area = area;
        }
        
        public Object getObject() {
                return area;
        }

        public void setObject(Object object) {
                if (object != null) { 
                        area = (Area) object;   
                }
        }

        public void detach() {
                // do nothing - we want to save selected
                // area between sessions
        }

        public Area getArea() {
                return area;
        }
}


public class PanelSearch extends PanelBase {

        public PanelSearch(String id) {
                super(id);
                
                // setModel(((WholSession) Session.get()).getAreaModel());
                
                add(new SearchForm("searchForm"));
                add(new FeedbackPanel("feedback"));
        }

        private class SearchForm extends Form {

                public SearchForm(String id) {
                        super(id);
                        
                        // set the area dropdown
                        final DropDownChoice areasDDC = new 
DropDownChoice("areas",
                                        ((WholSession) 
Session.get()).getAreaModel(),
                                        ((WholSession) 
Session.get()).getAreaList(),
                                        new ChoiceRenderer("name", "id"));
                
                        areasDDC.setOutputMarkupId(true);
                        add(areasDDC);

                        // set the country dropdown
                        final DropDownChoice countriesDDC = new 
DropDownChoice("countries",
                                        ((WholSession) 
Session.get()).getCountryModel(),
                                        ((WholSession) 
Session.get()).getCountryList(),
                                        new ChoiceRenderer("name", "id"));
                        
                        countriesDDC.setOutputMarkupId(true);
                        add(countriesDDC);      
                                                
                        final DropDownChoice regionsDDC = new 
DropDownChoice("regions",
                                        ((WholSession) 
Session.get()).getRegionModel(),
                                        ((WholSession) 
Session.get()).getRegionList(),
                                        new ChoiceRenderer("name", "id"));
                        
                        regionsDDC.setOutputMarkupId(true);
                        add(regionsDDC);        
                        
                        areasDDC.add(new 
AjaxFormComponentUpdatingBehavior("onchange") {
                             protected void onUpdate(AjaxRequestTarget target) {
                                // Reset the country dropdown when the area 
changes
                                Area area = ((AreaChoiceModel)
areasDDC.getModel()).getArea();                 
                                List<Area> lst = new 
ArrayList<Area>(area.getAreas());                  
                                countriesDDC.setChoices(lst);                   
                                ((WholSession) 
Session.get()).setCountryList(lst);
                                countriesDDC.clearInput();
                                regionsDDC.clearInput();
                                target.addComponent(countriesDDC);
                             }
                        });
                        
                        countriesDDC.add(new 
AjaxFormComponentUpdatingBehavior("onchange") {
                            protected void onUpdate(AjaxRequestTarget target) {
                               // Reset the region dropdown when the country
changes
                               Area area = ((AreaChoiceModel)
countriesDDC.getModel()).getArea();                     
                               List<Area> lst = new 
ArrayList<Area>(area.getAreas());                   
                               regionsDDC.setChoices(lst);                      
                               ((WholSession) 
Session.get()).setRegionList(lst);                        
                               regionsDDC.clearInput();                 
                               target.addComponent(regionsDDC);
                            }
                       });
                }

                protected void onSubmit() {
                        AreaChoiceModel areaModel = (AreaChoiceModel) 
((WholSession) 
                           Session.get()).getAreaModel();
                        AreaChoiceModel countryModel = (AreaChoiceModel) 
((WholSession) 
                            Session.get()).getCountryModel();
                        AreaChoiceModel regionModel = (AreaChoiceModel) 
((WholSession) 
                             Session.get()).getRegionModel();

                        info("selected area : " + areaModel);
                        info("selected country : " + countryModel);
                        info("selected region : " + regionModel);

                        setResponsePage(new SearchResults());
                };
        }
}



-- 
View this message in context: 
http://www.nabble.com/Combining-Ajax-and-non-Ajax-pages-tp15587166p15641991.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to