yii2:在窗体视图中为Gridview定制分页 [英] yii2:Custom Pagination for Gridview in form view

查看:803
本文介绍了yii2:在窗体视图中为Gridview定制分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Gridview小部件包含在 _form.php 文件中,该文件运行良好。

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

?>
<?php
$ searchModel = New \app\models\ServiceChargesSearch();
$ dataProvider = $ searchModel->搜索(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>

如果我将 $ dataprovider 分页部分位于 $ searchmodel 之下,分页工作正常,但过滤器无法正常工作,反之亦然。

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



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



感谢

解决方案

我没有使用Yii2的经验,但是如果它与1相似..



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



那么你会使用一个数据提供者来设置分页,然后你通过一个不同的分页



因为我可以看到 search()方法返回一个 dataProvider ,你应该改变那里的分页。



或者我认为您可以在 search()方法返回 dataProvider like:

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

所以你不需要 dataProvider的第一个实例

至于过滤器,我不知道它是如何表现你的 ServiceChargesSearch :: search code> function

但在Yii1中,您通常会:


1 )定义模型2)使用 $ _ GET 3)中的数据填充它:通过
$ model-> search()



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

<?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 $dataprovider pagination part below the $searchmodel, pagination works fine, but then filter doesn't work and vice-versa.

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

Any solution will be greatly appreciated.

Thanks

解决方案

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

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

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.

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

$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 dataProvider that you've declared before.

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

But in Yii1 you normally:

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

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

这篇关于yii2:在窗体视图中为Gridview定制分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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