简体   繁体   中英

How can I create a dropdown list in Wicket?

I'm trying to populate items in a dropdown list.

I've achieved getting values from my database, but I can't add them in a dropdown list.

public List<String> getNames(){
        List<String> polNames = new LinkedList<String>();
                try {
            String query ="SELECT names FROM clinics";
            Statement statement = this.connection.createStatement();
            ResultSet result = statement.executeQuery(query);
            while(result.next()){
                polNames.add(result.getString("name"));
            }
        }
        catch(SQLException ex){
            throw new UnsupportedOperationException(ex.getMessage());
        }
        return polNames;
    }

Above, I assigned the values to polNames . I want polNames to be added to the dropdown list.

Also, when clicking a value from list, I want to do a new query. How can I achieve this?

To create the list, you can use the DropDownChoice class . Here's a sample of what it might look like:

DropDownChoice<String> clinicNames =
    new DropDownChoice<String>("clinicNames",
                               new PropertyModel<String>(this, "selectedName"),
                               polNames) {
        // @Override
        // public void onSelectionChanged() {
        //     // Generate and submit your query here
        // }
    };

// EDIT: onSelectionChanged() is a final method; use this instead
OnChangeAjaxBehavior clinicNamesListener = new OnChangeAjaxBehavior() {
    protected void onUpdate(AjaxRequestTarget target) {
        // Generate and submit your query here
    }
};
clinicNames.add(clinicNamesListener);

This would make a new dropdown list with Wicket ID of "clinicNames," populated with the values in polNames , that updates the selectedName value in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM