Label link in GridView column

Hello,

I need your help, please.

All the labels of the columns in a gridview widget have some labels where is possible to sort the contenent with one click. Great.

But one of this columns is bounded to a relation:


[

'attribute' => 'status.status',

       'label' => 'Stato',

],

I added the label, as you can see, but now I can’t figure out how to make linkable and sortable this label.

Start with Kartik’s tutorial scenario 2.

Then, for clickable link on Gridview, make sure you have the following methods on the model for relationship. In this example, it is a relationship between user and profile and these methods are on the User model.





  public function getProfile()

    {

        return $this->hasOne(Profile::className(), ['user_id' => 'id']);

    }

    

    /**

    * @getProfileId

    * 

    */

 	public function getProfileId() 

    {

        return $this->profile ? $this->profile->id : 'none';

    }

    

    /**

    * @getProfileLink

    * 

    */

    public function getProfileLink() 

    {

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

        $options = []; 

        return Html::a($this->profile ? 'profile' : 'none', $url, $options);

    }




Don’t forget the attributes labels:





 'profileId' => Yii::t('app', 'Profile'),

 'profileLink' => Yii::t('app', 'Profile'),



Then in Gridview:




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



In the above examples, you need a method to return the id of the related record, so you can use that for the link.

Refer to the tutorial for how to set up search model. In your case, you are obviously going to use a relationship to status instead of profile. Hope this helps.

Thank you very much ! :slight_smile: