Yii2 ArrayDataProvider不显示已经分页的数据 [英] Yii2 ArrayDataProvider does not show already paginated data

查看:217
本文介绍了Yii2 ArrayDataProvider不显示已经分页的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Yii2提供的ArrayDataProvider对象有问题。我有一个巨大的API调用,它返回了超过7k个项目(这是一个问题,因为获取所有项目都需要很长时间)。我意识到我可以告诉API为我进行分页,因此我试图将已分页的数据实现到ArrayDataProvider中,但是仅在第1页上有效,当我要求第2页时,我看到API返回了下50个项目,但在我查看GridView对象不显示任何内容。

I have an issue with ArrayDataProvider object that Yii2 provides. I have a huge API call that returns more than 7k items (this is a problem because obtaining all items takes a long time). I realized that I can tell the API to paginate for me so I was trying to implement the already paginated data into an ArrayDataProvider but only works in page 1, when I ask for page 2 I see that API returns the next 50 items but in my view the GridView object does not show anything.

示例:

$dataProvider = new ArrayDataProvider([
        'key' => 'id',
        'allModels' => $items,
        'totalCount' => self::GetNumOfItems(),
        'sort' => [
            'attributes' => ['id', 'name', 'email'],
        ],
        'pagination' => [
            'pageSize' => 50,
        ],
    ]);

我要求总物品数量正确显示分页器。如果我要求页面1一切正常,但是如果更改页面2或其他任何gridview显示没有项目。我怀疑这与已经分页的数据有关,但是,有没有办法使这项工作有效?

I ask for the total amount of items to properly display the paginator. If I ask for page 1 all is working fine but if a change to page 2 or whatever the gridview is showing no items. I suspect that is related with the already paginated data but, is there a way to make this work?

推荐答案

ArrayDataProvider 分页显示中的所有元素$ allModels 。因此,如果每页有50个项目,第二页将显示51-100型。如果您已经有分页数据并且 $ items 仅包含来自第二页的50个元素,则 ArrayDataProvider 仍将搜索模型51 -100,不存在,因此您将获得空白页。

ArrayDataProvider paginates through all elements in $allModels. So if you have 50 items per page, second page will show models 51-100. If you already have paginated data and $items contains only 50 elements from second page, ArrayDataProvider will still search for models 51-100, which do not exist, so you get empty page.

在您的情况下,您可能不应该使用 ArrayDataProvider 并编写自己的数据提供程序,该数据提供程序的作用类似于API的代理。 ArrayDataProvider 仍然需要所有模型来执行排序或筛选,因此您要么从API提取所有7k项,要么允许在 ArrayDataProvider 级别,或者应该在API级别完成过滤和排序,并且框架核心中的任何现有数据提供者都无法满足您的需求。
您可以使用诸如 ProxyDataProvider 之类的内容搜索扩展名,但可能很难找到与您的API匹配的内容。

In your case you should probably not use ArrayDataProvider and write your own data provider which will work like a proxy to API. ArrayDataProvider still needs all models to perform sorting or filtering, so either you will fetch all 7k items from API and allow to handle filtering and sorting at ArrayDataProvider level, or filtering and sorting should be done at API level and none of existing data provider from framework core will fit your needs. You may search for extension with something like ProxyDataProvider, but it may be hard to find something that will match your API.

这篇关于Yii2 ArrayDataProvider不显示已经分页的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆