Question

Is it possible to reorder items that have been added to a repeating view (more precisely a ListView) in Apache Wicket?

I tried reordering them in the attached list, like shown in the following code, but this had no effect:

int indexA = itemList.indexOf(itemA);
int indexB = itemList.indexOf(itemB);
itemList.set(indexA, itemB);
itemList.set(indexB, itemA);

As this had no effect I tried resetting the list property of the ListView:

listView.setList(itemList);

Of course I remembered to trigger an according repaint for the web page, but all in all it had no effect.

In some further attempts I tried to add a new item not to the end of the list but to the beginning:

itemList.add(0, newItem);

Instead of just

itemList.add(newItem);

While the latter one works (and always worked fine), the first obviously works for the first item but throws an exception for the second item.

Last cause: Unable to find component with id 'list-component' in [ListItem [Component id = 0]]
    Expected: 'list-container:list-items:0:list-component'.
    Found with similar names: 'list-container:list-items:1:list-component'

Where list-container is the WebMarkupContainer surrounding the ListView, list-items is the ListView itself and list-component is the id of the item to be added.

So, is it not possible to reorder items after they have been added to a repeating view? Can new items only be added at the end of it? Or am I missing something here, probably a class different than ListView that implements such features?

My main goal is to be able to reorder items, the "add at the beginning"-approach was just a test if it would at least work to remove the items from the view and re-add them at the desired position.

Was it helpful?

Solution

The link that has 'move Up' works like this, you could use this as inspiration :)

public final Link<Void> moveUpLink(final String id, final ListItem<T> item)
    {
        return new Link<Void>(id)
        {
            private static final long serialVersionUID = 1L;

            /**
             * @see org.apache.wicket.markup.html.link.Link#onClick()
             */
            @Override
            public void onClick()
            {
                final int index = item.getIndex();
                if (index != -1)
                {

                    addStateChange();

                    // Swap items and invalidate listView
                    Collections.swap(getList(), index, index - 1);
                    ListView.this.removeAll();
                }
            }

            @Override
            public boolean isEnabled()
            {
                return item.getIndex() != 0;
            }
        };
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top