2016-04-29 11 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:文字列、初期値のためのSelectセレクトウィジェットに表示するテキストを。

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

Firtソリューションが動作しませんが、2番目と私は私が何を望むかachive;)おかげで:

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

またInputWidgetとしてそれを使用することができます! –

関連する問題