0
votes

As you can see I've gridview (from model called umumiy). And via id_nomi I'm showing nomi.rus (which means rus column from nomi model):

enter image description here

The issue here is I'm trying to make search from Nomi model via umumiy gridview. I'm trying to get values (with nomi.rus) via ajax. This is what I tried:

    $model = new UmumiyModel();
    $searchModel = new UmumiyModelSearch();

    if (Yii::$app->request->isAjax){
        $data = Yii::$app->request->post();
        $searchModel->nomi->rus = $data['dori_nomi']; // search input value
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->renderPartial('sotish', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'model' => $model,
        ]);
    }

What am I doing wrong???

1

1 Answers

1
votes

You can use a public member in NomiSearch model to store text value from "Id Nomi" input field of gridview.

So, in NomiSearch model:

class NomiSearch extends Nomi
{
    public $nomiText;

    public function rules()
    {
        return [
            // ...
            [['nomiText'], 'safe'],
        ];
    }

    public function search($params)
    {
        $query = Nomi::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([
        ]);

        if($this->nomiText!=null)
        {
            $query->andWhere(['IN', 'id_nomi', (new \yii\db\Query())->select('id')->from('nomi')->where(['like', 'nomi', $this->nomiText])]);
        }   

        return $dataProvider;
    }

}

Finally, in index view:

GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'label' => 'Id Nomi',
                'attribute' => 'nomiText',
                'value' => function($data) {
                    return $data->nomi->rus;
                },

            ],