Yii2 Tips 26 : All About Gridview

This is my personal notes on Gridview and their tips.

First, I have gridview table with the following structure and content :

yii2 tips gridview

yii2 tips gridview

Next step is to create the GridviewModel model and the CRUD using Gii.

A. Gridview Tips : Dropdown Search using Kartik Gridview

The end result will be like this, there is dropdown list on status_active, if active is selected, gridview will search the active ones, and if inactive is selected, gridview will search only the inactive ones.

Step 1 : Create function getActiveStatus and getStatusActiveList on GridviewModel

Step 2 : Install kartik Gridview

Step 3 : Add gridview modules to common/config/main.php

Step 4 : Modify views/gridview-model/index.php

The end result :

yii2 tips gridview

B. Gridview Tips : Date Search

We are going to search by date, the end result is when the date is selected, only record with the exact date is shown.

Step 1 : The GridviewSearch.php

Step 2 : Modify index.php

Don’t forget to add :

The result :

yii2 tips gridview

C. How to align the Header and Content of a Gridview, and give HTML styling to the content

Use attribute, headerOptions, contentOptions, and value.

The result would be like this :

yii2 tips gridview

D. Adding Preview File Button on the Gridview Content

In this example, class ‘lihat’ using jquery colorbox.

The result :

yii2 tips gridview

E. Adding Button to ActionColumn

In this case, the ‘ambil’ and ‘proses’ button are correspond to actionAmbil, and actionProses, respectively.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.