我如何分页我的模型对象列表(Play Framework)?

时间:2016-10-30 09:34:07

标签: java web playframework pagination

我是这个框架的新手! 我想生成模型的分页器以在模板中显示我的lits。

我使用Play-java,Ebean,scala.html模板。

谢谢!

我有这个!

型号:

public static Finder<String,User> find = new Finder(
    String.class, User.class
);


public static  List<User> all(){
    return find.all();
}

控制器:

public class UserController extends Controller {

   public Result index() {
      return ok(list_users.render(User.all()));
   }
}

查看(list_user.scala.html)

      <table>
             <thead>
                  <tr>
                    <th>Email</th>
                    <th>Name</th>
                    <th>Last Name</th>  
                 </tr>
             </thead>           
              <tbody>                
                @for(user<- users){               
                   <tr>
                       <td>@user.getEmail()</td>
                       <td>@user.getName()</td>
                       <td>@user.getLastName()</td>
                  </tr>       
               }
            </tbody>
    </table>         

1 个答案:

答案 0 :(得分:0)

定义一个函数,该函数从数据源获取给定页面配置的页面。

 public static PagedList<User> page(int page, int pageSize, String sortBy, String order, String filter) {
        return
            find.where()
                .ilike("first_name", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .fetch("*")
                .findPagedList(page, pageSize);
    }

然后定义一个Action,用于渲染填充了对象的模板

public Result list(int page, String sortBy, String order, String filter) {
        return ok(
            views.html.list.render(
                page(page, 10, sortBy, order, filter),
                sortBy, order, filter
            )
        );
    }

Complete code Sample会给你更多的信息。