Wicket ListView 不刷新
我正在迈出使用 Apache Wicket 的第一步,并遇到了以下问题。我有一个 ListView,它在其条目旁边显示一个“删除”链接。单击删除链接时,列表项表示的实体将从数据库中删除,但列表本身不会更新,直到我在浏览器中手动重新加载页面。
IModel<List<SampleEntity>> sampleEntityListModel = new LoadableDetachableModel<List<SampleEntity>>() {
@Override
protected List<SampleEntity> load() {
return mSampleEntityBA.findAll();
}
};
mListview = new ListView<SampleEntity>("listview", sampleEntityListModel) {
@Override
protected void populateItem(final ListItem<SampleEntity> item) {
item.add(new Label("listlabel", new PropertyModel<String>(item.getModelObject(),
"text")));
item.add(new Link<SampleEntity>("deleteLink", item.getModel()) {
@Override
public void onClick() {
mSampleEntityBA.delete(item.getModelObject());
}
});
}
};
I am taking my first steps with Apache Wicket and ran into the following problem. I have a ListView
that displays a "delete" link right next to its entries. When the delete link is clicked, the entity represented by the list item is deleted from the database but the list itself does not get updated until I reload the page manually in the browser.
IModel<List<SampleEntity>> sampleEntityListModel = new LoadableDetachableModel<List<SampleEntity>>() {
@Override
protected List<SampleEntity> load() {
return mSampleEntityBA.findAll();
}
};
mListview = new ListView<SampleEntity>("listview", sampleEntityListModel) {
@Override
protected void populateItem(final ListItem<SampleEntity> item) {
item.add(new Label("listlabel", new PropertyModel<String>(item.getModelObject(),
"text")));
item.add(new Link<SampleEntity>("deleteLink", item.getModel()) {
@Override
public void onClick() {
mSampleEntityBA.delete(item.getModelObject());
}
});
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当调用 onClick 时,item.getModelObject() 从sampleEntityListModel 中提取数据,而sampleEntityListModel 又调用mSampleEntityBA.findAll()。 SampleEntityListModel 的模型对象将在请求周期期间被缓存(直到它被分离 - 这通常是您想要的)并且不知道对 delete() 的调用。
为了刷新sampleEntityListModel,请在删除之后添加一个sampleEntityListModel.detach()调用(sampleEntityListModel必须是最终的,但这不会导致任何额外的状态被序列化)。这将导致模型在请求周期稍后呈现列表视图时获取一组新数据。
When onClick called, item.getModelObject() pulls from the sampleEntityListModel which in turn calls mSampleEntityBA.findAll(). The model object of sampleEntityListModel will be cached for the duration on the request cycle (until it is detached - which is usually what you want) and is not aware of the call to delete().
In order to refresh the sampleEntityListModel, add a sampleEntityListModel.detach() call just after the delete (sampleEntityListModel must be made final, but this will not cause any extra state to be serialized). This will cause the model to fetch a fresh set of data when the list view is rendered later in the request cycle.
您可能需要一个 AjaxLink 而不是该链接,然后您必须使用描述的策略来刷新列表 此处,可能会根据 wiki 使用 Wicket 1.3 代码而不是 1.4 的事实进行一些调整。
但使用不同的转发器可能会更好,例如 RefreshingView 或 DataView。 此处提供了一些分类中继器的示例。虽然它们都不是您正在寻找的内容,但查看该代码可能会有所帮助。
You probably want an AjaxLink instead of that Link, and then you have to make the list refresh, using the tactics described here, possibly adjusting a bit for the fact that the wiki has Wicket 1.3 code instead of 1.4.
But you might also be better off with a different repeater, such as a RefreshingView or a DataView. There are some examples of assorted repeaters here. While none of them are exactly what you're looking for, looking at that code might help.
看起来问题是你的 mSampleEntityBA.findAll();返回不正确的数据。如果没有看到更多代码,很难提供帮助。
另一方面,在处理数据库支持的列表时,您确实应该使用 DataView。
looks like the problem is that your mSampleEntityBA.findAll(); is returning incorrect data. hard to help without seeing more code.
on a different note, you should really be using DataView when working with database-backed lists.
您可能还想从 wiQuery 项目中查看 JQGrid,而不是 DataView。
You might also want to check out JQGrid from the wiQuery project instead of DataView.