在页面类Play框架中使用List

时间:2015-06-21 09:17:00

标签: playframework-2.3

在play框架模型中,我们可以在我们的视图中返回Page对象进行渲染,但Page类只能从finder对象调用。

public static Page<DokumenKeluarFileModel> search(int page, int pageSize, String sortBy, String order, String filter){
    return find.where(filter)
            .orderBy(sortBy + " " + order)
            .findPagingList(pageSize)
            .setFetchAhead(false)
            .getPage(page);
}

我的问题是我可以在不使用finder对象的情况下使用此Page对象吗?因为我有一个列表,我想用分页返回到视图。感谢您的建议

3 个答案:

答案 0 :(得分:0)

存在pagination模块,但它不会查找最新版本。

您可以尝试快速实施Page

import java.util.List;
import java.util.NoSuchElementException;

public class ListBackedPage<T> implements Page<T> {

    private final List<T> list;
    private final int pageSize;
    private final int index;

    private ListBackedPage(List<T> list, int pageSize, int index) {
        this.list = list;
        this.pageSize = pageSize;
        this.index = index;
    }

    public static <U> Page<U> fromList(List<U> sourceList, int pageSize) {
        return new ListBackedPage(sourceList, pageSize, 0);
    }

    public String getDisplayXtoYofZ(String to, String of) {
        final int start = index * pageSize;
        return String.format("%d %s %d %s %d", 
            start, to, start + pageSize, of, getTotalRowCount());
    }

    public List<T> getList() {
        final int start = index * pageSize;
        final int end = Math.min(start + pageSize, getTotalRowCount());
        System.out.println(start + " - " + end);
        return list.subList(start, end);
    }

    public int getPageIndex() { return index; }

    public int getTotalPageCount() {
        return (int) Math.ceil(list.size() / (double) pageSize);
    }

    public int getTotalRowCount() { return list.size(); }

    public boolean hasNext() {
        return index >= 0 && index < getTotalPageCount() - 1;
    }

    public boolean hasPrev() {
        return index > 0 && index <= getTotalPageCount();
    }

    public Page<T> next() {
        if (! hasNext())
            throw new NoSuchElementException();
        return new ListBackedPage(list, pageSize, index + 1);
    }

    public Page<T> prev() {
        if (! hasPrev())
            throw new NoSuchElementException();
        return new ListBackedPage(list, pageSize, index - 1);
    }

    public String toString() {
        return String.format("Page %d/%d", index + 1, getTotalPageCount());
    }

}

创建页面:

List<String> list = java.util.Arrays.asList("a", "b", "c");
Page<String> page = ListBackedPage.fromList(list, 2);

答案 1 :(得分:0)

这是我的路线

fullcalendar.min.js

我的控制器

GET     /pencarian                      controllers.Pencarian.index()
POST    /pencarian/search               controllers.Pencarian.search(page:Int ?=0)

对不起,我还是不能发布图片了。我将使用/ pencarian / search中的页面。点击下一步后,网址将为/ pencarian / Page 2/1

答案 2 :(得分:0)

这是我的观点

@(documentList: com.avaje.ebean.Page[PencarianResultBean], pencarianForm: Form[PencarianBean])
@import helper._


@body("Pencarian"){
<div class="container">
<div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table table-hover" style="width: 800px;">
            <thead>
            <tr>
                <th>Debitur</th>
                <th>Nama Dokumen</th>
                <th>Nomor</th>
                <th>Tanggal Dokumen</th>
                <th>Jatuh Tempo</th>
                <th>Pemilik</th>
                <th>Lokasi</th>
                <th>Luas</th>
                <th>Nilai</th>
                <th>Asli/Copy</th>
                <th>Ada/Tidak</th>
                <th>Nomor Bast</th>
                <th>Tanggal</th>
                <th>Keterangan</th>
            </tr>
            </thead>
            <tbody>
            @for(doc <- documentList.getList){
            <tr>
                <td>
                    @doc.getDebitur
                </td>
                <td>
                    @doc.getNamaDokumen
                </td>
                <td>
                    @doc.getNomor
                </td>
                <td>
                    @if(doc.getTanggalDokumen == null){
                    <em>-</em>
                    }else{
                    @doc.getTanggalDokumen.format("dd/MM/yyyy")
                    }
                </td>
                <td>
                    @if(doc.getJatuhTempo == null){
                    <em>-</em>
                    }else{
                    @doc.getJatuhTempo.format("dd/MM/yyyy")
                    }
                </td>
                <td>
                    @doc.getPemilik
                </td>
                <td>
                    @doc.getLokasi
                </td>
                <td>
                    <p class="autonumber">@doc.getLuas</p>
                </td>
                <td>
                    <p class="autonumber">@doc.getNilai</p>
                </td>
                <td>
                    @if(doc.getAsli == true){
                    A
                    }else{
                    C
                    }
                </td>
                <td>
                    @if(doc.getAvailable == true){
                    A
                    }else{
                    T
                    }
                </td>
                <td>
                    @doc.getNomorBast
                </td>
                <td>
                    @if(doc.getTanggal == null){
                    <em>-</em>
                    }else{
                    @doc.getTanggal.format("dd/MM/yyyy")
                    }
                </td>
                <td>
                    @doc.getKeterangan
                </td>
            </tr>
            }
            </tbody>
        </table>
    </div>
</div>

<ul class="pagination">
    @if(documentList.hasPrev) {
    <li class="prev">
        <a href="@documentList.prev">&larr; Previous</a>
    </li>
    } else {
    <li class="prev disabled">
        <a>&larr; Previous</a>
    </li>
    }
    <li class="current">
        <a>Displaying @documentList.getDisplayXtoYofZ(" to "," of ")</a>
    </li>
    @if(documentList.hasNext) {
    <li class="next">
        <a href="@documentList.next">Next &rarr;</a>
    </li>
    } else {
    <li class="next disabled">
        <a>Next &rarr;</a>
    </li>
    }
</ul>

@form(routes.Pencarian.index()){
<input type="hidden" name ="mutasi" value="@pencarianForm("mutasi").value">
<input type="hidden" name ="debitur" value="@pencarianForm("debitur").value">
<input type="hidden" name ="dokumen" value="@pencarianForm("dokumen").value">
<input type="hidden" name ="namaDokumen" value="@pencarianForm("namaDokumen").value">
<input type="hidden" name ="tanggalDokumenStart" value="@pencarianForm("tanggalDokumenStart").value">
<input type="hidden" name ="tanggalDokumenEnd" value="@pencarianForm("tanggalDokumenEnd").value">
<input type="hidden" name ="jatuhTempoStart" value="@pencarianForm("jatuhTempoStart").value">
<input type="hidden" name ="jatuhTempoEnd" value="@pencarianForm("jatuhTempoEnd").value">

<div class="form-group">
    <button type="submit" class="btn btn-info cusbutton">Cancel</button>
</div>
}

    }