Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change view, update and delete url on action column in yii2

Tags:

yii2

I have create action on update and view in controller, but this action does not change on action column in index page

 public function actionLeadView($id){
   $id =  $_GET['id'];
   $model = Leads::findOne($id);
   return $this->render('viewlead', [
            'model' => $model,
        ]);
 }    
 public function actionLeadUpdate($id){
   $id =  $_GET['id'];
   $model = Leads::findOne($id);
   date_default_timezone_set("Asia/Kolkata");
   $date = date('Y/m/d H-i-sa');       
    if ($model->load(Yii::$app->request->post())) {
      $model->modified = $date;
      if($model->validate()){
         $model->save();
         return $this->redirect(['viewlead', 'id' => $model->id]);                
            }else {
            return $this->render('updatelead', [
                'model' => $model,
            ]);
      }    
   } 
   else 
   {
        return $this->render('updatelead', [
            'model' => $model,
        ]);
 }
}
like image 668
Jaya Kumari Avatar asked Aug 01 '16 06:08

Jaya Kumari


5 Answers

       [
          'class' => 'yii\grid\ActionColumn',
          'header' => 'Actions',
          'headerOptions' => ['style' => 'color:#337ab7'],
          'template' => '{view}{update}{delete}',
          'buttons' => [
            'view' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                            'title' => Yii::t('app', 'lead-view'),
                ]);
            },

            'update' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                            'title' => Yii::t('app', 'lead-update'),
                ]);
            },
            'delete' => function ($url, $model) {
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                            'title' => Yii::t('app', 'lead-delete'),
                ]);
            }

          ],
          'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
                $url ='index.php?r=client-login/lead-view&id='.$model->id;
                return $url;
            }

            if ($action === 'update') {
                $url ='index.php?r=client-login/lead-update&id='.$model->id;
                return $url;
            }
            if ($action === 'delete') {
                $url ='index.php?r=client-login/lead-delete&id='.$model->id;
                return $url;
            }

          }
          ],
like image 199
Er Sahaj Arora Avatar answered Oct 18 '22 11:10

Er Sahaj Arora


Often you need to change just controller name in action button's url. You can do it simple using urlCreator

[
    'class' => 'yii\grid\ActionColumn',
    'urlCreator' => function ($action, $model, $key, $index) {
        return Url::to(['another-controller-name/'.$action, 'id' => $model->id]);
    }
],
like image 32
Skav Avatar answered Oct 18 '22 13:10

Skav


In gridview,

[
    'class' => 'yii\grid\ActionColumn',
    'template' => '{leadView} {leadUpdate}',
    'buttons' => [
       'leadView' => function ($url, $model) {
           $url = Url::to(['controller/lead-view', 'id' => $model->whatever_id]);
          return Html::a('<span class="fa fa-eye"></span>', $url, ['title' => 'view']);
       },
       'leadUpdate' => function ($url, $model) {
           $url = Url::to(['controller/lead-update', 'id' => $model->whatever_id]);
           return Html::a('<span class="fa fa-pencil"></span>', $url, ['title' => 'update']);
       },
    ]
]
like image 8
Insane Skull Avatar answered Oct 18 '22 13:10

Insane Skull


Since the question is

how to change view, update and delete url on action column in yii2

I'm improving the answer of @insane-skull by adding delete action

[
'class'    => 'yii\grid\ActionColumn',
'template' => '{leadView} {leadUpdate} {leadDelete}',
'buttons'  => [
    'leadView'   => function ($url, $model) {
        $url = Url::to(['controller/lead-view', 'id' => $model->whatever_id]);
        return Html::a('<span class="fa fa-eye"></span>', $url, ['title' => 'view']);
    },
    'leadUpdate' => function ($url, $model) {
        $url = Url::to(['controller/lead-update', 'id' => $model->whatever_id]);
        return Html::a('<span class="fa fa-pencil"></span>', $url, ['title' => 'update']);
    },
    'leadDelete' => function ($url, $model) {
        $url = Url::to(['controller/lead-delete', 'id' => $model->whatever_id]);
        return Html::a('<span class="fa fa-trash"></span>', $url, [
            'title'        => 'delete',
            'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
            'data-method'  => 'post',
        ]);
    },
]

]

like image 6
Clément Baconnier Avatar answered Oct 18 '22 12:10

Clément Baconnier


I am new in Yii2, thank you for helping me with your code @cbaconnier. This is your code, with a little modification:

[
'class'    => 'yii\grid\ActionColumn',
'template' => '{leadView} {leadUpdate} {leadDelete}',
'buttons'  => [
    'leadView'   => function ($url, $model) {
        $url = Url::to(['datakegiatan/view', 'id' => $model->ID_DATA]);
        return Html::a('<span class="fa fa-eye"></span>', $url, ['title' => 'view']);
    },
    'leadUpdate' => function ($url, $model) {
        $url = Url::to(['datakegiatan/update', 'id' => $model->ID_DATA]);
        return Html::a('<span class="fa fa-pencil"></span>', $url, ['title' => 'update']);
    },
    'leadDelete' => function ($url, $model) {
        $url = Url::to(['datakegiatan/delete', 'id' => $model->ID_DATA]);
        return Html::a('<span class="fa fa-trash"></span>', $url, [
            'title'        => 'delete',
            'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
            'data-method'  => 'post',
        ]);
    },
]],
like image 1
monti Avatar answered Oct 18 '22 13:10

monti