0

在我通过 Ajax 删除一个条目后,我试图在 PropertyListView 进行更新,但由于某种原因它不起作用。

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

有人可以帮忙吗

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>
4

2 回答 2

3

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

直接使用 anIModel而不是LoadableDetachableModel.

于 2012-07-25T16:00:15.047 回答
0

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

于 2012-07-26T05:07:00.033 回答