在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放到内存里,下面我用Python演示如何实现本地分页的算法(针对二级数据结构)
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 | initialSize = 2 # 首屏展示条数 eachSize = 5 # 剩余页展示条数 local_pages = [] # 本地分页数据 def makePage(d): ''' 制作分页数据 ''' local_pages.clear() if calcSize(d) > initialSize: # 总条数大于首屏数,使用本地分页 sublist = [] for item in d: for child in item[ "child" ]: sublist.append(child) firstPageSize = min ( len (sublist), initialSize) # 第一页的大小 local_pages.append(sublist[ 0 :firstPageSize]) # 取第一页的集合 remain_size = len (sublist) - firstPageSize # 剩余条数 group_count = int (remain_size / eachSize) # 计算分页数 last_count = remain_size % eachSize # 取余,最后剩余多少条 idx = 0 for idx in range (group_count): start = firstPageSize + idx * eachSize end = start + eachSize local_pages.append(sublist[start:end]) # 新增页集合 if last_count > 0 : local_pages.append(sublist[ - last_count:]) # 余数不为0,将作为最后一页集合 pass def calcSize(d) - > int : ''' 计算总条数 ''' size = 0 for item in d: size + = len (item[ "child" ]) + 1 return size def printPage(): ''' 打印页面 ''' idx = 0 for p in local_pages: idx + = 1 print ( "page:{}" . format (idx)) for item in p: print (item) data = [{ "id" : "1" , "name" : "parent_1" , "child" :[ { "id" : "1_1" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_2" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_3" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_4" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_5" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_6" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_7" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_8" , "name" : "RS234326348264" , "parent_id" : "1" }, { "id" : "1_9" , "name" : "RS234326348264" , "parent_id" : "1" }]}, { "id" : "2" , "name" : "parent_2" , "child" :[ { "id" : "2_1" , "name" : "RS234326348264" , "parent_id" : "2" }]}] print (f "首屏展示条数:{initialSize}" ) print (f "剩余页展示条数:{eachSize}" ) makePage(data) printPage() |
打印结果
首屏展示条数:2
剩余页展示条数:5
page:1
{‘id’: ‘1_1’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_2’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
page:2
{‘id’: ‘1_3’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_4’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_5’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_6’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_7’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
page:3
{‘id’: ‘1_8’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘1_9’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘1’}
{‘id’: ‘2_1’, ‘name’: ‘RS234326348264’, ‘parent_id’: ‘2’}
到此这篇关于一文教你使用Python实现本地分页的文章就介绍到这了,更多相关Python本地分页内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!