2017-08-07 103 views
0

我要添加过滤器与列表视图(有些事情喜欢在网格视图),但我不不知道如何像网格视图添加过滤器搜索列表视图Yii2

过滤器我想是的CheckBox如何做和DropDown选项。

这是我的操作代码在SiteController

<?php 

public function actionMobiles(){ 
    $searchModel = new MobileSearch(); 
    //$dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
//$dataProvider = $searchModel->search(Yii::$app->request->queryParams); 

    $dataProvider = new ActiveDataProvider([ 
    'query' => Mobile::find(), 
    'pagination' => [ 
    'pageSize' => 1, 
], 
]); 

// get the posts in the current page 
//$posts = $dataProvider->getModels(); 
return $this->render('mobiles', ['dataProvider' => $dataProvider,'searchModel' => $searchModel]); 


} 

?> 

在查看文件我有2个文件:

这里是手机浏览:

<?php 
use yii\widgets\ListView; 
use yii\helpers\Html; 
use yii\grid\GridView; 
use yii\helpers\ArrayHelper ; 
use app\models\Mobile; 
?> 

<?= ListView::widget([ 
    'dataProvider' => $dataProvider, 
     //'filterModel' => $searchModel, 
     'itemView' => '_mobiles', 

]); ?> 

并在_mobiles我有:

<?php 
use yii\widgets\DetailView; 
?> 

<?= DetailView::widget([ 
    'model' => $model, 
    'attributes' => [ 
     //'id', 
     'id', 
     'title', 
     //'ativo', 
    ], 
]) ?> 

它正确地显示数据库中的数据以及它的分页。 但如何添加筛选器更新列表(CheckBox和DropDown列表用于更新)?

+0

加 'filterModel'=> $ searchModel,在你的列表视图控件 – Gunnrryy

+0

??? @Gunnrryy – areff

+0

设置未知属性:yii \ widgets \ ListView :: filterModel @Gunnrryy – areff

回答

0

这里是你的代码,你应该根据你的代替属性和型号名称。

[ 
     'attribute' => 'attribute_name', 
     'label' => 'Email', 
     'value' => function($model){ 
      return $model->attribute_name; 
     }, 
     'filterType' => GridView::FILTER_SELECT2, 
     'filter' => \yii\helpers\ArrayHelper::map([Your Model]::find()->asArray()->all(), 'id', 'email'), 
     'filterWidgetOptions' => [ 
      'pluginOptions' => ['allowClear' => true], 
     ], 
     'filterInputOptions' => ['placeholder' => 'Email', 'id' => 'grid--search-email'] 
    ], 
+0

就像其他gridviews? 可以分页吗? – areff

+0

它只是网格视图内的另一个属性。你可以添加它作为一个新的列,也可以分页。 – Gvep

相关问题