2015-10-02 45 views
7

Yii2有一個searchModel來搜索GridView中的每個字段。是否可以在GridView之外創建一個用戶可以輸入關鍵字的單個搜索字段,並且在搜索按鈕被點擊時,結果將根據輸入的關鍵字在GridView中顯示。如何在yii2中實現單個搜索表單

控制器

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

它沒有因卡爾蒂克的Select2搜索下拉列表控件的一些併發症。現在我暫時切換到一個簡單的Yii2搜索字段。

VIEW

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

模型

<?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; 
    } 
} 

你有關於如何我實現一個單一的搜索任何想法?這是一種更智能的搜索,因爲它可以根據輸入的關鍵字搜索數據庫表中的所有內容。

編輯

當我搜索一個關鍵字,例如說 '你好',然後,它給了我這個網址和錯誤打回車鍵後:

網址:

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

錯誤消息:

錯誤請求(#400)缺少所需參數:編號

幫助。

+1

您已在'payslip-template/searchresults'動作中傳遞'id'作爲參數? – GAMITG

+0

你有沒有在_search.php中使用'searchresults'作爲'ActiveForm'的動作。如果你在_search.php中使用'searchresults'作爲表單的動作,那麼它就會被'index'動作取代。 – GAMITG

+0

我的網址有些奇怪:'/index.php?r=payslip-template/ searchresults&PayslipTemplateSearch [_id] = samplekeyword' –

回答

5

我有同樣的問題,我的解決辦法是:

型號

與搜索參數

class UserSearch extends User 
{ 
    public $searchstring; 
    ... 

啓用通過擴展您的UserSearch模型中的變量

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

更改您的搜索結果h-Method(注意:搜索字段與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, 
    ]); 
} 

就是這樣。

相關問題