简述
ps[2016.2.13]:更新一个问题,下面的源代码有误,具体往下看.
ps[2016.2.27]:将原来的函数组成一个类,完全兼容原来的使用方式.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # 错误代码***************************** ''' htmls = htmls.append( liahref % ( currentpage, url, lenindex, ipagesize, currentpage, lenindex) if lenindex == noformatpage else liahref % ( pageclass, url, lenindex, ipagesize, pageclass, lenindex)) ''' # 错误代码*********************************************************** # 正确代码***************************** htmls.append( liahref % ( currentpage, url, lenindex, ipagesize, currentpage, lenindex) if lenindex == noformatpage else liahref % ( pageclass, url, lenindex, ipagesize, pageclass, lenindex)) # 正确代码*********************************************************** |
这是一个伪代码,因分页需要数据库配合,所以这里只给出分页的代码.
源码
代码里面有很详细的注释,不在过于解释了.
v1.0[2016.2.27]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | """ 分页程序. 2016.2.27 add """ class Pager(object): """ 生成代码示例[使用了妹子UI的分页.参照:http://amazeui.org/widgets/pagination]: page: 当前页数,从1开始. pagesize: 每页里面多少数据,一般为10. rowscount: 查询数据库中的总数(一共有多少条数据,不能添加查询条件,直接count即可). url: 访问地址[注意:这里没有处理url本身带有参数的问题],这里自动添加了page和pagesize参数(!!丢弃了原来的所有参数,若有的话) countshow=Flase:默认不显示(总XXX条,共XX页,当前X页),如需开启,请传递True. """ def repage(self, page, pagesize, rowscount, url,countshow=False): # 页码 ipage = int(page) # 原始page ,数值不会被改变 noformatpage = int(page) # 原始page # 页码大小 ipagesize = int(pagesize) # 总数量 irowscount = int(rowscount) # 进行数值限定 if ipage <= 1: ipage = 1 # 进行分页查询时,需要跳过的数据. # 这个ipage和noformatpage是有区别的!!! # 例如,当前是第二页,那么下面这个ipage是10,而noformatpage是不变的2. ipage = (ipage - 1) * ipagesize # 给当前页添加一个css标记 currentpage = "am-active" # 当前 # 不添加标记 pageclass = "" # 正常 # 空包标记,用于替换. black = "" ''' 格式化数据. 主要作用: 大于/小于当前页5个的自动隐藏. 比如: 当前是第6页,那么只显示1-11页,并且第6页在中间. 示例如下: 1,2,3,4,5,[6],7,8,9,10,11 第8页的时候: 3,4,5,6,7,[8],9,10,11,12,13 以此类推... ''' def formatstyle(item, noformatpage): # 标记最大隐藏的.可配置. maxhidden = noformatpage + 5 # 标记最小隐藏的.可配置 minhidden = noformatpage - 5 # 为当前页添加css标记. if item == noformatpage: return liahref % (currentpage, black, url, item, ipagesize, currentpage, item) elif (minhidden) <= item <= (maxhidden): # 这个地方是控制显示的,只有符合当前页+5或者-5之内的才显示. return liahref % ( pageclass, black, url, item, ipagesize, pageclass, item) else: # 不满足条件的全都隐藏. return liahref % ( pageclass, hiddenstyle, url, item, ipagesize, pageclass, item) # 隐藏数据. hiddenstyle = "display:none;" ''' %1: 添加各种class,在这里就是:标记当前页. %2: 设置隐藏/显示(通过css控制) %3: url地址,不带参数的url地址. %4: 当前页码,从1开始. %5: 数据大小. %6: 标记当前页. %7: 显示的名称. ''' liahref = "<li class='%s' style='%s'><a href='%s?page=%s&pagesize=%s' class='%s' >%s</a></li>" # 循环生成数据,并且使用formatstyle控制显示或隐藏. htmls = [formatstyle(item, noformatpage) for item in range(0, irowscount // ipagesize)] # 获取总页数 countpage = len(htmls) # 处理余数的情况 if irowscount % ipagesize != 0: # 最后一页. lenindex = len(htmls) + 1 # 赋值给总页数 countpage = lenindex # 新增到最后. print(pageclass) print(url) print(lenindex) print(ipagesize) print(pageclass) print(lenindex) # liahref % ("", "http://localhost:5000/admin/list",68,10, "", 68) htmls.append(liahref % ( currentpage,black, url, lenindex, ipagesize, currentpage, lenindex) if lenindex == noformatpage else liahref % ( pageclass,black, url, lenindex, ipagesize, pageclass, lenindex)) # 正确代码*********************************************************** # 参考上面的a,只是去掉了外层的li标记 ahref = "<a href='%s?page=%s&pagesize=%s' class='%s'>%s</a>" # 第一页和上一页 firsthrefdisabled = "am-disabled" # 生成默认禁用数据 prevhref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>上一页</a>" firsthref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>第一页</a>" # 只有当大于1的时候第一页和上一页才可以用. if noformatpage > 1: firsthrefdisabled = black prevhref = ahref % (url, (noformatpage - 1), ipagesize, currentpage, '上一页') firsthref = ahref % (url, 1, ipagesize, currentpage, '第一页') if 1 == noformatpage else ahref % ( url, 1, ipagesize, pageclass, '第一页') # 新增到最前面 htmls.insert(0, "<li class='am-pagination-first %s '>%s</li><li class='am-pagination-prev %s'>%s</li>" % ( firsthrefdisabled, firsthref, firsthrefdisabled, prevhref)) # 第一页和上一页END. # 下一页和最末页 lasthrefdisabled = "am-disabled" # 生成默认禁用数据 nexthref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>下一页</a>" lastindexhref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>最末页</a>" # 只有当小于总数的时候下一页和最末页才可用. if noformatpage < (countpage): lasthrefdisabled = black nexthref = ahref % (url, (noformatpage + 1), ipagesize, currentpage, '下一页') lastindexhref = ahref % (url, countpage, ipagesize, currentpage, '最末页') htmls.append( " <li class='am-pagination-next %s '>%s</li><li class='am-pagination-last %s '>%s</li>" % ( lasthrefdisabled, nexthref, lasthrefdisabled, lastindexhref)) # 下一页和最末页END. # 显示最后的数据. if countshow: htmls.append("总%s条,共%s页,当前%s页" % (irowscount, (len(htmls) - 2), page)) return ipage, ipagesize, ''.join(htmls) |
v0.9[建议使用最新版本.]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | ''' v0.9[建议使用最新版本.] 这是一个分页的伪代码[使用了Flask(不过实际使用什么也无所谓)]. 效果可以看[Pagination.png]图片. 涉及到数据库和返回给前台. 这里只提供了一个分页的方法,注释很详细. prd. pruidong@gmail.com 2015.12.30 ''' ''' 分页程序. 生成代码示例[使用了妹子UI的分页.参照:http://amazeui.org/widgets/pagination]: <ul data-am-widget="pagination" class="am-pagination am-pagination-default"> <li class="am-pagination-first "><a href="'http://localhost:5000/index?page=1&pagesize=10" class="">第一页</a>.</li> <li class="am-pagination-prev "><a href="'http://localhost:5000/index?page=5&pagesize=10" class="am-active">上一页</a>.</li> <li class="" style=""><a href="'http://localhost:5000/index?page=1&pagesize=10" class="">1</a>.</li> <li class="" style=""><a href="'http://localhost:5000/index?page=2&pagesize=10" class="">2</a>.</li> <li class="" style=""><a href="'http://localhost:5000/index?page=3&pagesize=10" class="">3</a>.</li> <li class="" style=""><a href="'http://localhost:5000/index?page=4&pagesize=10" class="">4</a>.</li> <li class="" style=""><a href="'http://localhost:5000/index?page=5&pagesize=10" class="">5</a>.</li> <li class="am-active" style=""><a href="'http://localhost:5000/index?page=6&pagesize=10" class="am-active">6</a>.</li> <li class="am-pagination-next "><a href="'http://localhost:5000/index?page=7&pagesize=10" class="am-active">下一页</a>.</li> <li class="am-pagination-last "><a href="'http://localhost:5000/index?page=83&pagesize=10" class="am-active">最末页</a>.</li> </ul>总840条,共83页,当前6页 page: 当前页数,从1开始. pagesize: 每页里面多少数据,一般为10. rowscount: 查询数据库中的总数(一共有多少条数据,不能添加查询条件,直接count即可). url: 访问地址[注意:这里没有处理url本身带有参数的问题],这里自动添加了page和pagesize参数(!!丢弃了原来的所有参数,若有的话) v0.9[建议使用最新版本.] ''' def repage(page, pagesize, rowscount, url): # 页码 ipage = int(page) # 原始page ,数值不会被改变 noformatpage = int(page) # 原始page # 页码大小 ipagesize = int(pagesize) # 总数量 irowscount = int(rowscount) # 进行数值限定 if ipage <= 1: ipage = 1 # 进行分页查询时,需要跳过的数据. # 这个ipage和noformatpage是有区别的!!! # 例如,当前是第二页,那么下面这个ipage是10,而noformatpage是不变的2. ipage = (ipage - 1) * ipagesize # 给当前页添加一个css标记 currentpage = "am-active" # 当前 # 不添加标记 pageclass = "" # 正常 # 空包标记,用于替换. black = "" ''' 格式化数据. 主要作用: 大于/小于当前页5个的自动隐藏. 比如: 当前是第6页,那么只显示1-11页,并且第6页在中间. 示例如下: 1,2,3,4,5,[6],7,8,9,10,11 第8页的时候: 3,4,5,6,7,[8],9,10,11,12,13 以此类推... ''' def formatstyle(item, noformatpage): # 标记最大隐藏的.可配置. maxhidden = noformatpage + 5 # 标记最小隐藏的.可配置 minhidden = noformatpage - 5 # 为当前页添加css标记. if item == noformatpage: return liahref % (currentpage, black, url, item, ipagesize, currentpage, item) elif (minhidden) <= item <= (maxhidden): # 这个地方是控制显示的,只有符合当前页+5或者-5之内的才显示. return liahref % ( pageclass, black, url, item, ipagesize, pageclass, item) else: # 不满足条件的全都隐藏. return liahref % ( pageclass, hiddenstyle, url, item, ipagesize, pageclass, item) # 隐藏数据. hiddenstyle = "display:none;" ''' %1: 添加各种class,在这里就是:标记当前页. %2: 设置隐藏/显示(通过css控制) %3: url地址,不带参数的url地址. %4: 当前页码,从1开始. %5: 数据大小. %6: 标记当前页. %7: 显示的名称. ''' liahref = "<li class='%s' style='%s'><a href='%s?page=%s&pagesize=%s' class='%s' >%s</a>.</li>" # 循环生成数据,并且使用formatstyle控制显示或隐藏. htmls = [formatstyle(item, noformatpage) for item in range(1, irowscount // ipagesize)] # 获取总页数 countpage = len(htmls) # 处理余数的情况 if irowscount % ipagesize != 0: # 最后一页. lenindex = len(htmls) + 1 # 赋值给总页数 countpage = lenindex # 新增到最后. # 错误代码***************************** ''' htmls = htmls.append( ''' # 错误代码*********************************************************** # 正确代码***************************** htmls.append(liahref % (currentpage, url, lenindex, ipagesize, currentpage,lenindex) if lenindex == noformatpage else liahref % (pageclass,url, lenindex, ipagesize, pageclass, lenindex)) # 正确代码*********************************************************** #参考上面的a,只是去掉了外层的li标记 ahref = "<a href='%s?page=%s&pagesize=%s' class='%s'>%s</a>." # 第一页和上一页 firsthrefdisabled = "am-disabled" # 生成默认禁用数据 prevhref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>上一页</a>" firsthref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>第一页</a>" # 只有当大于1的时候第一页和上一页才可以用. if noformatpage > 1: firsthrefdisabled = black prevhref = ahref % (url, (noformatpage - 1), ipagesize, currentpage, '上一页') firsthref = ahref % (url, 1, ipagesize, currentpage, '第一页') if 1 == noformatpage else ahref % ( url, 1, ipagesize, pageclass, '第一页') # 新增到最前面 htmls.insert(0, "<li class='am-pagination-first %s '>%s</li><li class='am-pagination-prev %s'>%s</li>" % ( firsthrefdisabled, firsthref, firsthrefdisabled, prevhref)) # 第一页和上一页END. # 下一页和最末页 lasthrefdisabled = "am-disabled" # 生成默认禁用数据 nexthref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>下一页</a>" lastindexhref = "<a href='javascript:void(0)' class='am-disabled' disabled='disabled'>最末页</a>" # 只有当小于总数的时候下一页和最末页才可用. if noformatpage < (countpage): lasthrefdisabled = black nexthref = ahref % (url, (noformatpage + 1), ipagesize, currentpage, '下一页') lastindexhref = ahref % (url, countpage, ipagesize, currentpage, '最末页') htmls.append( " <li class='am-pagination-next %s '>%s</li><li class='am-pagination-last %s '>%s</li>" % ( lasthrefdisabled, nexthref, lasthrefdisabled, lastindexhref)) # 下一页和最末页END. # 显示最后的数据. htmls.append("总%s条,共%s页,当前%s页" % (irowscount, (len(htmls) - 2), page)) return ipage, ipagesize, ''.join(htmls) @app.route('/indexs', methods=['POSTS', 'GET']) def seetext(): page = request.args.get('page', 1) pagesize = request.args.get('pagesize', 10) see = SeeText() # 获取总数--不要带条件!!! rowscount = see.count() # 参数请参考方法说明. pageinfo = repage(page=page, pagesize=pagesize, rowscount=rowscount, url=request.base_url) # 查询数据 data = see.findAll(page[0], pagesize) # 转换一下,第二项是分页的数据. pageinfo = ''.join(pageinfo[2]) # 返回数据给前台 return render_template("/index/index.html", seetextdata=data, pageInfo=pageinfo) |