Yii2 Howto Create Link To Update Page In Detailview::widget

Can you help me to create a link inside detailView::Widget

The link should refer to the product update page like:

http://localhost/basic2/web/product/update?id=3

(Preferably in a modal popup)

Or should I create the link in the Model?

I Think I’ll have to create an array at ‘prod_name’ wit With HTML:a or so…

Here’s a part of my Product/view.php file


<?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'prod_name',

            'prod_price',

        ],

    ]) ?>

If you have generated your CRUD via gii tool, then you can see an example of the Update Link / button in the view.php file. Something like this below:




<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>



If you need to trigger update via modal… you need to use ajax/pjax in your code.

Thanks for the fast reply

I’ve tried but doesn’t work and I don’t where to place prod_name


  <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            [Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id])],

            'prod_price',          

        ],

    ]) ?>

I need an attribute label

Don’t place the code inside the detail view widget. Just place the code after or before the detail view - for you to render the Update button.

Is there a reason why you want to display this button inside the detail view?

I don’t need a button, just want to click on the product-name that will take me to that product’s edit page.

Sorry I’ve mixed up the detail view page with the index page.

and… the product_name is inside a GridView::widget

Assuming your attribute is product_name this should be a possible code




<?= DetailView::widget([

    'model' => $model,

    'attributes' => [

        'id',

        [

            'attribute'=>'product_name',

            'format'=>'raw',

            'value'=>Html::a($model->product_name, ['update', 'id' => $model->id]),

        ],

        'prod_price',          

    ],

]) ?>



Thanks Kartiv V that did the trick for the view file,

now for my index file wat do I have to put in place of $model->prod_name and $model->id

to get it working in my index file?


 <?=

    GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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

            [

                'attribute' => 'prod_name',

                'format' => 'raw',

                'value' => Html::a($model->prod_name, ['update', 'id' => $model->id]),

            ],

            'prod_price',

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

        ],

    ]);

    ?>

I suggest you read the documentation for using yii\grid\GridView and yii\widgets\DetailView.

You cannot set attributes the way you have for the GridView. It should be something like this:




    echo GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

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

            [

                'attribute' => 'prod_name',

                'format' => 'raw',

                'value' => function ($model, $key, $index) { 

                    return Html::a($model->prod_name, ['update', 'id' => $model->id]);

                },

            ],

            'prod_price',

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

        ],

    ]);



Read the yii\grid\DataColumn documentation as well.

Thank you very much, this was exactly what I meant ! Do you ever sleep? :)

Yes when I am not awake … ;) … just kidding.