Yii2 gridview 从多值列表中筛选(不是下拉列表筛选)
Yii2 gridview filter from a list of multiple value (not dropdown list filter)
我的 gridview 数据
day modeler total
2018-1-05 ABC 5
2018-1-06 DEF 8
2018-1-06 CAB 3
2018-1-06 GHI 3
2018-1-06 KLM 3
我有一个这样的网格视图。现在只能一一筛选建模师。我可以有一个多行搜索框并粘贴到 "ABC DEF CAB" 中,它会像下面这样过滤 3 个结果吗?
day modeler total
2018-1-05 ABC 5
2018-1-06 DEF 8
2018-1-06 CAB 3
我的控制器
public function actionIndex()
{
$searchModel = new ModelerSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
我的搜索模型
public function search($params)
{
$query = Modeler::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'modeler', $this->modeler])
->andFilterWhere(['like', 'total', $this->total]);
return $dataProvider;
}
}
谢谢!
您需要通过 gridview 中的 modeler
字段进行搜索,并且您希望它的工作方式是,如果您输入 "ABC"
,它应该会显示匹配的记录,如果您输入 "ABC DEF"
和 space 它应该会显示 2 条记录匹配其中之一。
所以,首先,在您的搜索模型之上添加
private $_selections = [];
然后更新你的搜索功能如下
public function search( $params ) {
$query = Modeler::find ();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider ( [
'query' => $query ,
] );
$this->load ( $params );
if ( !$this->validate () ) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if ( $this->modeler !== null && $this->modeler !== '' ) {
$this->_selections = preg_split ( '/\s+/i' , $this->modeler );
$query->andFilterWhere ( [ 'IN' , 'modeler' , $this->_selections ] );
}
// grid filtering conditions
$query->andFilterWhere ( [
'id' => $this->id ,
] );
$query->andFilterWhere ( [ 'like' , 'total' , $this->total ] );
return $dataProvider;
}
现在转到您的 gridview 并在 modeler
列中输入过滤器输入 "ABC DEF"
并观察它发生。
我的 gridview 数据
day modeler total
2018-1-05 ABC 5
2018-1-06 DEF 8
2018-1-06 CAB 3
2018-1-06 GHI 3
2018-1-06 KLM 3
我有一个这样的网格视图。现在只能一一筛选建模师。我可以有一个多行搜索框并粘贴到 "ABC DEF CAB" 中,它会像下面这样过滤 3 个结果吗?
day modeler total
2018-1-05 ABC 5
2018-1-06 DEF 8
2018-1-06 CAB 3
我的控制器
public function actionIndex()
{
$searchModel = new ModelerSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
我的搜索模型
public function search($params)
{
$query = Modeler::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'modeler', $this->modeler])
->andFilterWhere(['like', 'total', $this->total]);
return $dataProvider;
}
}
谢谢!
您需要通过 gridview 中的 modeler
字段进行搜索,并且您希望它的工作方式是,如果您输入 "ABC"
,它应该会显示匹配的记录,如果您输入 "ABC DEF"
和 space 它应该会显示 2 条记录匹配其中之一。
所以,首先,在您的搜索模型之上添加
private $_selections = [];
然后更新你的搜索功能如下
public function search( $params ) {
$query = Modeler::find ();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider ( [
'query' => $query ,
] );
$this->load ( $params );
if ( !$this->validate () ) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if ( $this->modeler !== null && $this->modeler !== '' ) {
$this->_selections = preg_split ( '/\s+/i' , $this->modeler );
$query->andFilterWhere ( [ 'IN' , 'modeler' , $this->_selections ] );
}
// grid filtering conditions
$query->andFilterWhere ( [
'id' => $this->id ,
] );
$query->andFilterWhere ( [ 'like' , 'total' , $this->total ] );
return $dataProvider;
}
现在转到您的 gridview 并在 modeler
列中输入过滤器输入 "ABC DEF"
并观察它发生。