1
votes

I'm really new in Yii2 and I still don't know how to configure it properly. I noticed that the GridView has search fields on each column. What I need now is to create a main/single search field wherein a user can input keywords then results will show in the GridView after hitting the search button.

Is this possible? I also used this Kartik widget in my search form field which has a dropdown list in it. Image here.

We're told to use this dropdown search and when the user inputs some keywords (sometimes returns 'No results found' on the dropdown list), and clicks the Search button, the page will refresh displaying all the results based on the keywords inputted.

I also searched some problems same as mine here in SO, such as these:

Yii2 Search model without using GridView

yii 2 , make an active form text field master search field

I found no luck. The second link doesn't have any answers.

I will include my action controller here in case you need it.

public function actionIndex()
{
    $session = Yii::$app->session;
    $searchModel = new PayslipTemplateSearch();

    $PayslipEmailConfig = PayslipEmailConfig::find()->where(['company_id'=> new \MongoId($session['company_id'])])->one();

    $payslipTemplateA = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->andwhere(['template_name' => 'A'])->one();
    $payslipTemplateB = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->andwhere(['template_name' => 'B'])->one();

    $pTemplateModel = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->all();
    $user = User::find()->where(['_id' => new \MongoId($session['user_id'])])->one();
    $module_access = explode(',', $user->module_access);


    $dataProvider = User::find()->where(['user_type' => 'BizStaff'])->andwhere(['parent' => new \MongoId($session['company_owner'])])->all();

    return $this->render('index', [
        'PayslipEmailConfig' => $PayslipEmailConfig,
        'dataProvider' => $dataProvider,
        'payslipTemplateA' => $payslipTemplateA,
        'payslipTemplateB' => $payslipTemplateB,
    ]);
}

My view, index.php

<?php 
    $users = User::find()->where(['user_type' => 'BizStaff'])->andwhere(['parent' => new \MongoId($session['company_owner'])])->all();
    echo $this->render('_search', ['model' => new User(), 'users' => $users]); 
?>

_search.php

<?php $form = ActiveForm::begin([
    'action' => ['searchresults'],
    'method' => 'get',
    'id' => 'searchForm'
]); ?>

<?php   
    $listData = array();
    foreach ($users as $user) {
        $listData[(string)$user->_id] = $user->employeeId. ' '.$user->fname.' '.$user->lname;
    }
    
    echo $form->field($model, '_id')->widget(Select2::classname(), [
        'data' => $listData,
        'addon' => [
                'append' => [
                    'content' => Html::button('Search', ['class'=>'btn btn-primary']),
                    'asButton' => true
                ]
        ],
        'options' => [ 'class' => 'dropdown-responsive', 'responsive' => true, 'placeholder' => 'Search employee ID or name (e.g. 10015 or John Cruz)', 'id' => 'user_id', 'name' => 'id'],
        'pluginOptions' => [
            'allowClear' => true,
            'responsive' => true
        ],
    ]);
?>            

<?php ActiveForm::end(); ?>

Really need help for this one.

1
can you post your index.php file?GAMITG
You need to pass searchModel to index file. and than that searchModel pass to _search file as model.GAMITG
so I will use this instead, echo $this->render('_search', ['searchModel' => new User(), 'users' => $users]); ?Ethelene Laverne
No. like as this $searchModel = new UserSearch(); echo $this->render('_search', ['model' => $searchModel, 'users' => $users]);GAMITG
ok. but how does it work?Ethelene Laverne

1 Answers

1
votes

This example code as per your requirement. so, try it.

User Searchmodel's search() method.

public function search($params)
{
   $query = User::find();
   $query->where(['user_type' => 'BizStaff'])->andwhere(['parent' => new \MongoId($session['company_owner'])])->all();

   $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
  ......
  ........
}

Controller's actionIndex

public function actionIndex()
{
    $session = Yii::$app->session;
    /*  $searchModel = new PayslipTemplateSearch();  */

    $PayslipEmailConfig = PayslipEmailConfig::find()->where(['company_id'=> new \MongoId($session['company_id'])])->one();

    $payslipTemplateA = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->andwhere(['template_name' => 'A'])->one();
    $payslipTemplateB = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->andwhere(['template_name' => 'B'])->one();

    $pTemplateModel = PayslipTemplate::find()->where(['company_id' => new \MongoId($session['company_id'])])->all();
    $user = User::find()->where(['_id' => new \MongoId($session['user_id'])])->one();
    $module_access = explode(',', $user->module_access);

    $searchModel = new UserSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    /*
       $dataProvider = User::find()->where(['user_type' => 'BizStaff'])->andwhere(['parent' => new \MongoId($session['company_owner'])])->all();
    */

    return $this->render('index', [
        'PayslipEmailConfig' => $PayslipEmailConfig,
        'dataProvider' => $dataProvider,
        'payslipTemplateA' => $payslipTemplateA,
        'payslipTemplateB' => $payslipTemplateB,
        'searchModel' => $searchModel,
    ]);
}

and index.php

<?php 
    $users = User::find()->where(['user_type' => 'BizStaff'])->andwhere(['parent' => new \MongoId($session['company_owner'])])->all();
    echo $this->render('_search', ['model' => $searchModel, 'users' => $users]); 
?>