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;
}
}
你有關於如何我實現一個單一的搜索任何想法?這是一種更智能的搜索,因爲它可以根據輸入的關鍵字搜索數據庫表中的所有內容。
編輯
當我搜索一個關鍵字,例如說 '你好',然後,它給了我這個網址和錯誤打回車鍵後:
網址:
錯誤消息:
錯誤請求(#400)缺少所需參數:編號
幫助。
您已在'payslip-template/searchresults'動作中傳遞'id'作爲參數? – GAMITG
你有沒有在_search.php中使用'searchresults'作爲'ActiveForm'的動作。如果你在_search.php中使用'searchresults'作爲表單的動作,那麼它就會被'index'動作取代。 – GAMITG
我的網址有些奇怪:'/index.php?r=payslip-template/ searchresults&PayslipTemplateSearch [_id] = samplekeyword' –