Skip to main content

Posts

Showing posts from 2015

Yii2 Gridview bulk action using checkbox column. How to properly create checkbox column in gridview for bulk actions

To add a CheckboxColumn to the  yii\grid\GridView , add it to the  columns  configuration as follows: 'columns'  => [      // ...      [          'class'  =>  'yii\grid\CheckboxColumn' ,          // you may configure additional properties here      ], ] This is the view: <?= Html :: beginForm ([ 'controller/bulk' ], 'post' );?> <?= Html :: dropDownList ( 'action' , '' ,[ '' => 'Mark selected as: ' , 'c' => 'Confirmed' , 'nc' => 'No Confirmed' ],[ 'class' => 'dropdown' ,])?> <?= Html :: submitButton ( 'Send' , [ 'class' => 'btn btn-info' ,]);?> <?= GridView :: widget ([ 'dataProvider' => $dataProvider , 'columns' => [ [ 'class' => 'yii\grid\CheckboxColumn' ], 'id' , ], ]); ?> <?= Html :: endForm ();?> This is

Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView, Gridview with relationships

One of the things you will find tricky to implement is the the sorting and filtering of a GridView's column that displays related model data. As you know if you have been playing with Yii2 lately, there is a new proposed way to search for data and is by using objects that extend from the main entity models and mark the searchable attributes as "safe". So, how do we sort and filter related data on a GridView widget? Lets imagine we have the following relations of a model named "Tour": /* * * @return \yii\db\ActiveQuery */ public function getCountry ( ) { return $this -> hasOne ( Country :: className ( ) , [ ' id ' => ' country_id ' ] ) ; } /* * * @return \yii\db\ActiveQuery */ public function getCity ( ) { return $this -> hasOne ( City :: className ( ) , [ ' id ' => ' city_id ' ] ) ; } And we wish to display the name of the country and the name of the city on a GridView. To do