Yii2-GridView in the development of common functions and techniques, yii2-gridview skills
A data grid or a GridView widget is one of the most powerful components in Yii. It has a property named dataProvider, which provides an example of a data provider and can display the provided data, that is, using yii \ grid \ GridView :: A group of column configurations in the columns attribute, rendering each row of data in a table.
For example,
use yii\helpers\Html;use yii\grid\GridView; <?= GridView::widget(['dataProvider' => $dataProvider,'filterModel' => $searchModel,'columns' => [['class' => 'yii\grid\SerialColumn'],'id',]);?>
I. table columns
The columns in the table are configured using the yii \ grid \ GridView: columns attribute in the GridView configuration item.
<? Phpuse yii \ grid \ GridView; echo GridView: widget (['dataprovider' => $ dataProvider, // table column value search function, be sure to use the attribute to display/$ searchModel = new ArticleSearch (); 'filtermodel' => $ searchModel, // redefine the paging style 'layout '=>' {items} <div class = "text-right tooltip-demo"> {pager} </div> ', 'pager' => [// 'options' => ['class' => 'hiddy'] // close the pagination 'firstpagelabel '=> "First ", 'prevpagelabel '=> 'prev', 'nextpagelabel' => 'Next', 'lastpag ELabel '=> 'last',] 'columns' => [['class' => 'yii \ grid \ SerialColumn '], // The serial number increases from 1. // The simple column defined by the data contained in the data provider. // the data of the column in the model is 'id', 'username ', // more complex column data ['class' => 'yii \ grid \ datacolumn', // because it is of the default type, you can omit 'value' => function ($ data) {return $ data-> name; // $ data ['name'] For array data, for example, SqlDataProvider. },], ['Label' => 'title', 'value' => 'title'], ['label' => 'Article content ', 'format' => 'html', 'value' => 'content'], ['label' => 'Article class ', /* 'attribute' => 'cid' to generate a tag. Click ** sort **/'value' => 'cate. cname' // associated table], [// action column yii \ grid \ ActionColumn // is used to display some action buttons, such as update and delete operations for each row. 'Class' => 'yii \ grid \ actioncolumn', 'header' => 'operation', 'template' => '{delete} {update }', // you only need to display the deletion and update 'headeroptions' => ['width' => '000000'], 'buttons' => ['delete' => function ($ url, $ model, $ key) {return Html: a ('<I class = "fa-ban"> </I> delete', ['del ', 'id' => $ key], ['class' => 'btn btn-default btn-xs ', 'data' => ['Confirm' => 'Are you sure you want to delete the article? ',]) ;},],]);?>
1. Processing Time
The main configuration item of the data column is the yii \ grid \ DataColumn: format attribute. The default value is \ yii \ i18n \ Formatter application component.
['Label' => 'Update date', 'format' => ['date', 'php: Y-m-d'], 'value' => 'updated _ at'], // or [// 'attribute' => 'created _ at', 'label' => 'Update time ', 'value' => function ($ model) {return date ('Y-m-d H: I: s', $ model-> created_at );}, 'headeroptions' => ['width' => '2013'],],
2. process images
['Label' => 'album art ', 'format' => 'Raw', 'value' => function ($ m) {return Html :: img ($ m-> cover, ['class' => 'img-circle', 'width' => 30]);}],
3. Data columns have links
[ 'attribute' => 'title', 'value' => function ($model, $key, $index, $column) { return Html::a($model->title, ['article/view', 'id' => $key]); }, 'format' => 'raw',],
4. Enumeration values in the data column (male/female)
['Attribute' => 'sex', 'value' => function ($ model, $ key, $ index, $ column) {return $ model-> sex = 1? 'Male': 'female ';}, // use the drop-down box in the Search Condition (filter condition) to search for 'filter' => ['1' => 'male ', '0' => 'female '], // or 'filter' => Html: activeDropDownList ($ searchModel, 'sex', ['1' => 'male ', '0' => 'female '], ['propt' => 'all'])], ['label' => 'product status ', 'attribute' => 'Pro _ name', 'value' => function ($ model) {$ state = ['0' => 'undeliverable ', '1' => 'deliverable ', '9' => 'Return, handled',]; return $ state [$ model-> pro_name];}, 'headeroptions' => ['width' => '123']
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.