开发者

How can I create a dropdown list in Wicket?

开发者 https://www.devze.com 2023-01-23 16:00 出处:网络
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.

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开发者_StackOverflow社区<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.

0

精彩评论

暂无评论...
验证码 换一张
取 消