0

i have problem trying to get some values. this is my situation (wicket)

i have a dropdownchoice study_template, i don't have problem populating the DDC, the problem is when i try to get some value (id or name). this is the code

ChoiceRenderer choiceRenderer = new ChoiceRenderer("name", "id");
study_template  = new DropDownChoice( "study_template",new PropertyModel( this, "selectedTemplate" ),template_names , choiceRenderer);

template_names is a list< SelectOption > with the values obtain from a DB. this works OK.

this is the class that i use to populate the DDC

public class SelectOption implements java.io.Serializable {
    private long id; 
    private String  name;
    public SelectOption(long id, String name ) {
        this.id = id; this.name=name;
    }

     public long getId() 
        {return id; } 

        public String getName() 
        {return name; } 
}

normally i can get the value with study_template.getModelObject(), but in this case it doesn't work, i don't have any ideas about to obtain the id and the name , i know that i need GETID() and GETNAME(), but i don't know how to use it, any help will be appreciated

2 Answers2

0

You could use something as below:


public class SpikePage extends WebPage {

    class Person {

        String id;
        String name;

        public Person(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public SpikePage() {

        Person employee = new Person("E001", "ABC");
        Person manager = new Person("E002", "XYZ");

        List personList = new ArrayList(2);
        personList.add(employee);
        personList.add(manager);

        Form form = new Form("form");

        final DropDownChoice personDropDownChoice = new DropDownChoice("person", new ArrayList(personList), new IChoiceRenderer() {
            @Override
            public Object getDisplayValue(Person object) {
                return object.getId().concat("-").concat(object.getName());
            }

            @Override
            public String getIdValue(Person object, int index) {
                return object.getId();
            }
        });

        personDropDownChoice.setOutputMarkupId(true);
        personDropDownChoice.setNullValid(false);
        form.add(personDropDownChoice);

        final Label label = new Label("name");
        label.setOutputMarkupId(true);
        form.add(label);

        personDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                Person selectedPerson = personDropDownChoice.getModelObject();
                label.setDefaultModelObject("Hi ".concat(selectedPerson.getName()));
                target.add(label);
            }
        });

        add(form);
    }
}

Mihir
  • 270
  • 3
  • 9
0

Thanks for your answer, i already get it work

i use this

private SelectOption selectedTemplate;

and then

selectedTemplate.getId();