Wicket - 更改下拉列表的值后不调用 LoadableDetachableModel 中的加载方法
Wicket - Load method in LoadableDetachableModel is not called after changing the value of the dropdown list
我在更新根据下拉列表值过滤的列表时遇到问题。
这是对我的模型的描述:
- 我有一个用户列表
- 当我点击一个用户时,这个用户的另一个订单列表是
显示
- 订单列表根据下拉列表的值进行过滤
包含状态列表
请看下图:
过滤器运行良好,但我面临的问题是,一旦我从下拉列表中选择一个元素,当用户更改时,订单列表不再更新。
这是我的代码片段:
订单面板的构建和列表的实例化:
public OrdersPanel(String id)
{
super(id);
this.setOutputMarkupId(true);
setOutputMarkupPlaceholderTag(true);
IModel<List<Order>> orderListModel = new OrderListModel();
orderListView = new orderListView("orderListView", orderListModel);
//...
}
订单列表模型:
private final class OrderListModel extends LoadableDetachableModel<List<Order>> {
private static final long serialVersionUID = 1L;
@Override
protected List<Order> load() {
//...
//We set the variable allOrders in order to be used later in the filtering process
//...
}
}
下拉菜单的构造:
private class StatusDropDown extends CustomDropDown<String> implements IAjaxIndicatorAware {
private static final long serialVersionUID = 1L;
private StatusDropDown(String id) {
super(id);
this.setNullValid(true);
StatusListModel statusModel = new StatusListModel();
setChoices(statusModel);
setChoiceRenderer(new StatusChoiceRenderer(statusModel));
}
@Override
protected void onUpdate(AjaxRequestTarget target) {
super.onUpdate(target);
if (target != null) {
new StatusDropDownRefreshEvent(this, target).fire();
target.addComponent(this);
}
}
/**
* disable ajax marker for the form fields
*/
public String getAjaxIndicatorMarkupId() {
return null;
}
}
CustomDropDown(必须在我工作的项目上下文中使用):
public class CustomDropDown<V> extends DropDownChoice<V> {
private static final long serialVersionUID = 1L;
public CustomDropDown(String id) {
this(id, id);
}
public CustomDropDown(String id, String property) {
super(id);
setOutputMarkupId(true);
setModel(new CustomComponentPropertyModel<V>(property));
add(new AjaxFormComponentUpdatingBehavior("onchange") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
new UpdateEvent(CustomDropDown.this, target).fire();
if (target != null) {
target.addComponent(CustomDropDown.this);
}
CustomDropDown.this.onUpdate(target);
}
});
}
@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
if (!isValid()) {
tag.append("class", "invalid", " ");
FeedbackMessage message = getFeedbackMessage();
if (message != null) {
tag.put("title", message.getMessage().toString());
message.markRendered();
}
} else if (isRequired()) {
tag.append("class", "red-background", " ");
}
}
public void setWidth(String width) {
this.add(new AttributeAppender("style", true, new Model<String>("width:" + width), ";"));
}
public CustomDropDown<V> setChoices(V... choices) {
this.setChoices(Arrays.asList(choices));
return this;
}
protected void onUpdate(AjaxRequestTarget target) {
}
}
StatusDropDownRefreshEvent 侦听器:
@AjaxUpdateEventListener
public void statusDropDownRefreshPanel(StatusDropDownRefreshEvent event){
if (event.getTarget() != null) {
orderListView.setList(getOrdersByStatus(allOrders));
event.getTarget().addComponent(this);
}
}
用户变更:
更改用户时,会从用户面板触发更新事件,然后缓存在订单面板中:
@AjaxUpdateEventListener
public void refreshPanel(CustomerOrderRefreshEvent event) {
if (event.getTarget() != null) {
event.getTarget().addComponent(this);
this.onBeforeRender();
}
}
onBeforeRender() 确定面板的可见性(如果没有订单可用,则订单面板不可见)
@Override
public void onBeforeRender() {
setVisibilityAllowed(checkVisibility());
super.onBeforeRender();
}
最后,checkVisibility方法:
private boolean checkVisibility() {
if (isUserChanged()) {
List<Order> src = orderListView.getModelObject();
statusDropDown.setDefaultModelObject(null);
return CollectionUtils.isNotEmpty(src);
}
return true;
}
我的主要问题是,一旦从列表中选择状态,更改所选用户不会更新订单列表。
非常感谢您的回复和抽出时间。
此致。
我找到了解决问题的方法。
订单列表未更新,因为在错误的对象上调用了方法 getObject。
load()方法的调用可以通过getObject()来完成,但条件是:对象必须是detach的(参见this link处getObject()的实现)
在我的例子中,分离的对象是 orderListModel 而不是 orderListView,所以这是我添加到我的代码中的内容:
//Set the content of the list model
List<Order> orders = orderListModel.getObject(); //This invokes the load method
//Update the content of the list
orderListView.setList(orders);
我在更新根据下拉列表值过滤的列表时遇到问题。
这是对我的模型的描述:
- 我有一个用户列表
- 当我点击一个用户时,这个用户的另一个订单列表是 显示
- 订单列表根据下拉列表的值进行过滤 包含状态列表
请看下图:
过滤器运行良好,但我面临的问题是,一旦我从下拉列表中选择一个元素,当用户更改时,订单列表不再更新。
这是我的代码片段:
订单面板的构建和列表的实例化:
public OrdersPanel(String id) { super(id); this.setOutputMarkupId(true); setOutputMarkupPlaceholderTag(true); IModel<List<Order>> orderListModel = new OrderListModel(); orderListView = new orderListView("orderListView", orderListModel); //... }
订单列表模型:
private final class OrderListModel extends LoadableDetachableModel<List<Order>> { private static final long serialVersionUID = 1L; @Override protected List<Order> load() { //... //We set the variable allOrders in order to be used later in the filtering process //... } }
下拉菜单的构造:
private class StatusDropDown extends CustomDropDown<String> implements IAjaxIndicatorAware { private static final long serialVersionUID = 1L; private StatusDropDown(String id) { super(id); this.setNullValid(true); StatusListModel statusModel = new StatusListModel(); setChoices(statusModel); setChoiceRenderer(new StatusChoiceRenderer(statusModel)); } @Override protected void onUpdate(AjaxRequestTarget target) { super.onUpdate(target); if (target != null) { new StatusDropDownRefreshEvent(this, target).fire(); target.addComponent(this); } } /** * disable ajax marker for the form fields */ public String getAjaxIndicatorMarkupId() { return null; } }
CustomDropDown(必须在我工作的项目上下文中使用):
public class CustomDropDown<V> extends DropDownChoice<V> { private static final long serialVersionUID = 1L; public CustomDropDown(String id) { this(id, id); } public CustomDropDown(String id, String property) { super(id); setOutputMarkupId(true); setModel(new CustomComponentPropertyModel<V>(property)); add(new AjaxFormComponentUpdatingBehavior("onchange") { private static final long serialVersionUID = 1L; @Override protected void onUpdate(AjaxRequestTarget target) { new UpdateEvent(CustomDropDown.this, target).fire(); if (target != null) { target.addComponent(CustomDropDown.this); } CustomDropDown.this.onUpdate(target); } }); } @Override protected void onComponentTag(ComponentTag tag) { super.onComponentTag(tag); if (!isValid()) { tag.append("class", "invalid", " "); FeedbackMessage message = getFeedbackMessage(); if (message != null) { tag.put("title", message.getMessage().toString()); message.markRendered(); } } else if (isRequired()) { tag.append("class", "red-background", " "); } } public void setWidth(String width) { this.add(new AttributeAppender("style", true, new Model<String>("width:" + width), ";")); } public CustomDropDown<V> setChoices(V... choices) { this.setChoices(Arrays.asList(choices)); return this; } protected void onUpdate(AjaxRequestTarget target) { } }
StatusDropDownRefreshEvent 侦听器:
@AjaxUpdateEventListener public void statusDropDownRefreshPanel(StatusDropDownRefreshEvent event){ if (event.getTarget() != null) { orderListView.setList(getOrdersByStatus(allOrders)); event.getTarget().addComponent(this); } }
用户变更: 更改用户时,会从用户面板触发更新事件,然后缓存在订单面板中:
@AjaxUpdateEventListener public void refreshPanel(CustomerOrderRefreshEvent event) { if (event.getTarget() != null) { event.getTarget().addComponent(this); this.onBeforeRender(); } }
onBeforeRender() 确定面板的可见性(如果没有订单可用,则订单面板不可见)
@Override public void onBeforeRender() { setVisibilityAllowed(checkVisibility()); super.onBeforeRender(); }
最后,checkVisibility方法:
private boolean checkVisibility() { if (isUserChanged()) { List<Order> src = orderListView.getModelObject(); statusDropDown.setDefaultModelObject(null); return CollectionUtils.isNotEmpty(src); } return true; }
我的主要问题是,一旦从列表中选择状态,更改所选用户不会更新订单列表。
非常感谢您的回复和抽出时间。
此致。
我找到了解决问题的方法。
订单列表未更新,因为在错误的对象上调用了方法 getObject。
load()方法的调用可以通过getObject()来完成,但条件是:对象必须是detach的(参见this link处getObject()的实现)
在我的例子中,分离的对象是 orderListModel 而不是 orderListView,所以这是我添加到我的代码中的内容:
//Set the content of the list model
List<Order> orders = orderListModel.getObject(); //This invokes the load method
//Update the content of the list
orderListView.setList(orders);