Gridview Link

Does anyone know if it’s possible set a link set a link on GridView to a related model. For example, If I have a relationship between User and Profile, using Kartik’s tutorial, I can include the profile id in my list of Users in views/user/index.php using GridView. This is really cool and easy to implement.

I would like to be able to set a link so that the user can click on the profile id and go to views/profile/view.php. I checked what I could find in the docs and couldn’t see if this was possible or how to do it. Any help would be appreciated, thanks.

For your grid column config use the raw format. For example:




'columns' => [

    ['class'=>'yii\grid\SerialColumn'],

    ['attribute'=>'profileLink', 'format'=>'raw']

]



where profileLink, is defined as a model getter like:




public function getProfileLink() {

    $url = Url::to(['/profile/view', 'id'=>$this->id]); // your url code to retrieve the profile view

    $options = []; // any HTML attributes for your link

    return Html::a($this->profile->name, $url, $options); // assuming you have a relation called profile

}



Thanks, I 'm getting:


 PHP Fatal Error – yii\base\ErrorException


Class 'common\models\Url' not found

I think it’s probably a lack of namespace in the User model. I clicked on the class in my IDE to find declaration and it couldn’t find it. I looked through the vendor folder but I couldn’t find the URL class.

Namespace all your classes properly:




use yii\helpers\Url;



I added that namespace, however it still can’t find the class. Also, I looked under vendor/yiisoft/yii2/helpers and there was no Url class. I don’t know where else to look for it.

You seem to have an outdated install. Run composer update to upgrade to latest version.

Did the update on composer. Just popped the method into the model and got the following:


PHP Fatal Error – yii\base\ErrorException


Class common\models\User contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (yii\web\IdentityInterface::findIdentityByAccessToken)

1. in C:\var\www\yiitry\common\models\User.php at line 313

308309310311312313    $options = []; // any HTML attributes for your link

    return Html::a($this->profile->name, $url, $options); // assuming you have a relation called profile

    }  

  */  

 

}

2. yii\base\Application::handleFatalError()

Ok, I can see it’s looking for a method I don’t have, so I popped in a dummy method and it allowed the class to load. Looks like this is going to work. I have to build the backend view controller/page and also do a fresh install of the advanced template so I can see what method it was really looking for and make sure I include that. I will work on this and update progress…

Thanks Kartik, this worked out really well. I was also able to use




['attribute'=>'userLink', 'format'=>'raw'],



on the DetailView widget in the view, so it works in both the GridView widget and the DetailView widget. Great stuff, thanks again.

If you want to enable sort and filter on the grid column, you may use the Scenario 2 in my wiki.