How to get row number from ActionColumn in GridView

So I’ve asked the question on stack

https://stackoverflow.com/questions/65928145/how-do-i-access-row-number-in-yii2-gridview-actioncolumn/

Basically, I want to get the row number in the action column. I know I can do this with some jQuery and the client side, but I’d really like to do it server side. $model->id and $key both return the id and key of the model, but not the row in the table.

What do I need to replace ROW-NUMBER with?

Any suggestions would be appreciated.

GridView::widget([
  'dataProvider' => $dataProvider,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn',
        'contentOptions' => ['style' => 'font-size:20px']],
    ['header' => 'Blue or Red',
        'class' => 'yii\grid\ActionColumn',
        'template' => '{blue} {red}',
        'buttons' => [
            'blue' => function ($url, $model, $key) {
                return bootstrap\Button::widget([
                    'label' => 'blue',
                    'id' => 'blue' . **ROW-NUMBER**,
                    'options' => [
                        'class' => 'btn-lg btn-dark votebtn',
                        'data-vote' => 1,
                        'data-row' => **ROW-NUMBER**
                    ]
                ]);
            },
            'red' => function($url, $model, $key) {
                return bootstrap\Button::widget([
                    'label' => 'red',
                    'id' => 'red' . **ROW-NUMBER**,
                    'options' => [
                        'class' => 'btn-lg btn-dark votebtn',
                        'data-vote' => -1,
                        'data-row' => **ROW-NUMBER**
                    ]
                ]);
            }
        ],        
    ],
  ],
]);

What is the purpose exactly? What are you trying to achieve? If you explain concretely what you are trying to do perhaps someone can offer a suggestion.

I don’t see why you can’t use the $key as you can easily identify the row based on that. This is why I ask the above.

Hi @QuantumPsi,

Probably you have to use ActionColumn::content instead of ActionColumn::buttons.

ActionColumn::content (or, Column::content in fact) accepts an anonymous function in which you can access $index. This is the zero-based index of the data model among the models array returned by $dataProvider.

So, something like the following should work, I hope.

[
    `class` => `yii\grid\Column`,
    `header`=> `Blue or Red`,
    `content` => function ($model, $key, $index, $column) {
         $blue = bootstrap\Button::widget([
              'label' => 'blue',
              'id' => 'blue' . $index,
              'options' => [
                  'class' => 'btn-lg btn-dark votebtn',
                  'data-vote' => 1,
                  'data-row' => $index
              ]
         ]);
        $red = ...;
        return $blue . ' ' . $red;
    },
    'format' => 'raw',
],

I think you don’t need to use ActionColumn because you are rendering buttons on your own without using its functionality.

If you want the row number to be the one in the current page, you have to access the gridview (or the data provider) for the starting index of the current page:

$rowNumber = $index - $column->grid->something
// or
$rowNumber = $index - $column->grid->dataProvider->something

Sorry, I’m not sure.

Last but not least, @DBCreator’s suggestion to use $key instead of $index is worth considering. It can make things simpler if it works.

2 Likes

This did it! Thank you!