我正在尝试将动作添加到我的tapestry选择菜单中。我现在通过注入selectModelFactory并从hibernate查询中提供一个列表来生成选择菜单。然后我想在菜单中提供一个额外的项目,当给定的选项没有提供所需的选择时,它会显示“+添加新项”。选择+ Add New Item时,我尝试使用onValueChanged方法捕获新对象并返回一个区域。我一直无法做到这一点。有人能指出我正确的方向。我需要阻止此对象被提交到数据库,这使我相信我不应该将它添加到现有列表中。
void onPrepare() {
List<MyClass> results = session.createCriteria(MyClass.class).list();
MyClass tempObject = new MyClass();
tempObject .setName("+ Add New Item");
results.add(tempObject);
selectModel = selectModelFactory.create(results, "label");
}
public Object onValueChanged(MyClass myClass) {
if(myClass!= null && myClass.getName().equals("+ Add New Item")) {
return myZone.getBody();
}
return null;
}
答案 0 :(得分:2)
看看这个working example。事件处理方法的命名不正确,应为onValueChangedNameOfYourSelect(MyClass value)
。或者我更喜欢使用的是OnEvent注释。
答案 1 :(得分:1)
您好请参阅nabble http://tapestry.1045711.n5.nabble.com/T5-Select-Menu-with-Other-Option-td4520881.html#a4529383邮件列表以供讨论。以下最终成为解决方案,但是阻止“其他”选择被保存呈现出其他复杂性。
@Property
private Funding funding;
@Property
@Persist
private SelectModel fundingModel;
@InjectComponent
private Zone fundingZone;
final private static Funding NEW_FUNDING = new Funding();
final private static String NEW_FUNDING_ID = "-1";
void onPrepare() {
fundings = session.createCriteria(Funding.class).list();
NEW_FUNDING.setName("+ Other");
fundings.add(NEW_FUNDING);
fundingModel = selectModelFactory.create(fundings, "label");
}
@CommitAfter
void onSuccess() {
//you would want to add some sort of logic to prevent the "Other" object from being commited.
}
public ValueEncoder<Funding> getEncoder() {
final ValueEncoder<Funding> encoder = valueEncoderSource.getValueEncoder(Funding.class);
return new FundingValueEncoder(encoder);
}
final private static class FundingValueEncoder implements ValueEncoder<Funding> {
final private ValueEncoder<Funding> delegate;
public FundingValueEncoder(ValueEncoder<Funding> delegate) {
this.delegate = delegate;
}
public String toClient(Funding value) {
if (value == NEW_FUNDING) {
return NEW_FUNDING_ID;
} else {
return delegate.toClient(value);
}
}
public Funding toValue(String clientValue) {
if (NEW_FUNDING_ID.equals(clientValue)) {
return NEW_FUNDING;
} else {
return delegate.toValue(clientValue);
}
}
}
public Object onValueChanged(Funding funding) {
if(funding == NEW_FUNDING) {
return fundingZone.getBody();
}
return null;
}