Search code examples
yii2yii2-advanced-appyii2-basic-app

Yii2 GridView Custom Search TextBox


As the design require to use the search Textbox out site the gridview what I want to do is to search from textbox outside the gridview by attribute 'name'. I don't know the way or best practice how to do it. Please help!!! enter image description here


Solution

  • If your code is generated by Gii you should have already available a _search partial view for this .. otherwise you can take a look at this guide http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html .. at this section http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html

    essentially as you can see in doc you should create a proper partial view

        <div class="post-search">
          <?php $form = ActiveForm::begin([
              'action' => ['index'],
              'method' => 'get',
          ]); ?>
    
          <?= $form->field($model, 'title') ?>
    
          <?= $form->field($model, 'creation_date') ?>
    
          <div class="form-group">
              <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
              <?= Html::submitButton('Reset', ['class' => 'btn btn-default']) ?>
          </div>
    
          <?php ActiveForm::end(); ?>
      </div>
    

    and render it inside your grid view

    with

        <?= $this->render('_search', ['model' => $searchModel]) ?>