See if this helps. I think your code is overly complicated. The attached
example adds books to a listview and also allows to remove selected ones.

Regards,
Carlos

On 8/27/07, pokkie <[EMAIL PROTECTED]> wrote:
>
>
> Carlos,
>
> I kind of got the code to work, but it doesn't feel right. Any help would
> be
> much appreciated
>
>    1. public void buildView() {
>    2.
>    3.         final BookListView bookListView = new
> BookListView("bookListView", new ArrayList());
>    4.
>    5.         Button addBookButton = new Button("addBookButton") {
>    6.             protected void onSubmit() {
>    7.                 String selectedBookName = "The Travels Of John";
>    8.                 Book book = new Book();
>    9.                 book.setBookName(selectedBookName);
>   10.
>   11.                 List<Book> books = new ArrayList<Book>();
>   12.                 books.add(book);
>   13.
>   14.                 BookModel bookModel = new BookModel(books);
>   15.
>   16.                 ListItem bookItem = new
> ListItem(bookListView.getModelIndex(), bookModel);
>   17.                 bookListView.populateItem(bookItem);
>   18.                 bookListView.setModel(bookModel);
>   19.                 bookListView.modelChanged();
>   20.             }
>   21.         };
>   22.     }
>   23.
>   24. private class BookModel implements IModel {
>   25.
>   26.         private List bookList;
>   27.
>   28.         public BookModel(List bookList) {
>   29.             this.bookList = bookList;
>   30.         }
>   31.
>   32.         public IModel getNestedModel() {
>   33.             return null;
>   34.         }
>   35.
>   36.         public Object getObject(final Component component) {
>   37.             return bookList;
>   38.         }
>   39.
>   40.         public void setObject(final Component component, final
> Object
> object) {
>   41.         }
>   42.
>   43.         public void detach() {
>   44.         }
>   45.     }
>   46.
>   47.     private class BookListView extends ListView {
>   48.
>   49.         private int modelIndex;
>   50.
>   51.         public BookListView(final String id, final List list) {
>   52.             super(id, list);
>   53.         }
>   54.
>   55.         protected void populateItem(final ListItem listItem) {
>   56.
>   57.             if (listItem.getModelObject() instanceof Book) {
>   58.                 Book book = (Book) listItem.getModelObject();
>   59.
>   60.                 Label bookName = new Label("bookName", new
> PropertyModel(book, "bookName"));
>   61.                 listItem.add(bookName);
>   62.
>   63.                 setModelIndex(getModelIndex() + 1);
>   64.             }
>   65.         }
>   66.
>   67.
>   68.         public int getModelIndex() {
>   69.             return modelIndex;
>   70.         }
>   71.
>   72.         public void setModelIndex(int modelIndex) {
>   73.             this.modelIndex = modelIndex;
>   74.         }
>   75.     }
>   76.
>   77.     private class Book implements Serializable {
>   78.         private int id;
>   79.         private String bookName;
>   80.         private boolean selected;
>   81.
>   82.
>   83.         public int getId() {
>   84.             return id;
>   85.         }
>   86.
>   87.         public void setId(int id) {
>   88.             this.id = id;
>   89.         }
>   90.
>   91.         public String getBookName() {
>   92.             return bookName;
>   93.         }
>   94.
>   95.         public void setBookName(String bookName) {
>   96.             this.bookName = bookName;
>   97.         }
>   98.
>   99.         public boolean isSelected() {
> 100.             return selected;
> 101.         }
> 102.
> 103.         public void setSelected(boolean selected) {
> 104.             this.selected = selected;
> 105.         }
> 106.     }
>
>
> Carlos Pita-4 wrote:
> >
> >>
> >>
> >> I am currently trying to convert the RepeatingView into a ListView, but
> I
> >> am
> >> having problems
> >> setting the model on the ListView. I call populateItem with a model,
> but
> >> afterwards the model
> >> is not being set on the view.
> >
> >
> > That's funny. Could you post the related code?
> >
> > Regards,
> > Carlos
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/RepeatingView-%3A-Removing-a-table-row-tf4331444.html#a12356657
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
package web.books;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;

@SuppressWarnings("serial")
public class BooksPage extends WebPage {

    private static class Book implements Serializable {
        
        private boolean selected = false;
        private String name;
        
        public Book(String name) {
            this.name = name;
        }
        
        public boolean isSelected() {
            return selected;
        }
        
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
        
        public String getName() {
            return name;
        }
    }
    
    private class BooksForm extends Form {
    
        private List<Book> books; 
        private String newName;
        
        public BooksForm(String id) {
            super(id);
            initializeBooks();
            addBooksView();
            add(new TextField("newName", new PropertyModel(this, "newName")));
            addAddButton();
            addRemoveButton();
        }

        private void initializeBooks() {
            books = new ArrayList<Book>();
            books.add(new Book("Voyage au bout de la nuit"));
            books.add(new Book("A confederacy of dunces"));
            books.add(new Book("The trial"));
            books.add(new Book("Memorias del subsuelo"));
        }

        private void addBooksView() {
            add(new ListView("books", new PropertyModel(this, "books")) {
                protected void populateItem(ListItem item) {
                    Book book = (Book)item.getModelObject();
                    item.add(new Label("name", new PropertyModel(book, "name")));
                    item.add(new CheckBox("selected", new PropertyModel(book, "selected")));
                }
            });
        }

        private void addAddButton() {
            add(new Button("add") {
                public void onSubmit() {
                    books.add(new Book(newName));
                }                
            });
        }

        private void addRemoveButton() {
            add(new Button("remove") {
                public void onSubmit() {
                    Iterator<Book> bookIterator = books.iterator();
                    while (bookIterator.hasNext()) {
                        if (bookIterator.next().isSelected()) {
                            bookIterator.remove();
                        }
                    }
                }                
            });
        }
    }
        
    public BooksPage() {
        add(new BooksForm("form"));
    }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to