Question

Im populating a table using ListView component in wicket.The last column of my table is button. So for each row I'll have a button in the last column.What I'm trying to implement is onlick of the button I need to delete the appropriate row. So for this I need to get the current index of the list on click of button. How to achieve/get this ?

Was it helpful?

Solution

I would extend Ajax button and pass the row reference (item) in the constructor...then you can do anything you want..by overriding the onSubmit method

Example:

private class SpecialButton extends AjaxButton {
    final Item<Object> rowItem;

    public SpecialButton(final String id, final Item<Object> rowItem) {
        super(id);

        this.rowItem = rowItem;
    }

    @Override
    protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
        // here you cand do everything you want with the item and the model object of the item.(row)
        Object object = rowItem.getModelObject();
    }

}

You should replace Object from Item<Object> with your reapeater model. After creating this private class you can reuse it for every row in your repeater.

If you want to delete that row you just have to remove the model from the list used to generate the repeater and refresh the repeater container(Wicket does not allow you to refresh the repeater by adding it to the target...instead you have to add the repeater continer.)

OTHER TIPS

Have a look at the repeaters Wicket Examples page to understand how to use ListView and other repeaters:

http://www.wicket-library.com/wicket-examples/repeater/

You can get the current index of the list from item.getIndex()

protected void populateItem(final ListItem<T> item) {
    int index = item.getIndex();
    ...  

Look here for inspirations on how to do it properly (without index):

Wicket ListView not refreshing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top