During the project development process, a large amount of data is displayed on the first page of the page, resulting inFront-end listThe loading display speed is slow, so you need to add paging locally and put all the data into memory first. I will use it below.PythonDemonstrate how to implement local paging algorithm (for secondary data structures)
initialSize = 2 # Number of displays on the first screeneachSize = 5 # Number of remaining pages displayed local_pages = [] # Local pagination data def makePage(d): ''' Create paging data ''' local_pages.clear() if calcSize(d) > initialSize: # The total number of characters is greater than the number of first screens, use local pagination sublist = [] for item in d: for child in item["child"]: (child) firstPageSize = min(len(sublist), initialSize) # The size of the first page local_pages.append(sublist[0:firstPageSize]) # Get the collection of the first page remain_size = len(sublist)-firstPageSize # Number of remaining group_count = int(remain_size / eachSize) # Calculate the number of pages last_count = remain_size % eachSize # Get the remaining amount, how many items are left in the end idx = 0 for idx in range(group_count): start = firstPageSize + idx * eachSize end = start + eachSize local_pages.append(sublist[start:end]) # Add a new page collection if last_count > 0: local_pages.append(sublist[-last_count:]) # The remainder is not 0, it will be used as the last page collection pass def calcSize(d)->int: ''' Calculate the total number of entries ''' size = 0 for item in d: size += len(item["child"]) + 1 return size def printPage(): ''' Print page ''' 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"Number of displays on the first screen:{initialSize}") print(f"Number of remaining pages displayed:{eachSize}") makePage(data) printPage()
Print results
Number of displays on the first screen: 2
Number of remaining pages: 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'}
This is the article about this article teaching you to implement local paging using Python. For more relevant content on Python local paging, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!