jacop41
jacop41

Reputation: 152

How do I chain Wicket dropdown list events?

I'm trying to chain dropdown list events.

I have these data:

Name auidi (a4{ 2006,2007,2008 } , a6{ 2006,2007,2009 } ,tt { 2005,2008,2010 }) ford (crown {2009,2010} , escape{2008,2009})

When I select the name of the car I want models to be shown. Later I select the model, and I want years to be shown.

I've found this example: http://wicketstuff.org/wicket14/nested/?wicket:bookmarkablePage=:org.apache.wicket.examples.ajax.builtin.ChoicePage

But there, there are two dropdowns. There is also a map. Should I use a map in a map? If so, how can I set the model?

When the map is in the type Map<String ,List<String >>, the model is like:

IModel<List<? extends String>> makeChoices = 
    new AbstractReadOnlyModel<List<? extends String>>() 
    { 
        @Override 
        public List<String> getObject() 
        { 
            Set<String> keys = modelsMap.keySet(); 
            List<String> list = new ArrayList<String>(keys); 
            return list; 
        } 
    }; 

What if map is Map<String,Map<String ,List<String >>> ?

If I code

IModel<Map? extends String>> makeChoices = new AbstractReadOnlyModel<Map<? extends String>>() 

My IDE warns me that it expects two arguments, and I don't know how to assign the values to the map in the model.

Upvotes: 1

Views: 1093

Answers (1)

zeratul021
zeratul021

Reputation: 2694

In your case for 3 DropDownChoices you will need 3 models and all of them need to represent lists which provide choices. You need nested maps to simulate their relationships and later for retrieval, e.g:

Map<String, Map<String, List<String>>> cars; //fill data
IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>() {
        @Override
        public List<String> getObject() {
            Set<String> keys = cars.keySet();
            List<String> list = new ArrayList<String>(keys);
            return list;
        }
    };

IModel<List<? extends String>> modelChoices = new AbstractReadOnlyModel<List<? extends String>>() {
        @Override
        public List<String> getObject() {
            Map<String, List<String>> models = cars.get(selectedMake);
            if (models == null) {
                return Collections.emptyList();
            } else {
                List<String> list = new ArrayList<String>(models.keySet());
                return list;
            }
        }
    };

 IModel<List<? extends String>> yearChoices = new AbstractReadOnlyModel<List<? extends String>>() {
        @Override
        public List<String> getObject() {
            Map<String, List<String>> models = cars.get(selectedMake);
            if (models == null) {
                return Collections.emptyList();
            } else {
                return models.get(selectedModel);
            }
        }
    };

Upvotes: 2

Related Questions