Answer the question
In order to leave comments, you need to log in
How to setup filter in yii2?
Hi friends. I came across a project written in yii2 by a programmer who did not leave a single comment in the code. Plus, I have never worked with yii2 projects before.
I'll get down to business. The task is this: you need to make (or rather complete) a filter by name, which are respectively registered in the Mysql database table. I found a filter script:
...
->andFilterWhere(['like', 'documents.contract_number', $this->contractNumber])
->andFilterWhere(['like', 'companies.request_number', $this->requestNumber])
->andFilterWhere(['program.type' => $this->service])
->andFilterWhere(['manager_id' => $this->manager_id])
->andFilterWhere(['work_status' => $this->work_status])
->andFilterWhere(['companies.came_from' => $this->cameFrom])
->andFilterWhere(['inn' => $this->inn])
...
->andFilterWhere(['like','program.name' , $this->mainProgram])
Answer the question
In order to leave comments, you need to log in
Good afternoon.
You need to add a little search model.
Add a public property to this model. This property will then be displayed in the gridView and filtered by it.
Approximately like this:
public $program_name_id; // необходимо добавить в правила rules(), например указать, что это свойство integer
public function rules()
{
return [
['program_name_id', 'integer']
];
}
// и добавить в фильтры
->andFilterWhere(['program.id' => $this->program_name_id])
'columns' => [
[
'attribute' => 'program_name_id',
'value' => 'program.name'
]
]
'columns' => [
[
'attribute' => 'program_name_id',
'filter' => Program::getAllProgram()
'value' => 'program.name'
]
]
use yii\helper\ArrayHelper;
public static function getAllProgram()
{
/**
* в запрос self::find()->all() можно добавить условие выборки, сортировки, группировки и т.д. и т.п.
*/
return ArrayHelper::map(self::find->all(), 'id', 'name');
}
public $program_name; // необходимо добавить в правила rules(), например указать, что это свойство string
public function rules()
{
return [
['program_name', 'string']
];
}
// и добавить в фильтры
->andFilterWhere(['like','program.name' , $this->program_name])
'columns' => [
[
'attribute' => 'program_name',
'value' => 'program.name'
]
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question