如何在 web2py 中自定义分页取决于 bootstrap?

how to make custom paging in web2py depend on bootstrap?

我想使用 web2py 和 bootstrap

进行自定义分页

如何在不使用如下默认方法的情况下在自定义方法中实现模型和控制器以及视图

默认方式是:

 def index():
    fields=[db.table_name.field1,db.table_name.field2]
    grid=SQLFORM.grid(db.table_name.id>0, deletable=False, details=False, create=False, editable=False, paginate=10, csv=False, searchable=False,user_signature=False,showbuttontext=False,fields=fields)
    return dict(grid=grid)

这是我的代码,它可以正常工作

注意:此分页代码需要bootstrap存在

在模型中添加名为 paging.py:

的新文件
# -*- coding: utf-8 -*-
import math

def base_url():
    return 'http://'+request.env.http_host+'/project_name+'/'

def pagination(total ,  page ,  Take ,  offset , controller_name , view_name , Params):
    try:
        cont_view = controller_name+'/'+view_name
        rowPerPage = Take
        if total < Take:
            rowPerPage = total
            totalPage = 0
        totalPage = 0;
        if total != 0 :
            #totalPage = int(total / rowPerPage)
            totalPage = int(math.ceil(total/float(rowPerPage)))
        current = page
        record = offset
        pageStart = current - offset
        pageEnd = current + offset
        numPage = "";
        if totalPage < 1:
            return ""
        numPage += "<ul class='pagination'>"
        if current > 1:
            numPage += "<li class='previous'><a href='"+base_url()+cont_view+"/?page="+str(page-1)+Params+"' aria-label='Previous'><span>&laquo;</span></a></li>"
        else :
            numPage += "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
        if current > (offset + 1):
            numPage += "<li><a href='"+base_url()+cont_view+"/?page=1"+Params+"' name='page1'>1</a></li><li class='disabled spacing-dot'><a href='#'> ...</a> </li>"
        i = 0
        while i < totalPage:
           i = i+1
           if (pageStart <= i and pageEnd >= i) :
                if (i == current):
                    numPage += "<li class='active'><a href='#'>"+str(i)+" <span class='sr-only'>(current)</span></a></li>"
                else:
                   numPage += "<li><a href='" +base_url()+cont_view+"/?page="+str(i)+Params+ "'>" + str(i) + "</a></li>"

        if (totalPage > pageEnd) :
            record = offset
            numPage += "<li class='disabled spacing-dot'><a href='#'> ...</a></li><li><a class='ui-bar-d' href='" +base_url() +cont_view+"/?page="+str(totalPage)+Params+ "'>" + str(totalPage) + "</a></li>"
        if (current < totalPage) :
            numPage += "<li class='next'><a class='ui-bar-d' href='" + base_url() +cont_view+"/?page="+str(page+1)+Params+ "'>&raquo;</a></li>"
        else :
            numPage += "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&raquo;</span></a></li>";
            numPage += "</ul>"
        return (numPage);
    except ValueError:
        return("")

您可以像下面这样在您的控制器中使用:

def index():
    take = 10

    if not request.vars.page:
        page = 1
    else:
       page = int(request.vars.page)
    skip = 0;
    if (page == 1):skip = 0
    else:skip = ((page - 1) * take)

    total  = db(db.table_name.id>0).count()
    paging = pagination(total ,  page ,  take ,  1 ,'default' , 'index' , '')
    data = db(db.table_name.id>0).select(db.table_name.field1, db.table_name.field2 , limitby=(skip,take*page))
    return dict(data = data , paging=paging , b_url = base_url())

之后你可以在视图中这样调用它:

         <table class="table table-striped">
     <tbody><tr class="conHeader">
        <th colspan="4"><strong><span>field 1 title</span></strong></th>
        <th colspan="3"><strong><span>field 2 title</span></strong></th>
    </tr>
{{for item in data:}}
     <tr class="">
        <td colspan="4"><strong><span>{{=item.field1}}</span></strong></td>
           <tr class="">
        <td colspan="4"><strong><span>{{=item.field2}}</span></strong></td>  
    </tr>       
{{pass}}
</tbody>
        </table>

   {{=XML(paging)}}