如何覆盖CRUD / list()函数?玩!骨架

时间:2011-10-19 20:04:18

标签: pagination playframework crud

我正在尝试为我的某个模型覆盖CRUD模块的list()函数。

我在Google群组中找到了this,这实际上是我遇到的问题。

基本上我想根据certian类别过滤列表,我试过这个:

CONTROLLER

public static void list(string category){
    List<Duty> object = Duty.getByCategory(category);
    render(object);
}

MODEL

public static List<Duty> getByCategory(String category){
    List<Duty> result = Duty.find("select distinct d from Duty d join " +
        "d.category c where c.name = ? order by d.name", category).fetch();
    return result;
}

我收到以下错误:

Template execution error

如何覆盖列表操作?

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

您似乎要覆盖控制器而不是模板。 CRUD列表方法的签名是this one,与您的签名略有不同:

public static void list(int page, String search, String searchFields, String orderBy, String order) {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        if (page < 1) {
            page = 1;
        }
        List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));
        Long count = type.count(search, searchFields, (String) request.args.get("where"));
        Long totalCount = type.count(null, null, (String) request.args.get("where"));
        try {
            render(type, objects, count, totalCount, page, orderBy, order);
        } catch (TemplateNotFoundException e) {
            render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
        }
    }

你会注意到render()传递了你做的更多参数,可能它们不是可选的。尽量为他们提供价值。

答案 1 :(得分:1)

您可以覆盖CRUD列表方法,并添加在其中中传递许多参数的过滤器,例如:

public static void list(int page, String search, String searchFields, String orderBy, String order) {
    ObjectType type = ObjectType.get(getControllerClass());
    notFoundIfNull(type);
    if (page < 1) {
        page = 1;
    }
    String where = "nameAttribute =" + value;

    List<Model> objects = type.findPage(page, search, searchFields, orderBy, order, where);
    Long count = type.count(search, searchFields, where);
    Long totalCount = type.count(null, null, where);
    try {
        render(type, objects, count, totalCount, page, orderBy, order);
    } catch (TemplateNotFoundException e) {
        render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order);
    }
}

答案 2 :(得分:0)

尝试从view(xtml)调用覆盖方法。

<form action="@{Controler.overrideList()}"  method="POST">

并使用以前的代码,并添加在where = "..."

中传递许多参数的过滤器