Tapestry 5 add an action to a select menu
I'm trying to add an action to my tapestry select menu. I'm currently generating the select menu by injecting selectModelFactory and providing it with a list from a hibernate query. I then want to provide an additional item to the menu that says something like "+ Add New Item" when the given choices do not present the desired choice. When selecting + Add New Item, I tried using the onValueChanged method to capture the new object and return a zone. I have been unable to make this work. Could someone point me in the right direction. I need to prevent this object from bei开发者_运维百科ng commited to the database as well which leads me to believe I should not be adding it to the existing list.
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;
}
Have a look at this working example. The naming of your event handing method is not correct and should be onValueChangedNameOfYourSelect(MyClass value)
. Or what I prefer to use is the OnEvent annotation.
Hello see nabble http://tapestry.1045711.n5.nabble.com/T5-Select-Menu-with-Other-Option-td4520881.html#a4529383 mailing list for discussion. The following ended up being the solution, however preventing the "Other" choice from being saved presented other complexities.
@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;
}
精彩评论