Search code examples
yii2

yii2 : make SerialColumn a link


How to make the numbering result of the SerialColumn a link. Normally it generates number starting from 1. I want to make it a link. What property to use?

'columns' => [
    // ...
    [
        'class' => 'yii\grid\SerialColumn',
        // you may configure additional properties here
    ],
]

Solution

  • You can't using the actual SerialColumn class.

    That being said it should be fairly easy to do with a regular column. You can define a content callback that will receive all the necessary information to do this on its own:

    'columns' => [
      // ...
      [
         'content' => function($model, $key, $index, $column) {
            $globalIndex = $index + 1;
            $pagination = $column->grid->dataProvider->getPagination();
            if ($pagination !== false) {
               $globalIndex = $pagination->getOffset() + $index + 1;
            }
            return \yii\helpers\Html::a($globalIndex, ['/route/action', 'id' => $globalIndex]);
         } 
      ],
      //...
    ]
    

    Note: I haven't tested this so might not fully work out of the box.