Wicket - 使用AjaxLink更新WebMarkupContainer

时间:2012-07-25 14:37:22

标签: java wicket

我在通过Ajax删除条目后尝试在PropertyListView上更新,但由于某种原因它无法工作。

我已将PropertyListView添加到WebMarkupContainer并将List添加到此视图。

有人可以提供帮助

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>

2 个答案:

答案 0 :(得分:3)

您正在使用LoadableDetachableModel,此模型每个RequestCycle调用一次load()方法,并保留transient个副本。当您使用WicketApplication.setEntrys(list);更新entrys列表时,您没有更新模型的transient实例。

直接使用IModel代替LoadableDetachableModel

答案 1 :(得分:0)

在更改数据后,在可拆卸模型上放下#detach()。