Select2在过滤器YII2之后继续加载

时间:2017-04-23 11:49:02

标签: gridview yii2 jquery-select2

感谢阅读。我对select2有一些问题 - 过滤gridview Yii2:

当我加载页面时,Yii2渲染select2:

enter image description here

所以它有效,数据已填满,但是select2保持显示加载图标:

enter image description here

这是我的代码,select2在“name”列中

<?=GridView::widget([
        'id'=>'crud-datatable',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'pjax'=>true,
        'pjaxSettings' => ['options' => ['enablePushState' => false]],
        'summary' => 'Từ {begin} đến {end}/ Tổng {totalCount} bản ghi',
        'columns' => array(
            [
                'class' => 'kartik\grid\CheckboxColumn',
                'width' => '20px',
            ],
            [
                'class' => 'kartik\grid\SerialColumn',
                'width' => '30px',
            ],
            [
                'class'=>'\kartik\grid\DataColumn',
                'attribute'=>'name',
                'headerOptions'=>['style'=>'text-align:center'],
                'filter'=>\kartik\select2\Select2::widget([
                    'model' => $searchModel,
                    'attribute' => 'name',
                    'data' => \yii\helpers\ArrayHelper::map(\common\models\Catnew::find()->all(), 'name', 'name'),
                    'theme' => \kartik\select2\Select2::THEME_BOOTSTRAP,
                    'hideSearch' => false,
                    'options' => [
                        'placeholder' => 'Search for name',
                    ]
                ])
            ],
            [
                'class'=>'\kartik\grid\DataColumn',
                'attribute'=>'position',
            ],
            [
                'class'=>'\kartik\grid\DataColumn',
                'attribute'=>'active',
            ],
            [
                'class'=>'\kartik\grid\DataColumn',
                'attribute'=>'home',
            ],
            [
                'class'=>'\kartik\grid\DataColumn',
                'attribute'=>'parent',
            ],
            [
                'class' => 'kartik\grid\ActionColumn',
                'dropdown' => false,
                'vAlign'=>'middle',
                'urlCreator' => function($action, $model, $key, $index) {
                    return Url::to([$action,'id'=>$key]);
                },
                'viewOptions'=>['role'=>'modal-remote','title'=>'View','data-toggle'=>'tooltip'],
                'updateOptions'=>['role'=>'modal-remote','title'=>'Update', 'data-toggle'=>'tooltip'],
                'deleteOptions'=>['role'=>'modal-remote','title'=>'Delete',
                    'data-confirm'=>false, 'data-method'=>false,
                    'data-request-method'=>'post',
                    'data-toggle'=>'tooltip',
                    'data-confirm-title'=>'Are you sure?',
                    'data-confirm-message'=>'Are you sure want to delete this item'],
            ],

        ),
        'toolbar'=> [
            ['content'=>
                Html::a('<i class="glyphicon glyphicon-plus"></i>', ['create'],
                ['role'=>'modal-remote','title'=> 'Create new Catnews','class'=>'btn btn-default']).
                Html::a('<i class="glyphicon glyphicon-repeat"></i>', [''],
                ['data-pjax'=>1, 'class'=>'btn btn-default', 'title'=>'Reset Grid']).
                '{toggleData}'.
                '{export}'
            ],
        ],          
        'striped' => true,
        'condensed' => true,
        'responsive' => true,          
        'panel' => [
            'type' => 'primary', 
            'heading' => '<i class="glyphicon glyphicon-list"></i> Catnews listing',
            'before'=>'<em>* Resize table columns just like a spreadsheet by dragging the column edges.</em>',
            'after'=>BulkButtonWidget::widget([
                        'buttons'=>Html::a('<i class="glyphicon glyphicon-trash"></i>&nbsp; Delete All',
                            ["bulkdelete"] ,
                            [
                                "class"=>"btn btn-danger btn-xs",
                                'role'=>'modal-remote-bulk',
                                'data-confirm'=>false, 'data-method'=>false,
                                'data-request-method'=>'post',
                                'data-confirm-title'=>'Are you sure?',
                                'data-confirm-message'=>'Are you sure want to delete this item'
                            ]),
                    ]).                        
                    '<div class="clearfix"></div>',
        ]
    ])?>

希望你的帮助:)

2 个答案:

答案 0 :(得分:3)

试试这个:

 [
    'attribute' => 'name',
    'value' => \yii\helpers\ArrayHelper::map(\common\models\Catnew::find()->all(), 'name', 'name'), 
    'filterType' => GridView::FILTER_SELECT2,
    'filter' =>\yii\helpers\ArrayHelper::map(\common\models\Catnew::find()->all(), 'name', 'name'),
    'filterWidgetOptions' => [
        'pluginOptions' => ['allowClear' => true],
    ],
    'filterInputOptions' => ['placeholder' => '...'],
    'format' => 'raw'
 ],

答案 1 :(得分:-1)

&#39; pjax&#39; =&gt; true 更改为 false 做得好