【问题标题】:Pagination with Django and extjs使用 Django 和 extjs 进行分页
【发布时间】:2016-09-24 18:17:44
【问题描述】:

Extjs 具有带有分页功能的网格面板。但是,我认为分页只有在从服务器接收到所有数据后才能工作。 (如果我在这里错了,请纠正我)。在我的情况下,来自服务器的总数据大小为 20MB,我不想通过 ajax 调用加载(浏览器被触发加载这么多数据)。这就是我需要的。 , ,

  1. 在页面加载时获取 1 MB 数据(大约)
  2. 在此数据中使用 extjs 分页
  3. 单击分页工具栏的下一步按钮后,进行 ajax 调用以获取下一个 1MB 数据并将其显示到网格中
  4. 再次在此数据中使用 extjs 分页
  5. 等等.....

请建议我如何实现这一点,或者 EXTJS 中是否有任何现有的方法可以做到这一点。感谢你的帮助。谢谢。

PS:Django 是我的后端服务器

【问题讨论】:

    标签: javascript ajax django extjs pagination


    【解决方案1】:

    但是,我认为分页只有在从服务器接收到所有数据后才能工作。

    是什么让你这么想?

    ExtJS 网格分页通过定义页面大小(比如说 100)来工作,然后存储告诉服务器它想要前 100 个条目。如果单击“下一页”,则从服务器获取后 100 个条目,依此类推。

    要使分页按预期工作,服务器 API 必须了解 startParampageParamlimitParam

    【讨论】:

    • 感谢@Alexander 消除了这个疑问。我将使用这种方法。还有一个后续问题。在任何时候如果我过滤我的网格存储,它将仅对当前的 100 个条目应用过滤器,对吗?
    • 您可以有一个"remote filter",它使商店在过滤器更改时调用服务器,以收集受过滤器影响的前 100 个条目,或者您可以有一个本地过滤器,它只过滤掉您在客户端拥有的 100 条记录。远程过滤器再次要求您的服务器 API 支持相应的参数,这次称为filterParam
    • 谢谢。正是我想要的。
    【解决方案2】:

    我知道现在已经很晚了,但是对于那些想要使用 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}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 2014-04-26
      • 1970-01-01
      相关资源
      最近更新 更多