Yii2中的主要搜索表单 [英] Main Search Form in Yii2

查看:105
本文介绍了Yii2中的主要搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Yii2中我真的很新,但我仍然不知道如何正确配置它。我注意到 GridView 在每列上都有搜索字段。我现在需要的是创建一个主/单搜索字段,其中用户可以输入关键字,然后结果将在按下搜索按钮后显示在 GridView 中。



这可能吗?我还在我的搜索表单字段中使用了这个Kartik小部件,该字段中有一个下拉列表。 此处图片



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



我也在SO里搜索了一些和我一样的问题,比如:

lockquote

不使用GridView的Yii2搜索模型



yii 2 ,创建一个活跃的表单文本字段主搜索字段


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



我会在这里包含我的动作控制器以防万一您需要。

  public function actionIndex()
{
$ session = Yii :: $ app-> session;
$ searchModel = new PayslipTemplateSearch(); ($'$'
$ b $ PayslipEmailConfig = PayslipEmailConfig :: find() - >其中(['company_id'=> new \MongoId($ session ['company_id'])]) - & ; $($'
$ b $ payslipTemplateA = PayslipTemplate :: find() - > where(['company_id'=> new \MongoId($ session ['company_id'])]) - & 'template_name'=>'A']) - > one(); $('session_ ['company_id'])]] - >和where((''template_name'= $'') >'B']) - > 1(); $($'$'
$ b $ 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);

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

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

我的观点, index.php

 <?php 
$ users = User :: find() - >其中(['user_type'=> 'BizStaff']) - >和where(['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为$ user){
$ listData [(string)$ user-> _id] = $ user-> employeeId。 ''。$ user-> fname。''。$ user-> lname;

$ b $ echo $ form->字段($ model,'_id') - >小部件(Select2 :: classname(),[
'data'=> ; $ listData,$ b $'addon'=> [
'append'=> [
'content'=> Html :: button('Search',['class'= >'btn btn-primary']),
'asButton'=> true
]
],
'options'=> ['class'=> 'dropdown-responsive','responsive'=> true,'placeholder'=>'搜索员工ID或姓名(例如10015或John Cruz)','id'=>'user_id','name'=> ;'id'],
'pluginOptions'=> [
'allowClear'=> true,
'responsive'=> true
],
]);
?>

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

真的需要帮助。

>解决方案

这个示例代码根据您的要求。所以,试试看。
$ b 用户SearchModel的 search()方法。

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

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

控制器 actionIndex

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

$ PayslipEmailConfig = PayslipEmailConfig :: find() - >其中(['company_id'=> new \ MongoId($ session ['company_id'])]] - >一(); $($'
$ b $ payslipTemplateA = PayslipTemplate :: find() - > where(['company_id'=> new \MongoId($ session ['company_id'])]) - & 'template_name'=>'A']) - > one(); $('session_ ['company_id'])]] - >和where((''template_name'= $'') >'B']) - > 1(); $($'$'
$ b $ 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->搜索(Yii :: $ app-> request-> queryParams);
/ *
$ dataProvider = User :: find() - >其中(['user_type'=>'BizStaff']) - >和where(['parent'=> new \\ \\MongoId($会话[ 'company_owner'])]) - >所有();
* /

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

index.php

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


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.

解决方案

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

这篇关于Yii2中的主要搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆