wicket:how to get dropdownlist selected value in ListView

1.1k Views Asked by At

i have a page,there are some dropdown.when page was load,accord the list,there will show some dropdown.now want to get the dropdownlist selected value in listview,but i can't get the dropdownchoice seleceted value.how can i do to get this value?

Can anyone tell me how to achieve this.

html code:

<table>
    <tr wicket:id="tritems">
        <th align="right"><span wicket:id="lblattr"></span></th>
        <td><select wicket:id="attrvalue"></select></td>
    </tr>
    <tr><th align="right"><wicket:message key="targetsystem" /></th><td><select wicket:id="targetsystem" /></td></tr>
</table>

java code:

final ListView trView=new ListView("tritems", new PropertyModel(this, "attrBizRoles")) { 
        private IBizRole attrvalueBizRole=new BizRole();    

        @Override 
        protected void populateItem(ListItem item) { 
                attrBizRole = (IBizRole) item.getModelObject(); 
                item.add(new Label("lblattr", attrBizRole.getName())); 
                // this list can get from attr 
                attrvalueBizRoles = (List<IBizRole>) attrBizRole.getChildBizRole(); 
                if (attrvalueBizRoles.size()>0) { 
                        attrvalueBizRole=attrvalueBizRoles.get(0); 
                } 
                DropDownChoice attrvalueChoice = new DropDownChoice("attrvalue",new PropertyModel<IBizRole>(this, "attrvalueBizRole"), attrvalueBizRoles,new IChoiceRenderer() { 

                        @Override 
                        public Object getDisplayValue(Object object) { 
                                attrvalueBizRole = (IBizRole) object; 
                                return attrvalueBizRole.getName(); 
                        } 

                        @Override 
                        public String getIdValue(Object object, int index) { 
                                attrvalueBizRole = (IBizRole) object; 
                                return String.valueOf(attrvalueBizRole.getId()); 
                        } 
                }); 
                item.add(attrvalueChoice); 
        } 
}; 

Thanks.

2

There are 2 best solutions below

1
On

PropertyModel(this, "attrvalueBizRole")

It seems all your items write into the same field "attrvalueBizRole" of your list view. This probably is not what you intended.

Don't your IBizRole has a property, you can bind the drop downs to directly?

0
On

Before to add "ListView" add a new HiddenField. it will store the selected value:

//Note:: your model have to have "getRowSelected()" and "setRowSelected()" methods and "String rowSelected" field...  
add(new HiddenField<String>("rowSelected", new PropertyModel<String>(getModelObject(), "rowSelected"))){    
        @Override
        protected void onInitialize() {
            add(new AjaxFormComponentUpdatingBehavior("change") {

                @Override
                protected void onEvent(AjaxRequestTarget target) {
                    getValue();//This method will return your selected value
                }
            });
        }
    });

In your populateItems() you have to set "data-value" attribute:

@Override 
protected void populateItem(ListItem item) { 
    //TODO::
    item.add(new AttributeModifier("data-value",attrvalueBizRole.getId())
    //TODO::
} 

In your HTML page you have to add an hidden-field just before your ListView

<input type="hidden" name="rowSelected" wicket:id="rowSelected"/>//<--
<tr wicket:id="tritems">
    <th align="right"><span wicket:id="lblattr"></span></th>
    <td><select wicket:id="attrvalue"></select></td>
</tr>