ListView中的组件不会在AJAX刷新时更新

时间:2012-11-21 03:23:25

标签: java wicket

我有一个Wicket面板,其中包含一个ListView,然后是子项(表单控件),但是当我按下一个内部CheckBox时,某些子项的可见性应该会改变。

但是,在AJAX更新期间重绘ListView之后,调用WebMarkupContainer.setVisible(false)不会隐藏ListView中的项目。

以下代码:

public class ImagePanel extends Panel {

private ArrayList<ImageEntry> imageEntryList;

public class ImageEntry implements Serializable {

    private static final long serialVersionUID = -3987685200930059655L;

    public String thumbnail;
    public String filename;
    public boolean webDownloaded;
    public WebMarkupContainer fileUpload;
    public WebMarkupContainer webDownload;
}

public ImagePanel(String id) {
    this(id, IMAGE_NORMAL);
}

public ImagePanel(String id, int type) {
    super(id);
    this.type = type;

    wmc = new WebMarkupContainer ("wmc");
    wmc.setOutputMarkupId(true);
    add(wmc);

    imageEntryList = new ArrayList<ImageEntry>();
    ImageEntry imageEntry = new ImageEntry();
    imageEntry.thumbnail = "blah";
    imageEntry.filename = "blah";
    imageEntryList.add(imageEntry);

    ListView<ImageEntry> llv = new LargeImageListView("large_image_list", imageEntryList);
    wmc.add(llv);

    SmallImageListView slv = new SmallImageListView("small_image_list", imageEntryList);
    wmc.add(slv);
}

private final class SmallImageListView extends ListView<ImageEntry> {

    private SmallImageListView(String id, List<? extends ImageEntry> list) {
        super(id, list);
    }

    @Override
    protected void populateItem(final ListItem<ImageEntry> item) {

            ... 

        if (type == IMAGE_WIZARD) {
            item.getModelObject().fileUpload = showWizardFileUpload(item);
            item.getModelObject().webDownload = showWizardWebDownload(item);
            showSortUpDown(item);
            showWebCheckbox(item);
        }

    }
}


private void showWebCheckbox(final ListItem<ImageEntry> item) {

    AjaxCheckBox checkbox = new AjaxCheckBox("use_web_image", new PropertyModel<Boolean>(item.getModelObject(), "webDownloaded")) {

        public void onUpdate(AjaxRequestTarget target) {

            if (getModelObject()) {
                System.out.println("Show");
                item.getModelObject().fileUpload.setVisible(false);
                item.getModelObject().webDownload.setVisible(false);
            } else {
                System.out.println("Hide");
                item.getModelObject().fileUpload.setVisible(false);
                item.getModelObject().webDownload.setVisible(false);
            }

            target.add(wmc);
        }

    };

    item.add(checkbox);
}   

    ...

}

2 个答案:

答案 0 :(得分:2)

使用ListView.setReuseItems(true)确保ListView中的对象被正确序列化...否则,每次都会收到一个不同的对象,并且.setVisible()属性将被重置为其默认值(例如'真')。

slv.setReuseItems(true);

答案 1 :(得分:1)

尝试

WebMarkupContainer.add(new AttributeModifier("style", new Model("display:none"))); 

WebMarkupContainer.add(new AttributeAppender("style", new Model("display:none"), "="));

代替。