筛选 YII2 后 Select2 继续加载

Posted

技术标签:

【中文标题】筛选 YII2 后 Select2 继续加载【英文标题】:Select2 keep loading after filter YII2 【发布时间】:2017-09-20 02:42:25 【问题描述】:

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

Yii2 在我加载页面时渲染 select2:

enter image description here

所以它起作用了,数据被填充了,但是 select2 继续显示加载图标:

enter image description here

这是我的代码,在“名称”列中选择2

<?=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>',
        ]
    ])?>

希望你的帮助:)

【问题讨论】:

【参考方案1】:

试试这个:

 [
    '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'
 ],

【讨论】:

我正在使用网格视图,并在其中放置了[ 'attribute' =&gt; 'Sub_Division_Name', 'filter' =&gt; Select2::widget([ 'data' =&gt; \yii\helpers\ArrayHelper::map(\common\models\SurveyHescoSubdivision::find()-&gt;all(),'id','name'), 'name' =&gt; 'Sub_Division_Name', 'options' =&gt; [ 'placeholder' =&gt; 'Sub Division Name', ], 'theme' =&gt; Select2::THEME_DEFAULT, 'pluginOptions' =&gt; [ 'allowClear' =&gt; true, ], ]), ],【参考方案2】:

'pjax'=>true 更改为 false 干得好

【讨论】:

以上是关于筛选 YII2 后 Select2 继续加载的主要内容,如果未能解决你的问题,请参考以下文章

不加载kartik 的select2 作为GridView 中的过滤器。 Yii2

select2 和 Pjax 在 yii2 中不能一起工作

实现select2与jqGrid联动动态重新加载数据

实现select2与jqGrid联动动态重新加载数据

YII2:kartik Select2

Yii2 在“select2”小部件上需要验证规则