Main Search Form in Yii2

主宰稳场 提交于 2019-12-13 02:19:09

问题


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:


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]); 
?>


来源:https://stackoverflow.com/questions/32882097/main-search-form-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!