I'm really new to Yii2, and I still don't know how to set it up correctly. I noticed that the GridView has search fields for each column. Now I need to create a main / single search field in which the user can enter keywords, then the results will be displayed in the GridView after clicking the search button.
Is it possible? I also used this Kartik widget in my search form field, which has a drop-down list. The image is here .
We are told to use this drop-down search, and when the user enters some keywords (sometimes returns “No results found” in the drop-down list), and clicks the “Search” button, the page will refresh, displaying all the results based on the entered keywords.
I also looked for some problems similar to mine here in SO, for example:
Yii2 Model Search Without GridView
yii 2, make the main search field in the text field of the active form
I was not lucky. The second link has no answers.
I will include my action controller here if 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 (eg 10015 or John Cruz)', 'id' => 'user_id', 'name' => 'id'], 'pluginOptions' => [ 'allowClear' => true, 'responsive' => true ], ]); ?> <?php ActiveForm::end(); ?>
Actually need help for this.