Yii2 related learning record, GridView widget usage and kartik-v/yii2-grid extension (v), yii2gridview_php tutorial

Source: Internet
Author: User
Tags php language yii

Yii2 related learning record, GridView widget usage and kartik-v/yii2-grid extension (v), Yii2gridview


Now record the relevant contents of the GridView, is also obsessive-compulsive disorder, Yii2 the GridView although good, but the filter bar if some fields do not need, will not automatically merge into a row, of course, you can filter the bar instead, but at the top of their own to write some data to retrieve, but it is very troublesome, You have to plan your own style, write a search or something. Exactly when the search will retrieve the column and the title merge, see the kartik-v/yii2-grid can implement these functions, but also comes with the export of Excel, CSV and other data, so also by the way to try its function, call out their favorite style. Before using the kartik-v GridView, we still need to understand the function of the GridView Yii2, which is generally used as follows:

1, the YII default has four kinds of style columns, respectively for Data column (default), Action column (action), Checkbox column (selectable), Serial column (with serial number)

2. The title name will be automatically replaced with the corresponding Chinese according to the Attributelabels () method in the model. Of course, it can be defined by ' label ', while ' attribute ' specifies which field to sort by. The other looks like ' visible ' to hide the display, ' header ' shows the header content, ' headeroptions ' to define CSS or style styles, and so on.

[    ' label ' = ' You want the name ',    ' attribute ' + ' id ',// field can be sorted ]

3. Custom display pages and sort fields, if Searchmodel is used, it needs to be defined in the Searchmodel search method, otherwise it needs to be defined in the controller:

$dataProvider New Activedataprovider ([    $query,    ' pagination ' = =        [' pageSize ' + = 15 ,// if not defined, by default,    ' sort ' = = [' Attributes ' = ' + ' [' ID ']],// if defined, Can only be sorted by ID, otherwise all fields will be ]);

4, drop-down menu retrieval, like according to the drop-down menu to retrieve the user state, you need to first define the relevant method in the model, and then in the GridView processing:

//model, define a static methodConststatus_deleted = 0;ConstStatus_active = 10; Public Static functionGetzhstatus ($status=false){    $status _array= [        ' = ' and ' Please select ', Self::status_deleted=> ' Forbidden ', Self::status_active=> ' normal '    ]; return $status==false?$status _array: Arrayhelper::getvalue ($status _array,$status, ' unknown ');} //in the GridView[     ' attribute ' = ' status ', ' Filter ' =>html::activedropdownlist ($searchModel, ' status ', User::getzhstatus (), [' class ' = ' Form-control ']), ' value ' =function($data){        returnUser::getzhstatus ($data-status); },],

5, format time, there are several ways:

Method One: Similar to the above definition callback function

[    ' attribute ' = ' created_at ',    ' value ' =function($data) {         return Date (' y-m-d ',$data-created_at);      },]

Method Two: With the GridView format configuration

[    ' attribute ' = ' created_at ',    ' format ' =>[' Date ', ' php:y-m-d '],    ' value ' = ' created_ ' At ',],

Method Three: First configure your time format in config, otherwise it will be in English format, and then processed in the GridView.

// define your own time, money and other formats in common/config/main.php ' components ' and [    ' formatter ' = [        ' dateformat ' = ' yyyy-mm-dd ',        ' datetimeformat ' = ' yyyy-mm-dd HH:mm:ss ',        ' decimalseparator ' = > ', ', ' thousandseparator ' + ', '        currencycode ' = ' CNY ',    ],

Here's a quick way to write: "Attribute:format:label" (attribute: Format: label) in this format, click here for details on the API documentation. So we can do this: (if the above is defined, then method two can be directly ' format ' = ' date ' to display correctly)

' Created_at:date ',

6, display hyperlinks or pictures, is the method of the callback function, here with the hyperlink as an example, note that format needs to be ' raw ', do not format the results of any formatting, specific formatting aspects can point here to see.

[     ' attribute ' = ' id ',    ' value ' =function($model$key$index  $column) {        return html::a ($model->id,[' User/view ', ' id ' = = $model->id],[' class ' = ' Profile-link ', ' target ' = ' _blank ']);    },    ' Format ' = ' raw ',],

7, the association form display, this place directly to see the guide, point here to see, in short, if the management form is set up, Directly with similar order.name this can be directly displayed, if you want to retrieve the sort, you need to write this field in the Searchmodel rulers rule method and attributes attribute method. Add a Andfilterwhere search in the search method, and use the $query->joinwith associated field, $dataProvider->sort->attributes[] To add sorting, and so on.

8. Custom Action Column button,

[    ' class ' = ' Yii\grid\actioncolumn ',    ' header ' = ' operation ',    ' headeroptions ' =>[' width ' = > ' + ', '    template ' = ' {view} {update} ' {delete} {forbid} ',    ' buttons '        = ' = ' forbid ' = >function($url,$model) {            return Html::A ('  ', [' user/forbid ', ' id ' = = $model->id]);             }    ]

The above is the use of the GridView Yii2 comes with. Again kartik-v/yii2-grid, official documents and demo of the comparison, it is more than the original form of several columns, such as editable column (editable), Radio column ( Radio box), and many other features, such as scrolling can be fixed to the title bar for easy viewing, you can total, export svn,excel and other formats. Here's a little bit:

PDF errors may be error after the installation is complete, as these associations are not yet installed, can be installed as prompted or you can configure the ' export ' =>false to cancel the export function, which will be configured in detail when exporting.

1, the overall structure such as:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.