0

I have created a list view which will show number of records in table view and inside the listview contains a textfield and a dropdownchoice field.

What I need to achieve is onchange of the textfield to default the value in the dropdownchoice for a particular record. But appears that when using the following codes, when its only 1 record to be updated, it works, but when multiple records exist, it will fail. Need help! Thanks in advance.

Sample Code:

new ListView("list", getItemList()) {
    protected void populateItem(ListItem item) {
        final Sl qv = (Sl) item.getModelObject();
        item.setModel(new CompoundPropertyModel(qv));

        ratio = new TextField("ratio");
        item.add(ratio);
        ratio.setOutputMarkupId(true);

        ratio.add(new AjaxFormComponentUpdatingBehavior("onkeyup") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                getProperties().put("rating" + Sl.getid(), "Strong");
                target.addComponent(rating);
            }
        });

        rating = new DropDownChoice("rating", getDdlmodels().get(item.getIndex()), ratingList);
        item.add(rating);
        rating.setOutputMarkupId(true);
    }
}

Note: PropertyModel with ValueMap properties as key (DdlModels) has been used to get and set value of the dropdownchoice. Upon onchange of the textfield, the propertyModel has been updated, however the dropdownchoice has not been refresh/re-rendered even though I have added for ajax refresh.

sschong
  • 13
  • 3
  • You need to add your components to the list view item. So instead of add(..) do a item.add(). and get rid of that anoying add(varaible = new ..). Its hard to read. – bert Apr 16 '13 at 09:53
  • Thanks for the reply. Actually its already item.add(), missed that out while constructing a simple version of the codes. Thanks for pointing out. Actually the above codes working fine when only got 1 record but failed when multiple records, I believe its something related to the target.add(rating), which I think can't be refreshed in this way, but I can't find other way. – sschong Apr 16 '13 at 10:03

1 Answers1

0

The problem is that you "bind" all textfields to the last "rating" since it's a private member of your class.

When the "onUpdate" behavior gets called, it will use the rating member that is declared outside the scope of the populate item method thus the one assigned at the last iteration of the listview.

That's why it works for one record. Your current implementation should work for the last "row" only.

The "rating" variable should be declared inside the populate item method.

Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35
  • Yes! thanks for the reply, it works after I change variable declaration inside the populate item method. Thanks again!. – sschong Apr 23 '13 at 02:44