Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

How do I access row number in Yii2 GridView ActionColumn?

I have some custom buttons in the actionColumn of gridview in Yii2. I need to add the row number to the button data. Here's what I have...

GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yiigridSerialColumn',
            'contentOptions' => ['style' => 'font-size:20px']],
        ['header' => 'Blue or Red',
            'class' => 'yiigridActionColumn',
            'template' => '{blue} {red}',
            'buttons' => [
                'blue' => function ($url, $model, $key) {
                    return bootstrapButton::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 bootstrapButton::widget([
                        'label' => 'red',
                        'id' => 'red' . **ROW-NUMBER**,
                        'options' => [
                            'class' => 'btn-lg btn-dark votebtn',
                            'data-vote' => -1,
                            'data-row' => **ROW-NUMBER**
                        ]
                    ]);
                }
            ],        
        ],
    ],
]);

What do I need to put in place of ROW-NUMBER to get the row each button is in?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

assuming your "ROW-NUMBER" is a valid model value eg id

you an use $model->id if you have mode object

 'buttons' => [
            'blue' => function ($url, $model, $key) {
                return bootstrapButton::widget([
                    'label' => 'blue',
                    'id' => 'blue' . **ROW-NUMBER**,
                    'options' => [
                        'class' => 'btn-lg btn-dark votebtn',
                        'data-vote' => 1,
                        'data-row' => $model->id
                    ]
                ]);
            },
            'red' => function($url, $model, $key) {
                return bootstrapButton::widget([
                    'label' => 'red',
                    'id' => 'red' . **ROW-NUMBER**,
                    'options' => [
                        'class' => 'btn-lg btn-dark votebtn',
                        'data-vote' => -1,
                        'data-row' => $model->id
                    ]
                ]);
            }
        ],        

or use $model['id'] if you have an array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...