如何在yii2中实现单个搜索表单

时间:2015-10-02 03:53:21

标签: php search gridview yii2

Yii2有一个searchModel来搜索GridView中的每个字段。是否可以在GridView之外创建一个用户可以输入关键字的单个搜索字段,并且在点击搜索按钮时,结果将根据输入的关键字显示在GridView中。< / p>

CONTROLLER

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();
    $searchModel = new UserSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'PayslipEmailConfig' => $PayslipEmailConfig,
        'dataProvider' => $dataProvider,
        'payslipTemplateA' => $payslipTemplateA,
        'payslipTemplateB' => $payslipTemplateB,
        'searchModel' => $searchModel,
    ]);
}
public function actionSearchresults($keyword)
{
    $session = Yii::$app->session;
    if ( $keyword == '') {
        return $this->redirect(\Yii::$app->request->getReferrer());
    } else {
        $user = User::find()->where( [ '_id' => new \MongoId($id) ] )->one(); 
        $searchModel = new PayslipTemplateSearch();

        $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();

        return $this->render('searchresults', [
            'searchModel' => $searchModel,
            'user' => $user,
            'payslipTemplateA' => $payslipTemplateA,
            'payslipTemplateB' => $payslipTemplateB,
        ]);
    }    
}

我在这里问了一个与此问题相关的问题:Main Search Form in Yii2

由于Kartik的Select2搜索下拉窗口小部件中存在一些复杂情况,因此无法解决问题。现在我暂时切换到简单的Yii2搜索字段。

查看

echo $form->field($model, '_id')->textInput(array('placeholder' => 'search'))->label(false);

MODEL

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\User;

/**
 * UserSearch represents the model behind the search form about `app\models\User`.
 */
class UserSearch extends User
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[/*'_id',*/ 'creator_id'], 'integer'],
            [['fname', 'lname', 'email', 'username', 'user_type'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $session = Yii::$app->session;

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

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

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            '_id' => $this->_id,
            'creator_id' => $this->creator_id,
        ]);

        $query->andFilterWhere(['like', 'fname', $this->fname])
            ->andFilterWhere(['like', 'lname', $this->lname])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'user_type', $this->user_type]);

        return $dataProvider;
    }
}

您对如何实施单一搜索有任何想法吗?这是一种更智能的搜索,因为它可以根据输入的关键字搜索数据库表中的所有内容。

修改

当我搜索某个关键字时,例如&#39; hello&#39;,然后在按下回车键后给我这个网址和错误:

网址:

  

http://localhost/iaoy-dev/web/index.php?r=payslip-template%2Fsearchresults&PayslipTemplateSearch%5B_id%5D=hello

错误讯息:

  

错误请求(#400)缺少必需参数:id

帮助。

1 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,我的解决方案是:

<强>模型

使用搜索参数

扩展您的UserSearch模型
class UserSearch extends User
{
    public $searchstring;
    ...

启用传递变量

    public function rules()
    {
        return [
            ...
            [['searchstring'], 'safe'],
        ];
    }

更改您的搜索方法(注意:搜索字段与orFilterWhere结合,取决于您的需求)。

     $query->orFilterWhere(['like', 'fname', $this->searchstring])
        ->orFilterWhere(['like', 'lname', $this->searchstring])
        ->orFilterWhere(['like', 'email', $this->searchstring])
        ->orFilterWhere(['like', 'username', $this->searchstring])
        ->orFilterWhere(['like', 'user_type', $this->searchstring]);

查看(也可以是布局)

使用搜索输入扩展您的表单。您可以自己设置输入字段的样式,这只是一个示例:

<?php
/* @var $searchModel app\models\UserSearch */
echo $form->field($searchModel, 'searchstring', [
        'template' => '<div class="input-group">{input}<span class="input-group-btn">' .
        Html::submitButton('GO', ['class' => 'btn btn-default']) .
        '</span></div>',
    ])->textInput(['placeholder' => 'Search']);
?>

<强>控制器

在发布表单后,还要检查$searchstring的值。

public function actionIndex()
{
    ...
    $searchModel = new UserSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    ...
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

就是这样。