Tapestry 5 将操作添加到选择菜单

发布于 2024-11-16 12:50:15 字数 734 浏览 4 评论 0原文

我正在尝试向我的挂毯选择菜单添加一个操作。我目前正在通过注入 selectModelFactory 并为其提供来自休眠查询的列表来生成选择菜单。然后,我想在菜单中提供一个附加项目,当给定的选项未提供所需的选择时,该项目会显示“+ 添加新项目”之类的内容。当选择+添加新项目时,我尝试使用 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;
}

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 being 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏九 2024-11-23 12:50:15

看一下这个工作示例。您的事件处理方法的命名不正确,应为 onValueChangedNameOfYourSelect(MyClass value)。或者我更喜欢使用的是 OnEvent注解。

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.

不再让梦枯萎 2024-11-23 12:50:15

您好,请参阅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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文