Moeez
Moeez

Reputation: 478

Yii2- How to add a search box in grid view

I am new to Yii-2. I have a grid-view in my index page which some entries are displaying.

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

        [
            'label' => 'Meter MSN',
            'value' => function ($d) {
                return $d->meter->meter_msn;
            },
           // 'filter' => Html::activeDropDownList($searchModel, 'meter_id', \app\models\Meters::toArrayList(), ['prompt' => "All Meters", 'class' => 'form-control']),

        ],
        'imsi',
        'telecom',
        'status',
        [
            'label' => 'Created By',
            'value' => function ($data) {
                if (is_object($data))
                    return $data->created->name;
                return ' - ';
            },
            //'filter' => Html::activeDropDownList($searchModel, 'created_by', \app\models\User::toArrayList(), ['prompt' => "Created By", 'class' => 'form-control']),
        ],
        'comments',
        'historic',


        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

Now I want to add a search-box against Meter MSN. In above code the filter is hidden so it was working but I don't want to add a drop-down instead I want a search box.

Below is my search class

public function search($params)
{
    $query = MetersInventoryStore::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,
        'meter_id' => $this->meter_id,
        'created_by' => $this->created_by,
        'updated_by' => $this->updated_by,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'store_id' => $this->store_id,
        'meter_serial'=>$this->meter_serial,
        //            'historic' => $this->historic,
        'status'=>'SIM Installed',

    ])
      //  ->orFilterWhere(['status'=>'Communication Failed'])
    ;

    //       $query->andFilterWhere(['like', 'meter_serial', $this->meter_serial])
    //        ->andFilterWhere(['like','meter_id',$this->meter_id]);
    $query->orderBy(['id' => SORT_DESC]);
    return $dataProvider;
}

How can I place a search-box in it? As the simple search class will set up the search functionality by default. But my MSN value is coming from a function so I have no idea how can I place a search-box. Any help would be highly appreciated.

Upvotes: 0

Views: 4885

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

for add filter field in a calculated column you should add a pubblic var in in your search model

public function search($params)
{
    public $your_column;

   // declare as safe 
   public function rules()
   {
    return [
       ...
        [[  'your_column', ], 'safe'],

    ];
   }

   $query = MetersInventoryStore::find();

and then refer to your_column in grid_view

 ...
 'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    //'meter_id',

    [
        'attribute' => 'your_column',
        'label' => 'Meter MSN',
        'value' => function ($d) {
            return $d->meter->meter_msn;
        },
    ],

And last your searchModel you must expand your filter condition for manage properly your calculated column based on the filter value you passed.

You can find some sample in this tutorial http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

Upvotes: 4

Related Questions