Yii2 GridView Custom Link rewrite actioncolumn

Source: Internet
Author: User
Tags button type

Recently started with Yii2, really great, but there are many shortcomings, today is to say is the GridView link problem.

<?= Gridview::widget ([    $dataProvider,    $searchModel,    ' columns ' = [' email ', [?>    

This is one of the simplest default gridview,gii generated by this, then the problem comes.
If the user management is not a standalone controller, but under the user controller or the site controller, the Actioncolumn default link is view, UPDATE, delete
But I want to be User-view, user-update, user-delete such a link, and then I modified the next, the code is as follows.

<?= gridview::widget ([ " Dataprovider ' +  $dataProvider,  ' filtermodel ' =  $searchModel,  ' columns ' = [[ ' Yii\grid\serialcolumn '],  ' id ',  ' username ',  ' email ', [ ' class ' =  ' Yii\grid\actioncolumn ',  ' template ' =  {User-view} {user-update} {User-delete} ',],],]); ?>             

As a result, everything is gone, why? Then I opened Yii\grid\actioncolumn, read the source, found that he only rendered the default view, update, delete
If a label such as {User-view} does not exist in the button group (buttons[]), the output is empty.

So we have to add a button, and then the code goes like this.

<?= Gridview::widget ([' Dataprovider ' =$dataProvider,' Filtermodel ' =$searchModel,' Columns ' = [[' Class ' =' Yii\grid\serialcolumn '],' ID ',' Username ',' Email ', [' Class ' =' Yii\grid\actioncolumn ',' Template ' =' {User-view} {user-update} {user-delete} ',' Buttons ' = [The following code comes from the Yii\grid\actioncolumn simple modification of the next' User-view ' =function($url,$model,$key) {$options = [' Title ' = Yii::t (' Yii ',' View '),' Aria-label ' = Yii::t (' Yii ',' View '),' Data-pjax ' =' 0 ',];Return Html::a (' <span class= ' Glyphicon glyphicon-eye-open ' ></span> ',$url,$options); },' User-update ' =function($url,$model,$key) {$options = [' Title ' = Yii::t (' Yii ',' Update '),' Aria-label ' = Yii::t (' Yii ',' Update '),' Data-pjax ' =' 0 ',];Return Html::a (' <span class= ' Glyphicon glyphicon-pencil ' ></span> ',$url,$options); },' User-delete ' =function($url,$model,$key) { $options = [ ' title ' = Yii::t ( Yii ',  ' Aria-label ' = Yii::t (  ' Delete '),  ' data-confirm ' = > yii::t ( "Yii",  ' is you sure do want to delete this item? '), 
                                                                           
                                                                             ' Data-method ' + 
                                                                             ' post ',  ' Data-pjax ' = =  ' 0 ',]; return html::a ( ' <span class= "Glyphicon Glyphicon-trash" ></span> ',  $url,  $options);},]], [],]); ?>         
                                                                               

This is OK, but the code is very disgusting, this is not what I want, so I rewrite the yii\grid\actioncolumn to enhance the function of the template.
Similar to ' template ' = ' {url-link:type} ', here the Url-link is your link address, type is the button type, the default Class 3 button is still there.

For example: ' template ' = ' {User-view:view} {user-update:update} {user-del:delete} '
So the address and style can be easily done, of course, you can still customize the button, the same way as above.

<?= Gridview::widget ([' Dataprovider ' =$dataProvider,' Filtermodel ' =$searchModel,' Columns ' = [[' Class ' =' Yii\grid\serialcolumn '],' ID ',' Username ',' Email ', [' Class ' =' Backend\components\actioncolumn ',' Template ' =' {User-view:view} {user-update:update} {User-del:delete} {User-diy-btn:diy} ',' Buttons ' = [Custom Buttons ' diy ' = function  ( $url,  $model,  $key) { $options = [ ' title ' = Yii::t ( ' Aria-label ' + yii::t (  ' View '),  ' data-pjax ' = Span class= "hljs-string" > ' 0 ',]; return html::a ( ' <span class= "Glyphicon Glyphicon-refresh" ></span> ',  $url,  $options);},]], [],]); ?>             

You just add a DIY type button on the OK, if used, you can write directly to backend\components\actioncolumn here.
The effect is as follows.

That's the ideal state, OK, here's the Actioncolumn code.
I put it in the Backend\components directory, you can also put in other places you like, the namespace change is OK.

<?phpNamespaceBackend\components;ClassActioncolumnExtends \Yii\Grid\actioncolumn{Public$template =' {: view} {: update} {:d elete} ';/** * Overrides the label rendering method. *@param mixed $model *@param mixed $key *@param int $index *@return Mixed * *ProtectedfunctionRenderdatacellcontent($model,$key,$index) {Return Preg_replace_callback ('/\\{([^}]+) \\}/',function($matches)Use($model,$key,$index) {List$name,$type) = Explode (‘:‘,$matches [1].‘:‘);Get button name and typeif (!Isset$this->buttons[$type])) {If the type does not exist, the default is view $type =  ' view ';} if ( "= =  $name) {//name is empty, use type name  $name =  $type;}  $url =  $this->createurl ( $name,  $model,  $key,  $index); return call_user_func ( $this->buttons[$ Type],  $url,  $model,  $key);},  $this->template); }} 

btn-warning yellow success Green

plus display plus OK show Checkmark remove display wrong number Eye-open display eye symbol open display upload pencil display Pencil Trash Display Delete
Refresh Display refreshes

Yii2 GridView Custom Link rewrite actioncolumn

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.