2016-04-29 61 views
1

我在Yii2项目中遇到了另一个虚拟问题。我有一个标准的GridView在我看来,这liek定义:kartik Select2作为yii2 Grid中的过滤器输入

<?= GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'layout' => '{items}{summary}', 
     'columns' => [ 
      [ 
       'class' => 'yii\grid\SerialColumn', 
       'contentOptions' => [ 
        'style' => 'vertical-align: middle;' 
       ] 
      ], 
      [ 
       'attribute' => 'name', 
      ], 
      [ 
       'attribute' => 'type', 
       'value' => function($model){ 
        /* @var $model app\models\Object */ 
        return $model->typeNames()[$model->type]; 
       }, 
       'filter' => Select2::widget([ 
        'name' => 'ObjectSearch[type]', 
        'data' => Object::typeNames(), 
        'theme' => Select2::THEME_BOOTSTRAP, 
        'hideSearch' => true, 
        'options' => [ 
         'placeholder' => 'Wybierz typ obiektu...', 
         'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null 
        ] 
       ]), 
      ], 
      [ 
       'attribute' => 'countryId', 
       'value' => 'country.name', 
       'filter' => Select2::widget([ 
        'name' => 'ObjectSearch[countryId]', 
        'data' => Country::forWidgets(), 
        'theme' => Select2::THEME_BOOTSTRAP, 
        'options' => [ 
         'placeholder' => 'Wybierz państwo...' 
        ] 
       ]), 
      ], 
    //other columns 
     ], 
    ]); ?> 

我已经定义了一个searchModel类:

class ObjectSearch extends Object 
{ 
    public function rules() 
    { 
     return [ 
      [['type'], 'integer'], 
      [['name', 'city', 'countryId'], 'string'] 
     ]; 
    } 

    //some other functions 

    public function search($params) 
    { 
     $query = Object::find()->where(['userId' => Yii::$app->user->id]); 
     $query->joinWith('country'); 

     $dataProvider = new ActiveDataProvider([ 
      'query' => $query, 
     ]); 

     $dataProvider->sort->attributes['countryId'] = [ 
      'asc' => ['countries.name' => 'ASC'], 
      'desc' => ['countries.name' => 'DESC'] 
     ]; 

     if (!$this->load($params) && $this->validate()) { 
      return $dataProvider; 
     } 

     $query->andFilterWhere(['like', 'objects.name', $this->name]) 
      ->andFilterWhere(['like', 'city', $this->city]) 
      ->andFilterWhere(['=', 'objects.countryId', $this->countryId]) 
      ->andFilterWhere(['=', 'type', $this->type]); 

     return $dataProvider; 
    } 
} 

排序和查找工作正常 - 我已经得到了正确的结果。那么我的问题是什么?对于在textInput中输入一些文本时的标准列,此输入的值将保留在搜索后。但是,当我在Select2窗口小部件搜索工作中选择一些值时,但搜索后所选值消失,并且我只有一个占位符。

感谢您的帮助,
卡米尔

回答

3

您只需要使用initValueText PARAM:

initValueText:字符串文本中选择二小部件的初始值显示。

例如, :

Select2::widget([ 
    'name' => 'ObjectSearch[type]', 
    'data' => Object::typeNames(), 
    'initValueText' => $searchModel->type, 
    // ... other params 
]) 

你也可以使用它作为一个InputWidget:

Select2::widget([ 
    'model' => $searchModel, 
    'attribute' => 'type', 
    'data' => Object::typeNames(), 
    // ... other params 
]) 
+0

Firt解决方案不起作用,但与第二我achive我想要的东西;)谢谢! –

相关问题