简体   繁体   English

没有在网格视图中加载kartik的select2作为过滤器。 Yii2

[英]Not load kartik's select2 as filter in GridView. Yii2

I try to filter a column using Select2 Kartik, but nothing appears. 我尝试使用Select2 Kartik过滤列,但未显示任何内容。 The select2 does not load completely. select2不能完全加载。 Show the display label as None. 将显示标签显示为“无”。

/views/user/index.php

    'columns' => [
       [
           'class' => 'yii\grid\SerialColumn',
       ],
       'name',

    /*
       This code not works :(
    */

       [
           'attribute' => 'identification_type',
           'value'=> function($model){
               return Yii::t('app', TYPE_ID[$model->identification_type]);
           },
           'filter' => Select2::widget([
               'model' => $searchModel,
               'attribute' => 'identification_type',
               'data' => Arrai::t('person',TYPE_ID),
               'theme' => Select2::THEME_BOOTSTRAP,
               'hideSearch' => true,
               'options' => [
                   'placeholder' => Yii::t('person', 'Identification type'),
           ]),
       ],

    /*
       This code works!
    */

       [
           'attribute' => 'identification_type',
           'filter' => Arrai::t('app', TYPE_ID),
           'value'=> function($model){
                return Yii::t('app', TYPE_ID[$model->identification_type]);
            },
           'filterInputOptions' => [
                'class' => 'selectpicker',
                'data-style'=>"btn btn-primary btn-round",
                'title'=> "Sin seleccion",
           ],
        ],

    // other columns


]

And the following is the result html: 以下是结果html:

Result HTML

<div class="form-group is-empty">
<select id="personsearch-identification_type" class="form-control" name="PersonSearch[identification_type]" 
    data-s2-options="s2options_6cc131ae"
    data-krajee-select2="select2_0ed9734f"
    style="display:none">
       <option value="">Tipo de identificación</option>
       <option value="CC">Citizenship card</option>
       <option value="CE">Foreigner ID</option>
       <option value="PAS">Passport</option>
       <option value="NIT">NIT</option>
</select>
<span class="material-input"></span></div>

In this result, a <span> tag is required to display the Select2. 在此结果中,需要<span>标记才能显示Select2。 But it does not appear. 但是它没有出现。

Change your column configurations to the following for using select2, you are providing the text to the data option if I am not wrong Arrai::t('person', TYPE_ID) whereas it should be an array either hardcoded or from the database. 将列配置更改为以下内容以使用select2,如果我没有记错的话,您将向data选项提供文本Arrai::t('person', TYPE_ID) ,但它应该是硬编码数组或来自数​​据库的数组。

[
   'attribute' => 'identification_type',
   'value'=> function($model){
          return Yii::t('app', TYPE_ID[$model->identification_type]);
    },
    'filter' => Select2::widget([
         'model' => $searchModel,
         'attribute' => 'identification_type',
         'data' => ['1'=>'option1','2'=>'option2'],
         'theme' => Select2::THEME_BOOTSTRAP,
         'hideSearch' => true,
         'pluginOptions' => [
               'allowClear' => true,
               'width' => 'resolve',
          ],
         'options' => [
                'placeholder' => Yii::t('person', 'Identification type'),
          ]
    ),
 ],

I am using hardcoded array you can update it according to your requirements, if your values for the dropdown should be coming from the database then you can replace the array with 我使用的是硬编码数组,您可以根据自己的要求进行更新,如果下拉列表中的值应来自数据库,则可以将其替换为

yii\helpers\ArrayHelper::map(Model::find()->asArray()->all(),'id','name')

where Model should be replaced with your specific model name and 'id' , 'name' should also be replace with valid column names. 其中应将Model替换为您的特定模型名称,并将'id' , 'name'替换为'id' , 'name' ,同时还将有效列名替换为'id' , 'name'

I found the problem What happens is that I had the advanced search enabled. 我发现了问题发生的事情是我启用了高级搜索。 And the select2 of the advanced search has the same ID as the GridView filter. 并且高级搜索的select2与GridView过滤器具有相同的ID。

I just have to modify the ID name and it's ready. 我只需要修改ID名称就可以了。

[
    'attribute' => 'id_setting_person_identification_type',
    'value' => function($model){
        $type_identication = SettingPerson::getMapGroup('Tipo de identificación');
        return Yii::t('app', $type_identication[$model->id_setting_person_identification_type]);
    },
    'filterType' => GridView::FILTER_SELECT2,
    'filter' => $type_identication, 
    'filterWidgetOptions' => [
        'pluginOptions' => ['allowClear' => true],
    ],
    'filterInputOptions' => [
        'placeholder' => '-',
        'id' => 'id_setting_person_identification_type_gridview',
    ]
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM