2016-04-29 26 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我想要的東西;)謝謝! –

相關問題