dataprovider limit

Hi All,

I have a problem with yii dataprovider.

I want to have data to be shown limited only 30 rows from all data.

Then I want to use the paging of yii gridview, set to 5 per page.

so this is my code :




use yii\grid\GridView;

use app\models\Post;

use yii\data\ActiveDataProvider;







$model = Post::find()->limit(40);

$dataProvider = new ActiveDataProvider(

	[

		'query'=>$model,

			'pagination' => [

			'pageSize' => 5,

		]

	]

);




echo GridView::widget([

		'dataProvider' => $dataProvider,

		'columns' => [

			['class' => 'yii\grid\SerialColumn'],

			'post_id',

			'message', 

			],


]);



It displayed the data, but showing all the data which is 1522 records.

I want to limit the data on the provider by the query (active record) then limit it again by data provider pagination.

40 data to be shown, then 5 per page from 1522 records data.

Any advice ?

Thanks

in pagination attribute of ActiveDataProvider, try to add limit attribute:




.....

'pagination' => [

    'pageSize' => 5,

    'limit' => 40

]

....



Unfortunately limit is read only in activedataprovider.

Any other solutions ?

You are right.

Other solution is to use ArrayDataProvider, based on return of all().





use yii\grid\GridView;

use app\models\Post;

use yii\data\ActiveDataProvider;





$arrModels = Post::find()->limit(40)->all();

$dataProvider = new ArrayDataProvider( [ 'allModels' => $arrModels, 'pagination' => [ 'pageSize' => 5 ] );




echo GridView::widget([

                'dataProvider' => $dataProvider,

                'columns' => [

                        ['class' => 'yii\grid\SerialColumn'],

                        'post_id',

                        'message', 

                        ],


]);



Perfect !!

Thank you !

Solved.