You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Thomas Kappler <tk...@googlemail.com> on 2007/12/04 09:28:16 UTC

Getting position of elements in a ListView

Hi all,

I use a ListView holding Components to implement a dynamic form, i.e.,
where the user can add and remove DropDownChoices herself.  (See
http://cwiki.apache.org/WICKET/forms-with-dynamic-elements.html.)

Now, a problem is the removal of elements when the user clicks the
"remove" link next to a DDC.  My original code looks like that:

ListView menuList = new ListView(MENU_LIST_ID, dropdownMenus) {
    public void populateItem(final ListItem item) {
        final DropDownChoice choice = (DropDownChoice)item.getModelObject();
        item.add(choice);
        AjaxFallbackLink removeMenuLink = new
AjaxFallbackLink(REMOVE_MENU_LINK_ID) {
            public void onClick(final AjaxRequestTarget target) {
                // Parent is the ListView, its model object is the
list of menus.
                List<PositionAwareDropDownChoice> choices =
                        (List<PositionAwareDropDownChoice>)
item.getParent().getModelObject();
                choices.remove(choice);							
                target.addComponent(enclosingContainer);
            }
        };
        removeMenuLink.add(new Label(REMOVE_MENU_LINK_TEXT_ID,
removeMenuLinkText));
        item.add(removeMenuLink);
}

However, choices.remove(choice) doesn't always work because the list
doesn't seem to contain the DDC "choice", i.e., equals() is not true.

So I thought of removing the DDC by their position in the list, but
that means I have to store it myself.  Currently, I extended DDC,
adding getPos() and setPos().  The line where the removal happens now
looks like that:
choices.remove( ((PositionAwareDropDownChoice)choice).getPos() );

It seems a bit clunky and verbose to me.  Does anyone know of a better way?

Cheers,
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org