xujiedba 2015-09-28 16:40:49 2721次浏览 2条回复 0 0 0

后端

$result=new ArrayDataProvider([
    'allModels' =>$querys,
    'pagination' => [
        'pageSize' => 10,
    ],
]);

第一页返回数据:

{"items":[{"id":"25","user_id":"61","content":"/videos/2015/9/1443421568074.mp4","msgtype":"3","createtime":"2015-09-28 14:26:50"},{"id":"24","user_id":"61","content":"/videos/2015/9/1443421526787.mp4","msgtype":"3","createtime":"2015-09-28 14:26:22"},{"id":"23","user_id":"61","content":"/videos/2015/9/1443421515896.mp4","msgtype":"3","createtime":"2015-09-28 14:26:14"},{"id":"22","user_id":"61","content":"/videos/2015/9/1443420108590.mp4","msgtype":"3","createtime":"2015-09-28 14:01:52"},{"id":"20","user_id":"61","content":"/images/2015/9/1443416785658.png","msgtype":"2","createtime":"2015-09-28 13:06:26"},{"id":"21","user_id":"61","content":"/images/2015/9/1443416785691.png","msgtype":"2","createtime":"2015-09-28 13:06:26"},{"id":"17","user_id":"61","content":"/images/2015/9/1443416606422.png","msgtype":"2","createtime":"2015-09-28 13:03:28"},{"id":"18","user_id":"61","content":"/images/2015/9/1443416606676.png","msgtype":"2","createtime":"2015-09-28 13:03:28"},{"id":"19","user_id":"61","content":"/images/2015/9/1443416606944.png","msgtype":"2","createtime":"2015-09-28 13:03:28"},{"id":"13","user_id":"61","content":"/images/2015/9/1443416605748.png","msgtype":"2","createtime":"2015-09-28 13:03:27"}],"_links":{"self":{"href":"http://localhost:8080/message/familychat?userId=61&page=1&per-page=10"},"next":{"href":"http://localhost:8080/message/familychat?userId=61&page=2&per-page=10"},"last":{"href":"http://localhost:8080/message/familychat?userId=61&page=3&per-page=10"}},"_meta":{"totalCount":22,"pageCount":3,"currentPage":1,"perPage":10}}

第二页返回数据:

{"items":{"10":{"id":"14","user_id":"61","content":"/images/2015/9/1443416605939.png","msgtype":"2","createtime":"2015-09-28 13:03:27"},"11":{"id":"15","user_id":"61","content":"/images/2015/9/1443416606145.png","msgtype":"2","createtime":"2015-09-28 13:03:27"},"12":{"id":"16","user_id":"61","content":"/images/2015/9/1443416606158.png","msgtype":"2","createtime":"2015-09-28 13:03:27"},"13":{"id":"11","user_id":"61","content":"/images/2015/9/1443416284963.jpg","msgtype":"2","createtime":"2015-09-28 12:58:06"},"14":{"id":"12","user_id":"61","content":"/images/2015/9/1443416285024.jpg","msgtype":"2","createtime":"2015-09-28 12:58:06"},"15":{"id":"9","user_id":"61","content":"/images/2015/9/1443415655991.png","msgtype":"2","createtime":"2015-09-28 12:47:36"},"16":{"id":"10","user_id":"61","content":"/images/2015/9/1443415655986.png","msgtype":"2","createtime":"2015-09-28 12:47:36"},"17":{"id":"7","user_id":"61","content":"/images/2015/9/1443415596499.png","msgtype":"2","createtime":"2015-09-28 12:46:37"},"18":{"id":"8","user_id":"61","content":"/images/2015/9/1443415596529.png","msgtype":"2","createtime":"2015-09-28 12:46:37"},"19":{"id":"5","user_id":"61","content":"/images/2015/9/1443414274906.png","msgtype":"2","createtime":"2015-09-28 12:24:41"}},"_links":{"self":{"href":"http://localhost:8080/message/familychat?userId=61&page=2&per-page=10"},"first":{"href":"http://localhost:8080/message/familychat?userId=61&page=1&per-page=10"},"prev":{"href":"http://localhost:8080/message/familychat?userId=61&page=1&per-page=10"},"next":{"href":"http://localhost:8080/message/familychat?userId=61&page=3&per-page=10"},"last":{"href":"http://localhost:8080/message/familychat?userId=61&page=3&per-page=10"}},"_meta":{"totalCount":22,"pageCount":3,"currentPage":2,"perPage":10}}

怎么去掉这个分页索引呢求教

Yii用户专享10000元红包

Yii开发者福利,万元红包可用于云服务器、高防服务器、裸金属服务器、云数据库、高防IP购买和续费

  • 回复于 2017-03-30 13:30 举报

    你好!这个解决了吗? 最后怎么解决的呢?

  • 回复于 2017-04-10 18:19 举报

    这个结果是因为类ArrayDataProviderprepareModels()方法第91行,获取数据是$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);array_slice最后一个参数固定为true,所以不会重置数组的数字索引。为了json_encode的结果没有数组下标,所以需要的是将获取到当前页的数据数组的数字索引重新排列数组下标。

    觉得可行的方法是,
    方法1:获取到当前分页的数据之后,将当前分页的数据放到新的数组中,如:

    $result_data = $result->getModels();
    $items = array();
    foreach($result_data as $row){
        array_push($items,$row);
    }
    echo json_encode($items);
    

    方法2:使用array_merge方法重新排列数组下标

    $items = array_merge($result->getModels());
    
您需要登录后才可以回复。登录 | 立即注册