使用Django和extjs分页

bmvo0sr5  于 2022-09-26  发布在  Go
关注(0)|答案(2)|浏览(198)

Extjs具有带分页功能的网格面板。但是,我认为只有在从服务器接收到所有数据后,分页才能工作。(如果我错了,请纠正我)。在我的例子中,来自服务器的总数据大小为20MB,我不想通过ajax调用加载这些数据(浏览器很难加载这么多数据)。这是我需要的,
1.在页面加载时获取1 MB数据(大约)
1.在此数据中使用extjs分页
1.单击页面工具栏的next按钮后,进行ajax调用以获取下一个1MB数据并将其显示到网格中
1.在此数据中再次使用extjs分页
1.依此类推。。。。。
请建议我如何实现这一点,或者EXTJS中是否有任何现有的方法来实现这一目标。感谢你的帮助。谢谢
PS:Django是我的后端服务器

fcy6dtqo

fcy6dtqo1#

但是,我认为只有在从服务器接收到所有数据后,分页才能工作。
你怎么会这么想?
ExtJS网格分页的工作原理是定义页面大小(比如100),然后存储告诉服务器它需要前100个条目。如果单击“下一页”,则从服务器获取第二个100个条目,依此类推。
为了使分页按预期工作,服务器API必须理解startParam、e1d1e和limitParam

nmpmafwu

nmpmafwu2#

我知道现在已经很晚了,但这里有一个解决方案,适合任何想要使用extjs发送的“start”&“limit”分页参数实现django&extjs分页的人。

def fetchRecords(self, params):

    totalCount = 0
    pageNumber = 1
    records = []
    ids = []

    #Instanciate your query object
    query = Q()

    #Not really relevant for this case but in case you have any filter criteria params then put them here
    if(params.get("searchStartDate")):
         startDate = datetime.strptime(params.get("searchStartDate"), '%Y-%m-%d').date()
         query &= Q(date_created__gte=startDate)     
    if(params.get("searchEndDate")):
         endDate = datetime.strptime(params.get("searchEndDate"), '%Y-%m-%d').date()
         query &= Q(date_created__lte=endDate)     

    # Get the total count, EXT JS Grids need the total count value to be able to paginate
    totalCount = YourModel.objects.filter(query).count()

    #Get the primary keys, we do this because we don't want to get all the objects onto memory. The paginator doesn't 
    #Optimize the fetched data. If your table has millions of records and you load all the record objects to mem, the
    #execution might be quite slow
    your_model_ids_list = YourModel.objects.filter(query).order_by("-id").only('id')

    #Compute the page number based on the pagination "start" & "limit" params sent by EXT grid
    if(int(params.get("start")) != 0 ):
        pageNumber = (int(params.get("start")) / int(params.get("limit"))) + 1

    #Instanciate the paginator object with the primary keys list matching your filter criteria & limit        
    paginator = Paginator(your_model_ids_list, int(params.get("limit")))

    #Get the records that fall on the particular page number that we computed above
    recordIds = paginator.page(pageNumber)

    #Iterate through the record IDs and place them in an array list
    for recordId in recordIds.object_list:
        ids.append(recordId.id)

    #Now fetch the records from your model based on the unique ids that fall on the particular page fetched
    #above
    result = YourModel.objects.filter(Q(pk__in=ids)).order_by("-id")

    #Formulate your response object and return the data
    return {'totalCount': totalCount, 'records': result}

相关问题