php yii2: 表格视图中 Gridview 的自定义分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28327446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:48:02  来源:igfitidea点击:

yii2:Custom Pagination for Gridview in form view

phpgridviewpaginationyii2

提问by Pawan

I have included the Gridview widget in _form.phpfile, which is working well. The problem is the filter and pagination.

我在_form.php文件中包含了 Gridview 小部件,它运行良好。问题是过滤器和分页。

<?php
$dataProvider = new ActiveDataProvider([
    'query' => \app\models\ServiceCharges::find(),
    'pagination' => [
        'pageSize' => 5,
    ],
]);

 ?>
    <?php  
   $searchModel = New \app\models\ServiceChargesSearch(); 
   $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

   ?>

        </div>
</div>
<div>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

           'id',
           'service_name',                
           'room_category',
           'charges_cash',
           'charges_cashless',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

    </div> 

If I am putting the $dataproviderpagination part below the $searchmodel, pagination works fine, but then filter doesn't work and vice-versa.

如果我将$dataprovider分页部分放在下面$searchmodel,分页工作正常,但过滤器不起作用,反之亦然。

How can I have both filter and pagination working in the _form.php.

如何在 _form.php 中同时使用过滤器和分页。

Any solution will be greatly appreciated.

任何解决方案将不胜感激。

Thanks

谢谢

回答by Alexandru Trandafir Catalin

I have no experience with Yii2 but if it is similar than 1..

我没有使用 Yii2 的经验,但如果它类似于 1..

Why are you declaring dataProvidertwice? I imagine the first one is to be able to customize page size.

你为什么要声明dataProvider两次?我想第一个是能够自定义页面大小。

So what happens is you use one data provider to set pagination but then you pass a different one to the table.

所以会发生什么是你使用一个数据提供者来设置分页,然后你将另一个数据提供者传递给表。

Second I don't know how your model looks inside but..

其次,我不知道你的模型在里面看起来如何,但是..

Since I can see the search()method returns a dataProvider, you should change the pagination inside there.

由于我可以看到该search()方法返回 a dataProvider,因此您应该更改其中的分页。

Or I think you can change it right after the search()method returns the dataProviderlike:

或者我认为您可以在search()方法返回后立即更改它dataProvider

$searchModel = New \app\models\ServiceChargesSearch(); 
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize=5;

So you don't need the first instance of dataProviderthat you've declared before.

所以你不需要dataProvider你之前声明的第一个实例。

As for the filters I do not know how it exactly behaves your ServiceChargesSearch::searchfunction

至于过滤器,我不知道它究竟如何表现你的ServiceChargesSearch::search功能

But in Yii1 you normally:

但是在 Yii1 中,您通常会:

1) Define model 2) Fill it up with data from $_GET3) Pass $model->search()to grid

1) 定义模型 2) 用数据填充它$_GET3) 传递 $model->search()到网格

If filters still not work you can provide code from the model.

如果过滤器仍然不起作用,您可以提供模型中的代码。

回答by shades3002

I do it this way in the controller:

我在控制器中这样做:

public function actionIndex()
{
    $searchModel = new YourModels();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->pagination->pageSize = 10;

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}