views:

301

answers:

2

Hi!

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());
                    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

A: 

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.

Don Roby
I don't think so. When using forms in a list view you must reuse the list items, or else the form components can't be found.
seanizer
Well, I tried and i worked!But I think that bert says I cant use a ListView for this purpose because validation.
jOki
I'm not by any means surprised that it worked. But bert and seanizer are both right that it's not advised. If you can make sense of the stuff in the blog entry I linked, it's probably better than using either a ListView or RefreshingView. And that blog is by one of the authors of Wicket.
Don Roby
Well, finally Im gonna implement it manually because I got some ajax validation errors using any of this methods.Thanks u all!
jOki
A: 

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

removeLink(java.lang.String id, ListItem 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

bert
I knew about removeLink, but it works without AJAX!Abour refreshingView... I was searching in google and I didnt find so mucho info about that, just an example in wicket Live Examples and I got a "Expired web".Could you show me how I could convert my code from ListView to RefreshinfView?Thanks mate!
jOki
Is this still active? if so, i could sit down and write an example.
bert