Yii2有一个searchModel来搜索GridView中的每个字段.是否可以在GridView外部创建一个用户可以输入关键字的单个搜索字段,当搜索按钮被点击时,结果将根据输入的关键字显示在GridView中.
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,
]);
}
}
它不是由于Kartik的Select2搜索下拉小部件中的一些复杂性.现在我暂时切换到简单的Yii2搜索字段.
视图
echo $form->field($model, ‘_id’)->textInput(array(‘placeholder’ => ‘search’))->label(false);
模型
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;
}
}
你对我如何实现单一搜索有任何想法吗?这是一种更智能的搜索,因为它可以根据输入的关键字搜索数据库表中的所有内容.
编辑
当我搜索一个关键字,例如’hello’时,它会在输入密钥后给我这个url和错误:
网址:
07001
错误信息:
Bad Request (#400) Missing required parameters: id
救命.
今天的文章php如何实现表单搜索,php – 如何在yii2中实现单个搜索表单分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/61809.html