1
votes

Hi i have a yii code in which i can able to display the data from the model but the search bar was not comming in the gridview, please help.

Gridview code :

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

           // 'emp_attendance_pid',
            [

                    'label' => 'Employee ID',
                    'attribute' => 'emp_history_id',
                    'value' => 'emp_history_id',
            ],
            [
                  'label' => Yii::t('app','First Name'),
                 // 'attribute' => 'emp_history_id',
                  'value' => 'empHistory.emp_first_name',
                'filter' => ArrayHelper::map(app\modules\employee\models\EmpInfo::find()->all(), 'emp_info_id', 'emp_first_name')
            ],
            [
                  'label' => Yii::t('app','Last Name'),
                 // 'attribute' => 'emp_history_id',
                  'value' => 'empHistory.emp_last_name',
                'filter' => ArrayHelper::map(app\modules\employee\models\EmpInfo::find()->all(), 'emp_info_id', 'emp_last_name')
            ],

Here you can see the filter, in that i am able to get the data using the arrayhelper but i need to get data which i am giving in the search box, the search box was also not coming, only drop down is comming

1
Please show your related controller/action ..and the related ModelSearch .. - ScaisEdge
$query = EmpHistory::find(); $dataProvider = new ActiveDataProvider([ 'query' => $query,]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['emp_history_pid' =>$this->emp_history_pid,'emp_history_id' => $this->emp_history_id,'emp_history_date' => $this->dbDateSearch($this->emp_history_date), ]); $query->andFilterWhere(['like', 'emp_history_process', $this->emp_history_process]) ->andFilterWhere(['like', 'emp_history_remarks', $this->emp_history_remarks]); - Paventhan
this is the search action I have - Paventhan
Please edit your question and add the code properly formatted... add also the controller action code .. - ScaisEdge

1 Answers

1
votes

Parameter attribute is required for filtering.

If you define filter property, you will have a dropdown list. As I understood, you want text input, thus you don't need filter.

If you want to search via related table, you should create public properties in SearchModel and create validation rules. As i see from your example, you want to filter data from related table;

Here is a little example:

Setup search model

 public $emp_first_name;

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

 public function search($params) 
 {
     $query = Person::find()->joinWith(['empHistory'])->groupBy(Person::tableName().'.id');

     $dataProvider = new ActiveDataProvider([
         'query' => $query,
     ]);

     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }

     $query->andFilterWhere(['like', EmpInfo::tableName().'.emp_first_name', $this->emp_first_name]);

     return $dataProvider;
}

In View:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'emp_history_id',
        [
            'attribute' => 'emp_first_name',
            'value' => 'empHistory.emp_first_name',
        ],
        ['class' => 'yii\grid\ActionColumn'],
    ]
]); ?>