简体   繁体   English

过滤crud list问题玩! 框架

[英]Filtering crud list problem play! framework

it's my first post here:) I have a problem with the CRUD module.这是我在这里的第一篇文章:) 我的 CRUD 模块有问题。 I want to add more filter on the list but I don't success to understand the Factory Model.我想在列表中添加更多过滤器,但我没有成功理解工厂 Model。 I have this code:我有这个代码:

@With(Secure.class)
public class Contacts extends CRUD {


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

    //System.out.println(type);

    List<Model> contacts = Model.Manager.factoryFor(Contact.class).fetch((page - 1) * getPageSize(), getPageSize(), orderBy, order, searchFields == null ? new ArrayList<String>() : Arrays.asList(searchFields.split("[ ]")), search, (String) request.args.get("where"));
    System.out.println(contacts);

    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"));


     // Liste des origines
    List<Origine> origines = Origine.find("order by nom asc").fetch();
    List<Employe> employes = Employe.find("order by nom asc").fetch();

    try {
        render(type, objects, count, totalCount, page, orderBy, order, origines,employes);
    } catch (TemplateNotFoundException e) {
        render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order, origines, employes);
    }
}

} }

I would like to search the filed "origine" and "employe" how can i do that?我想搜索归档的“原产地”和“雇员”,我该怎么做? Thank you for your help.谢谢您的帮助。 :) :)

I progress in my code... Your advice was helpfull.我在我的代码中取得了进步......你的建议很有帮助。 Now I have created a new class Recherche which extend CRUD.现在我创建了一个扩展 CRUD 的新 class Recherche。 I would like to change the field "Contact" with dynamic field because I would like to extend Recherche for Contact and Compte and other class !我想用动态字段更改“联系人”字段,因为我想扩展 Recherche 的 Contact 和 Compte 以及其他 class ! I have tested ObjectType width no sucess...我已经测试过 ObjectType 宽度没有成功...

public class Recherche extends CRUD {


public static List findPage(int page, String search, String searchFields, String orderBy, String order, String where) throws ClassNotFoundException{

    int pageLength = getPageSize();
    String q = "from Contact where 1=1 ";
    if (search != null && !search.equals("")) {
        String searchQuery = getSearchQuery();
        if (!searchQuery.equals("")) {
            q += " and (" + searchQuery + ")";
        }
        q += (where != null ? " and " + where : "");
    } else {
        q += (where != null ? " and " + where : "");
    }

    /**
     * Ajout des champs auxiliaires
     */
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties();

    String qaux = "";
    List<Integer> relationArray = new ArrayList<Integer>();

    for (Property field : fields) {
        if(field.isRelation){

            if(request.params.get(field.name) != null){
                int requestArg = Integer.parseInt(request.params.get(field.name));

                if(requestArg != 0){
                    if (!qaux.equals("")) {
                        qaux += " and ";
                    }
                    relationArray.add(requestArg);
                    qaux += " "+field.name+"_id = ?"+(relationArray.size()+1)+" ";
                }
            }
        }
    }
    if(!qaux.equals("")){
        q+= " and ( "+qaux+" ) ";
    }
    /**
     * Fin ajout champs auxiliaires
     */


    if (orderBy == null && order == null) {
            orderBy = "nom";
            order = "ASC";
    }
    if (orderBy == null && order != null) {
            orderBy = "nom";
    }
    if (order == null || (!order.equals("ASC") && !order.equals("DESC"))) {
            order = "ASC";
    }
    q += " order by " + orderBy + " " + order;
    Query query = Contact.em().createQuery(q);
    if (search != null && !search.equals("") && q.indexOf("?1") != -1) {
        query.setParameter(1, "%" + search.toLowerCase() + "%");
    }

    // Champs auxiliaires
    for (int i = 0; i < relationArray.size(); i++) {
        query.setParameter((i+2), relationArray.get(i));
    }


    query.setFirstResult((page - 1) * pageLength);
    query.setMaxResults(pageLength);

    return query.getResultList();
}


public static Long count(String search, String searchFields, String where) {
    String q = "select count(c.id) from Contact c where 1=1 ";

    if (search != null && !search.equals("")) {
        String searchQuery = getSearchQuery();
         if (!searchQuery.equals("")) {
             q += " and (" + searchQuery + ")";
         }
         q += (where != null ? " and " + where : "");
     } else {
         q += (where != null ? " and " + where : "");
     }
     /**
     * Ajout des champs auxiliaires
     */
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties();

    String qaux = "";
    List<Integer> relationArray = new ArrayList<Integer>();

    for (Property field : fields) {
        if(field.isRelation){

            if(request.params.get(field.name) != null){
                int requestArg = Integer.parseInt(request.params.get(field.name));

                if(requestArg != 0){
                    if (!qaux.equals("")) {
                        qaux += " and ";
                    }
                    relationArray.add(requestArg);
                    qaux += " "+field.name+"_id = ?"+(relationArray.size()+1)+" ";
                }
            }
        }
    }
    if(!qaux.equals("")){
        q+= " and ( "+qaux+" ) ";
    }

    /**
     * Fin ajout champs auxiliaires
     */


     Query query = Contact.em().createQuery(q);
     if (search != null && !search.equals("") && q.indexOf("?1") != -1) {
         query.setParameter(1, "%" + search.toLowerCase() + "%");
     }
     // Champs auxiliaires
    for (int i = 0; i < relationArray.size(); i++) {
        query.setParameter((i+2), relationArray.get(i));
    }

     return Long.decode(query.getSingleResult().toString());
 }


public static void list(int page,String search,int origine,String searchFields, String orderBy, String order) throws ClassNotFoundException {
    ObjectType type = ObjectType.get(getControllerClass());
    notFoundIfNull(type);

    if (page < 1) {
        page = 1;
    }

    List<Contact> objects = Contacts.findPage(page, search, searchFields, orderBy, order, (String) request.args.get("where"));
    Long count = Contacts.count(search, searchFields, (String) request.args.get("where"));
    Long totalCount = Contacts.count(null, null, (String) request.args.get("where"));


     // Liste des origines
    List<Origine> origines = Origine.find("order by nom asc").fetch();
    // Liste des employes
    List<Employe> employes = Employe.find("order by nom asc").fetch();
    // Liste des villes
    List<Ville> villes = Ville.find("order by nom asc").fetch();

    try {
        render(type, objects, count, totalCount, page, orderBy, order, origines,employes,villes);
    } catch (TemplateNotFoundException e) {
        render("CRUD/list.html", type, objects, count, totalCount, page, orderBy, order, origines, employes,villes);
    }
}


private static String getSearchQuery() {
    List<Property> fields = Contact.Manager.factoryFor(Contact.class).listProperties();

    ObjectType type = ObjectType.get(getControllerClass());
    notFoundIfNull(type);



    String q = "";
    for (Property field : fields) {
        if(field.isSearchable){
            if (!q.equals("")) {
                q += " or ";
            }

            q += "lower(" + field.name + ") like ?1";
        }
    }
    return q;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM