GridView To Enable Search Option

Hi Expert,

I’m new to gridview. want to have Search function on the gridview data. Can I know how to enable ?




<?php


$gridColumns = [

    [

        'class' => '\kartik\grid\CheckboxColumn',

        'headerOptions' => ['class'=>'kartik-sheet-style'],

        'rowSelectedClass' => GridView::TYPE_WARNING,

    ],


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


    [

        'attribute' => 'unique_id',

        'value' => function ($model) {

            if (isset($model->unique_id)) {

                return StringHelper::truncate(Html::encode($model->unique_id), 20);

            }

            return null;

        }

    ],

    [

    'class'=>'kartik\grid\BooleanColumn',

    'attribute'=>'formupd_flag',

    'trueIcon'=>'<span class="glyphicon glyphicon-ok text-success"></span>',

    'falseIcon'=>'<span class="glyphicon glyphicon-remove text-danger"></span>',

    'vAlign'=>'middle',

    ],

    ['class' => 'kartik\grid\ActionColumn',

        'controller' => 'accesscontrols2',

        'visible' => true,

        'dropdown'=> true,

        'dropdownButton' => ['class'=>'btn btn-primary'],

        'dropdownOptions' => ['class' => 'pull-right'],

        'buttons' => [


                 //delete button

                'delete' => function ($url) 

                    {

                    $options = array_merge([

                        'title' => Yii::t('app', 'Delete'),

                        'aria-label' => Yii::t('app', 'Delete'),

                        'data-confirm' => Yii::t('app', 'Are you sure you want to delete this Unique ID from Access Control?'),

                        'data-method' => 'post',

                        'data-pjax' => '0',

                    ], []);

                    return '<li>'.Html::a(

                        '<span class="glyphicon glyphicon-bin"> </span> '.

                        Yii::t('app', 'Delete'),

                        $url,

                        $options

                    ).'</li>';

                     },

                 ], 

            'urlCreator' => function ($action, $model) 

                {

                if ($action === "delete") {

                    $url = Url::to(['deleteuniqueid', 'id' => $model->id]);

                    return $url;

                }

                return '';

            },

            'template' => Yii::$app->user->can('edit_own_content') ?

                '{delete}' :

                '{view}',

        ],

    ];


?>


    <div class="google-analytics-index">

        <div class="row">

            <div class="col-md-12">


               <?= 


                GridView::widget([

                    'id' => 'accesscontrols-grid',

                    'dataProvider' => $dataProvider,

                    'filterModel'=>$searchModel,

                    'columns' => $gridColumns,

                    'resizableColumns' => false,

                    'pjax' => false,

                    'export' => false,

                    'responsive' => true,

                    'bordered' => true,

                    'striped' => true,

                    'panelTemplate' => '<div class="panel {type}">

                    {panelHeading}

                    {panelBefore}

                    {items}

                    <div style="text-align: center">{pager}</div>

                </div>',

                    'panel' => [

                        'type'=>GridView::TYPE_INFO,

                        'heading'=> Yii::t('accesscontrols', 'Access Controls') . ' <small class="panel-subtitle hidden-xs">'.

                            Yii::t('accesscontrols', 'Unique ID Listing').'</small>',

                        'footer'=>false,

                        // Visible only for admin user

                        'before'=> (!empty(Yii::$app->user) && Yii::$app->user->can("admin")) ?

                            ActionBar::widget([

                                'grid' => 'accesscontrols-grid',

                                'templates' => [

                                    '{bulk-actions}' => ['class' => 'col-xs-6 col-md-2 col-md-offset-10'],

                                ],         

                                'bulkActionsItems' => [

                                    'General' => ['general-delete' => 'Delete'],

                                ],

                                'bulkActionsOptions' => [

                                    'options' => [

                                        'general-delete' => [

                                            'url' => Url::toRoute('delete-multiple'),

                                            'data-confirm' => Yii::t(

                                                'accesscontrols',

                                                'Are you sure you want to delete these Unique ID from Access Controls? All data related to each item will be deleted. This action cannot be undone.'

                                            ),

                                        ],

                                    ],

                                'class' => 'form-control',

                                ],

                            ]

                             ) : null,

                    ],

                    'toolbar' => false

                ]);


                ?>

            </div>

        </div>

    </div>



The key is


'filterModel'=>$searchModel,

have a look here.

You need a second searchmodel.

Got it. Thanks for your tips and not is working good. Appreciate your guidance.