This article is a small series to gather and organize some of the network on the GridView appeared most of the problems, this article to do a summary is hereby share to the cloud Habitat Community Platform for your reference.
If there is a GridView FAQ below, I will add the following message.
Pull down Search
Date formatted and implemented date searchable
Whether to display according to the parameters
Links can be clicked to jump
Show pictures
HTML rendering
Custom Buttons
Set width and other styles
Custom Fields
Custom row Style
Add button to call JS operation
Yii2 GridView dropdown Search Implementation Case Tutorial
YII2 GridView date format and implement date searchable case
Whether to show a column case
Let's give you a simple case.
Condition: There is a get-shape parameter type
Requirement: Column name is displayed only if the value of type is equal to 1, otherwise the column is not displayed
The code implementation is as follows:
[
' attribute ' => ' name ',
' value ' => $model->name,
' Visible ' => intval (Yii:: $app->request- >get (' type ')) = = 1,
],
Implementation is also very simple drop.
Link clickable Jump case
This is similar to the next HTML rendering we want to say, here is the column's property value format, which is the format to view the file yii\i18n\formatter.php, various format can be resolved
[
' attribute ' => ' order_id ', '
value ' => function ($model) {return
html::a ($model->order_id, "/ order?id={$model->order_id} ", [' Target ' => ' _blank ']);
},
' format ' => ' raw ',
],
Show Picture Cases
Ditto, here you only need to specify format format as image, format the second parameter can be set picture size, you can refer to the following code
['
label ' => ' Avatar ',
' format ' => [
' image ',
[
' width ' => '],
' height ' => ' ']
,
' value ' => function ($model) {return
$model->image;
}
],
HTML Rendering case
What do you mean Nan, for example, we have a field labeled title, but this title is different, TA contains HTML tags, we don't want to show <p>title123<p> this form on the page, We want title123 to be shown as P tag, the code can refer to the following, just specify format as raw form
[
' attribute ' => ' title ',
' value ' => function ($model) {return
Html::encode ($model->title);
} ,
' format ' => ' raw ',
],
Custom button Case
Often list page We do not want to delete the button, want to add one such as get the xxx button, how to do it? Here you need to set the Actioncolumn class, modify the configuration item template and add Get-xxx to the buttons item template
[
' class ' => ' Yii\grid\actioncolumn ', '
template ' => ' {get-xxx} {view} {update} ',
' header ' => ' Operations ',
' buttons ' => [
' get-xxx ' => function ($url, $model, $key) {return
html::a (' Get xxx ', $url, [' Tit Le ' => ' get xxx '];
},
]
,
Set width case
For a simple example, our title column is too long, can you give me the width of this column first?
Answer: Can.
Take a look at the example:
[
' attribute ' => ' title ', '
value ' => ' title ',
' headeroptions ' => [' width ' => '],
]
You only need to specify the configuration item headeroptions.
custom Field Case
When is customization? Here we mean adding a column to the table and there is no corresponding column in the database. If we add a list of order consumption money and the table does not exist in this field
['
attribute ' => ' consumption amount ',
' value ' => function ($model) {
//here can be associated with other fields of the table
}
],
Custom row Style
A small partner said, the GridView table generated in GII, the line with the color is not obvious, look uncomfortable, I drop obediently, concrete how uncomfortable we will not be prosecuted. Let's see how to define a row style
<?= Gridview::widget ([
//...
] Dataprovider ' => $dataProvider,
' rowoptions ' => function ($model, $key, $index, $grid) {return
[' class ' => $index% 2 ==0? ' label-red ': ' Label-green '];
},
//...
?>
The previous operation we are all based on column columns, because this is the control of the row, so we configure Rowoptions to pay a little attention. In addition, custom label-red and Label-green need to have a corresponding style implementation, here we look at the actual effect of the page
Add button to call JS operation case
The product manager over there came, Xiao Wang Ah, you are the modification of the status of the function is very frequent, each time you have to point to the details page before you can modify, can I on the list page on the mouse so a little succeeded to modify it?
The fact is that an asynchronous request operates the current line state, and we look at how it is implemented in the GridView.
[
' class ' => ' Yii\grid\actioncolumn ', '
header ' => ' operation ',
' template ' => ' {view} {update} { Update-status} ',
' buttons ' => [
' Update-status ' => function ($url, $model, $key) {return
html::a (' Update status ', ' javascript:; ', [' onclick ' => ' update_status (This, '. $model->id. '); },
],
],
We need to write in the page JS implementation method Update_status, about how to load JS at the bottom of the page please click the reference
Supplement: The features and techniques commonly used in the development of the GridView widget.
The data grid, or the GridView widget, is one of the most powerful parts of Yii.
It has a property named Dataprovider, which provides an example of a data provider and can display the provided data, that is, a set of column configurations using the Yii\grid\gridview::columns property to render each row of data in a table.
For example
Use Yii\grid\gridview;
Echo yii\grid\gridview::widget ([
' Dataprovider ' => $dataProvider,
]);
One, table column
The columns of the table are configured through the Yii\grid\gridview::columns property in the GridView configuration item.
<?php use Yii\grid\gridview; Echo gridview::widget ([' Dataprovider ' => $dataProvider,//Table column value search function, note must match attribute to show//$searchModel = new
Articlesearch (); ' Filtermodel ' => $searchModel,//redefine pagination style ' layout ' => ' {items}<div class= ' text-right tooltip-demo ' >{pager} </div> ', ' pager ' =>[//' Options ' =>[' class ' => ' hidden ']//' Turn off pagination ' Firstpagelabel ' => ' a ' Prevpagelabel ' => ' Prev ', ' Nextpagelabel ' => ' Next ', ' Lastpagelabel ' => ' last ',] ' columns ' => [[' Class ' => ' The Yii\grid\serialcolumn '],//serial number is defined as a simple column//using the data ' ID ' of the column of the model, ' username ',///More complex column data [' Class ' => '] from 1 self-growth/data Provider
Grid\datacolumn ',//because it is the default type, you can omit the ' value ' => function ($data) {return $data->name;
If the array data is the $data [' name '], for example, the case using Sqldataprovider. },], [' label ' => ' title ', ' Value ' => ' title '], [' label ' => ' article content ', ' format ' => ' HTML ', ' value ' => ' content '], [' LA Bel ' => ' article category ',/* ' attribute ' => ' CID ', produces a label, click sortable/' value ' => ' cate.cname '/associated table], [//action row 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} ',//only need to show deletion and update ' headeropt Ions ' => [' width ' => '], ' buttons ' => [' delete ' => function ($url, $model, $key) {return html::a (' <i cl ass= "FA fa-ban" ></i> delete ', [' del ', ' ID ' => $key], [' Class ' => ' btn btn-default btn-xs ', ' Data ' => [' con Firm ' => ' Are you sure you want to delete the article?
',]
]
); },],],],?>
1. Processing time
The primary configuration item for a data column is the Yii\grid\datacolumn::format property.
Its value defaults to using the \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 ' => ' 170 '],
],
2. Processing pictures
[
' label ' => ' cover map ', '
format ' => ' raw ', '
value ' =>function ($m) {return
html::img ($m-> Cover,
[' Class ' => ' img-circle ', '
width ' =>]]
,
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. Data Column Display enumeration value (male/female)
[
' attribute ' => ' sex ', '
value ' =>function ($model, $key, $index, $column) {return
$model->sex ==1? ' Male ': ' Female '
,
//In search condition (filter condition) Use dropdown box to search
' filter ' => [' 1 ' => ' man ', ' 0 ' => '],
//or
' Filter ' => html::activedropdownlist ($searchModel,
' sex ', [' 1 ' => ' male ', ' 0 ' => ' female '],
[' Prompt ' => '] All ']
],
[
' label ' => ' product status ',
' attribute ' => ' pro_name ', '
value ' => function ($model) {
$state = [
' 0 ' => ' not shipped ', '
1 ' => ' shipped ', '
9 ' => ' returned, processed ',
];
return $state [$model->pro_name];
},
' headeroptions ' => [' width ' => ']
]
Recommended readings for cloud-dwelling communities:
Simple analysis of common operation of GridView in Yii2
Yii2 the bottom of the page load CSS and JS tips
Brief analysis of YII2 GridView date format and date to search tutorials
A brief analysis of YII2 GridView implementation Drop-down Search Tutorial
The above content is for YII2 in the common operation of the GridView all introduction, hope to help everyone!