开发者

wicket - Implement Ajax add/remove items ListView

Im getting crazy about this issue. I implemented a ListView which you can add/remove TextField dinamically, but only the last TextField is removed.

An example:

// Object type which is used in the list
public class ExampleObject implements Serializable{
    private String keyword;

    public String getKeyword() {
        return this.keyword;
    }

    public void setKeyword(String s) {
        keyword = s;
    }
}

//ListView
List<ExampleObject> keywordList = new ArrayList<ExampleObject>();
keywordList.add(new ExampleObject());

ListView keywordView = new ListView("keywordView", keywordList) {
    @Override
    protected void populateItem(final ListItem item) {
            ExampleObject model = (ExampleObject) item.getModelObject();
            item.add(new TextField("subKeyword", new PropertyModel(model, "keyword")));

            // keyword remove link
            AjaxSubmitLink removeKeyword = new AjaxSubmitLink("removeKeyword", myForm)         
            {
                @Override
                protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                    ExampleObject selected = (ExampleObject) item.getModelObject();
                    // I also tried deleting by index. println shows the
                    // selected object is the element I want to remove, so why always
                    // remove last object of the list?
                    keywordList.remove(selected);
                    if (target != null) {
                        target.addComponent(myForm);
                    }
                }
            };

            item.add(removeKeyword);

            // keyword add link
            AjaxSubmitLink addKeyword = new AjaxSubmitLink("addKeyword", metadataForm)      
                {
                @Override
                protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                    keywordList.add(new ExampleObject());
    开发者_如何学Python                if (target != null) {
                        target.addComponent(myForm);
                    }
                }
            };
            item.add(addKeyword);
}
keywordView.setReuseItems(true);
metadataForm.add(keywordView);

Any help would be very appreciate, because I thing this issue is really a very stupid mistake but I cant get it!

Thanks


It might be as simple as getting rid of the line

keywordView.setReuseItems(true);

The reuseItems flag is an efficiency so that the page does not rebuild the ListView items unnecessarily, but it can lead to confusion such as what you're seeing.

ListView really wasn't made for use with forms though, and you'll probably be better off with another tactic entirely.

This blog entry on building a list editor form component might be useful. It will need some changes if you're not on Wicket 1.4, but similar stuff is definitely possible in Wicket 1.3, and the comments have some hints.


Read the javadoc of ListView#setReuseItems():

"But if you modify the listView model object, than you must manually call listView.removeAll() in order to rebuild the ListItems."


You can not use a ListView this way. Either use the members of ListView provided:

removeLink(java.lang.String id, ListItem<T> item)

and

newItem(int index)

but, I never used those. If I have to display a List and be able to add remove Items dynamically, I prefer the RefreshingView.

If you do use FormComponents inside a RefreshingView, make sure you set a Reusestartegy (setItemReuseStrategy())

Bert

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜