Tommy Sadiq Hinrichsen
Tommy Sadiq Hinrichsen

Reputation: 804

Wicket - Update WebMarkupContainer with AjaxLink

Im trying to update at PropertyListView after i have deleted an entry via Ajax, but for some reason it wont work.

I have added the PropertyListView to a WebMarkupContainer and added the List to this view.

Can somebody help

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

@SuppressWarnings("serial")
public class StockViewPanel extends Panel{

    private WebMarkupContainer listContainer;
    @SuppressWarnings("rawtypes")
    private PropertyListView plw; 

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public StockViewPanel(String id) {
        super(id);

        IModel model = new LoadableDetachableModel() { 
            @Override 
            protected Object load() { 
                return WicketApplication.getStockEntrys();
            }};     

            plw = new PropertyListView("stockEntrys", model ) 
            { 
                @Override
                protected void populateItem(final ListItem item) 
                { 

                    //StockEntry stockEntry = (StockEntry) item.getModelObject(); 
                    item.add(new Label("name"));
                    //SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                    item.add(new Label("date"/*, formatter.format(stockEntry.getDate())*/)); 
                    //NumberFormat nf = NumberFormat.getInstance(new Locale("da", "dk"));
                    item.add(new Label("number"/*, nf.format(stockEntry.getNumber())*/)); 
                    item.add(new Label("price"/*, nf.format(stockEntry.getPrice())*/));
                    item.add(new AjaxLink("deleteEntryLink"){

                        @Override
                        public void onClick( AjaxRequestTarget target ) 
                        {
                            List list = new ArrayList(WicketApplication.getStockEntrys());
                            Object ob = item.getModelObject();
                            list.remove(ob);
                            WicketApplication.setEntrys(list);
                            target.add(listContainer);
                        }
                    });
                }


            };
            listContainer = new WebMarkupContainer("listContainer");
            listContainer.setOutputMarkupId(true);
            listContainer.add(plw);
            add(listContainer);
    }
}

<html>
<body>
    <wicket:panel>
        <table wicket:id="listContainer">
            <thead>
                <th>Navn</th>
                <th>Dato</th>
                <th>Antal</th>
                <th>Pris</th>
                <th></th>
            </thead>
            <tbody>
                <tr wicket:id="stockEntrys" class="stockEntry">
                    <td wicket:id="name">name</td>
                    <td wicket:id="date">date</td>
                    <td wicket:id="number">number</td>
                    <td wicket:id="price">price</td>
                    <td> <a href="#" wicket:id="deleteEntryLink">DELETE</a> </td> 
                </tr>
            </tbody>
        </table>
    </wicket:panel>
</body>
</html>

Upvotes: 0

Views: 4046

Answers (2)

svenmeier
svenmeier

Reputation: 5681

Fall #detach() on your detachable model after changing its data.

Upvotes: 0

jordeu
jordeu

Reputation: 6821

You are using a LoadableDetachableModel, this model calls load() method once per RequestCycle and keeps a transient copy of it. When you are updating the entrys list with a WicketApplication.setEntrys(list); you are not updating the transient instance of the model.

Use directly an IModel instead of LoadableDetachableModel.

Upvotes: 3

Related Questions