具有CGridView分页的ArrayDataProvider Yii2

时间:2014-12-12 06:12:32

标签: yii2

有像这篇文章一样的问题 CArrayDataProvider with CGridView pagination Yii

以上帖子在yii1,现在yii2发布所以我需要在yii2中相同 我的数组也基于用户搜索参数,当我点击分页时,用户搜索参数没有通过,所以我会这样做吗?

下面是我的数组$ retVal

yii\data\ArrayDataProvider Object
      (
        [key] => id
[allModels] => Array
    (
        [0] => Array
            (
                [id] => 1
                [date] => 2014-12-01
                [distance] => 0
            )

        [1] => Array
            (
                [id] => 2
                [date] => 2014-12-01
                [distance] => 0
            )

        [2] => Array
            (
                [id] => 3
                [date] => 2014-12-01
                [distance] => 0
            )

        [3] => Array
            (
                [id] => 4
                [date] => 2014-12-01
                [distance] => 0
            )

        [4] => Array
            (
                [id] => 5
                [date] => 2014-12-01
                [distance] => 0
            )

        [5] => Array
            (
                [id] => 6
                [date] => 2014-12-01
                [distance] => 0
            )

        [6] => Array
            (
                [id] => 7
                [date] => 2014-12-01
                [distance] => 0
            )

        [7] => Array
            (
                [id] => 8
                [date] => 2014-12-01
                [distance] => 0
            )

        [8] => Array
            (
                [id] => 9
                [date] => 2014-12-01
                [distance] => 0
            )

        [9] => Array
            (
                [id] => 10
                [date] => 2014-12-01
                [distance] => 0
            )

        [10] => Array
            (
                [id] => 11
                [date] => 2014-12-01
                [distance] => 0
            )
         )

[id] => 
[_sort:yii\data\BaseDataProvider:private] => yii\data\Sort Object
    (
        [enableMultiSort] => 
        [attributes] => Array
            (
            )

        [sortParam] => sort
        [defaultOrder] => Array
            (
            )

        [route] => 
        [separator] => ,
        [params] => 
        [urlManager] => 
        [_attributeOrders:yii\data\Sort:private] => 
    )

[_pagination:yii\data\BaseDataProvider:private] => yii\data\Pagination Object
    (
        [pageParam] => page
        [pageSizeParam] => per-page
        [forcePageParam] => 1
        [route] => 
        [params] => 
        [urlManager] => 
        [validatePage] => 1
        [totalCount] => 0
        [defaultPageSize] => 20
        [pageSizeLimit] => Array
            (
                [0] => 1
                [1] => 50
            )

        [_pageSize:yii\data\Pagination:private] => 20
        [_page:yii\data\Pagination:private] => 
    )

[_keys:yii\data\BaseDataProvider:private] => 
[_models:yii\data\BaseDataProvider:private] => 
[_totalCount:yii\data\BaseDataProvider:private] => 
[_events:yii\base\Component:private] => Array
    (
    )

[_behaviors:yii\base\Component:private] => 
)

下面是我的arraydayaprovider和$ retVal是我传递的数组

      $dataprovider = new ArrayDataProvider([
      'allModels' => $retVal,
       'pagination' => array('pageSize' => 20),

      ]);

      return $dataprovider;
}

以下是查看代码

             <?php

       use yii\helpers\Html;
       use kartik\grid\GridView;
       //use yii\grid\GridView;


    $this->title = Yii::t('app', 'Speed Report');
    $this->params['breadcrumbs'][] = $this->title;
    ?>
    <div class="usertype-index">
     <div class="panel panel-default">
    <div class="panel-body">
    <div class="row">
    <div class="col-lg-12">
     <?php 
      GridView::widget([
        'dataProvider'=> $dataProvider,

        'columns' =>[
        [ 'class' => '\kartik\grid\SerialColumn'],

        'time',
        'speed',
        'address',
          //['class' => '\kartik\grid\ActionColumn'],

        ],


        'panel' => [
        'heading'=>'<h3 class="panel-title"><i class="white fa fa-bar-chart"></i> Distance Report</h3>',
        'type'=>'primary',
        'before'=>Html::a(Yii::t('app', '<i class="glyphicon glyphicon-plus"></i> Generate {modelClass}', ['modelClass' => 'Report',]), ['speed-report'], ['class' => 'btn btn-primary']) ,
   // 'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset Grid', ['index'], ['class' => 'btn btn-info']),
        'showFooter'=>false,
        'pager'=>false,
        'toggleData' => false
        ],
        ]);?>


      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

the docs开始,您需要为数据提供者提供一个关键字段(类似于您之前发布的问题)。

示例:

$query = new Query;
$provider = new ArrayDataProvider([
    'key' => 'id' //make this unique identifier of your models.
    'allModels' => $query->from('post')->all(),
    'pagination' => [
        'pageSize' => 10,
    ],
]);

因此,在您的情况下,您可能需要在id的每个集合中添加$retval字段并执行此操作:

  $dataprovider = new ArrayDataProvider([
  'key' => 'id', //or whatever you id actually is of these models.
  'allModels' => $retVal,
   'pagination' => array('pageSize' => 20),

  ]);

  return $dataprovider;
}