Brief analysis on the common operation of YII2 in Yii2gridview
This article is a small part of the collection to collect some information on the network of the GridView appeared on most of the problems, this article to do a summary to share to help guests home platform for your reference.
If the following is not mentioned in the GridView FAQ, below the message, I will add.
Drop-down search
Date formatting and implementation 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 Styles
Add button Invoke JS operation
Yii2 GridView drop-down search implementation Case tutorial
Yii2 GridView date formatting and implementation date searchable case
Whether to show a column of cases
Let's give a simple case
Condition: There is a get shape parameter type
Requirement: The column name is displayed only if the value of type is equal to 1, otherwise the column does not display
The code is implemented as follows:
[' attribute ' = ' + ' name ', ' value ' = = $model->name, ' Visible ' and Intval (Yii:: $app->request->get (' type ') ) = = 1,],
The way of implementation is also very simple drop.
Links can be clicked Jump case
This is very similar to what we are going to say about HTML rendering, here is the column's attribute value format, which format can be used to view the file yii\i18n\formatter.php, the 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 case
As above, just specify format as image, format second parameter can set the picture size, 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, for example, we have a field labeled title, but this title is not the same, TA contains HTML tags, we do not want to show on the page
Title123
This form, we want to title123 in the form of P tags, the code can refer to the following, only need to 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 an example to get the xxx button, how to do? Here you need to set the Actioncolumn class, modify the configuration item template and add the get-xxx in the buttons to the template.
[' class ' = ' yii\grid\actioncolumn ', ' template ' = ' {get-xxx} {view} {update} ', ' header ' = ' operation ', ' buttons ' = [' get-xxx ' = function ($url, $model, $key) {return html::a (' Get xxx ', $url, [' title ' = ' Get xxx ']);},],
Set width case
For a simple example, our title column, too long, can you give me the width of this column first?
Answer: Yes.
Take a look at the example:
[' attribute ' + ' title ', ' value ' = ' title ', ' headeroptions ' = ' [' width ' = ' 100 '],],
You only need to specify the configuration item headeroptions.
custom Field Cases
When is it customized? Here we mean adding a column to the table and not having a corresponding column in the database. If we add a new list of order consumption money and the field does not exist in the table
[' attribute ' + ' consumption amount ', ' value ' = = function ($model) {//This can be obtained based on other fields of the table}],
Custom row Styles
A small partner said, the GII generated the GridView table, line and line of color is not obvious, look uncomfortable, I drop obediently, specific how uncomfortable we will not be investigated. Let's see how to define the line 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, because this is the control of the row, so we configure rowoptions to a little attention. In addition, the 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 Invoke JS operation case
The product manager over there came, Xiao Wang, you the function of the modified state is very frequent, every time you have to point into the details page to modify, can I on the list page mouse so that a little bit on the successful modification?
is actually an asynchronous request to manipulate the state of the current row, we look at the GridView inside how to achieve.
[' 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 ' =&G t; ' 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, click Reference
Added: The features and techniques commonly used in the development of GridView widgets.
The data grid, or the GridView widget, is one of the most powerful parts of Yii.
It has a property called Dataprovider, which provides an example of a data provider and can display the data provided, 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, the table column
The columns of the table are configured through the Yii\grid\gridview::columns property in the GridView configuration item.
<?phpuse Yii\grid\gridview;echo gridview::widget ([' dataprovider ' = + $dataProvider,//Table column value search function, Note Be sure to match attribute to show//$searchModel = new Articlesearch (); ' Filtermodel ' + $searchModel,//redefine page style ' layout ' = ' {Items}{pager} ', ' Pager ' =>[//' Options ' =>[' class ' = > ' Hidden ']//close page ' Firstpagelabel ' + ' first ', ' prevpagelabel ' = ' Prev ', ' Nextpagelabel ' and ' Next ', ' Lastpagelabel ' + ' last ', ' columns ' and ' = ' [' class ' = ' Yii\grid\serialcolumn '],//serial number from 1//The simple column defined by the data contained in the data provider Using the data ' ID ' of the Model column, ' username ',///More complex column data [' class ' = ' yii\grid\datacolumn ',//because it is the default type, you can omit ' value ' = function ($ Data) {return $data->name;////If the array data is $data [' name '], for example, the case of using Sqldataprovider. },],[' label ' + ' title ', ' value ' = ' title '],[' label ' = ' article content ', ' format ' + ' html ', ' value ' = ' content '],[' The label ' = = ' article category ',/* ' attribute ' + ' CID ', produces a tag, click sortable */' value ' = ' cate.cname '//correlation 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 ' =&Gt ' Action ', ' template ' = ' {delete} {update} ',//only need to show delete and update ' headeroptions ' = [' width ' = ' + '], ' buttons ' and ' = ' Delete ' = function ($url, $model, $key) {return html::a ('Delete ', [' del ', ' ID ' + $key], [' class ' = ' btn btn-default btn-xs ', ' data ' = ' confirm ' and ' = ' Are you sure you want to delete the article? ',]]);}, ],],],]);? >
1. Processing time
The primary configuration item for the 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. Working with pictures
[' label ' = ' cover map ', ' format ' = ' raw ', ' Value ' =>function ($m) {return html::img ($m->cover,[' class ' = ') Img-circle ', ' width ' = 30]);}],
3. Data column with links
[' attribute ' = ' title ', ' Value ' = function ($model, $key, $index, $column) {return html::a ($model->title, [' Art Icle/view ', ' id ' = $key]);}, ' format ' = ' raw ',],
4. Data column Display enumeration values (male/female)
[' attribute ' = ' sex ', ' value ' =>function ($model, $key, $index, $column) {return $model->sex==1? ' Male ': ' Female '; },//Use the drop-down box in the search criteria (filter) to search for ' filter ' = [' 1 ' = ' man ', ' 0 ' = ' female '],//or ' filter ' and ' Html::activedropdownlist ' ($ Searchmodel, ' sex ', [' 1 ' = ' male ', ' 0 ' = ' ' female '],[' prompt ' = ' all '])],[' label ' = ' product status ', ' attribute ' and ' pro_ ' Name ', ' value ' = function ($model) {$state = [' 0 ' = ' not shipped ', ' 1 ' = ' shipped ', ' 9 ' = ' return ', processed ',];return $state [$mode L->pro_name];}, ' headeroptions ' = [' width ' = ' 120 ']]
Recommended reading for the Guest house:
Analysis on the common operation of the GridView in Yii2
Yii2 the bottom of the page loading CSS and JS tips
Brief analysis of Yii2 GridView date formatting and implementation date searchable tutorials
Brief analysis of YII2 GridView implementation drop-down search tutorial
The above content is for the Yii2 in the GridView common operation of all the introduction, I hope you have some help!
http://www.bkjia.com/PHPjc/1122885.html www.bkjia.com true http://www.bkjia.com/PHPjc/1122885.html techarticle A brief analysis of the common operation of the YII2 in the GridView, Analysis Yii2gridview This article is a small part of the collection to collect some information on the network of the GridView appeared on most of the problems, this article to do a summary to share to help ...