Yii2中的主要搜索表单

时间:2015-10-01 07:45:34

标签: php gridview yii2

我在Yii2中真的很新,我仍然不知道如何正确配置它。我注意到GridView在每列上都有搜索字段。我现在需要的是创建一个主/单个搜索字段,其中用户可以输入关键字,然后在点击搜索按钮后,结果将显示在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, ]); }

这可能吗?我还在我的搜索表单字段中使用了这个Kartik小部件,其中包含一个下拉列表。 Image here

我们被告知要使用此下拉搜索,当用户输入一些关键字时(有时返回'未找到结果'在下拉列表中),并点击搜索按钮,页面将刷新根据输入的关键字显示所有结果。

我也在SO中搜索了与我相同的一些问题,例如:

  

Yii2 Search model without using GridView

     

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

我发现没有运气。第二个链接没有任何答案。

我会在这里包含我的动作控制器以备不时之需。

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

我的观点, index.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(); ?>

_search.php

{{1}}

真的需要帮助。

1 个答案:

答案 0 :(得分:1)

此示例代码根据您的要求。所以,试试吧。

用户搜索模型的search()方法。

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&#39; 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,
    ]);
}

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