2016-08-01 8 views
6

でアクション列にURLを削除し、インデックスページにGridViewコントロールで、更新およびyii2

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, 
     ]); 
} 
} 

答えて

13
 [ 
      '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; 
      } 

      } 
      ], 
+0

が見つかりません!よくやった。 –

+0

柔軟性を高めるために、 'return \ yii \ helpers \ url :: to 'のように[Urlヘルパークラス](http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html)を使うことができます(['/コントローラ/アクション'、 'param1' => 'param1value'、 'param2' => 'param2value']); '。こうすることで、コントローラーアクションの名前を変更する場合、ルーティングスキームは最小限の調整だけで済み、どこのリンクもすべて自動的に調整されます。 – bds

3

を私は、コントローラ内の更新とビューのアクションを作成しているが、このアクションは、アクション列に変更されません。

[ 
    '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']); 
     }, 
    ] 
] 
+0

回答を更新してください。 1つの閉じるスクエアブラケットに 'buttons' => [ - –

4

質問は

あるので、どのようにCへハンゲビュー、更新および私は削除アクション

[ 
'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', 
     ]); 
    }, 
] 

を追加することにより、非常識な、頭蓋骨@の答えを向上していますyii2

でアクション列に URLを削除]

1

多くの場合、あなたが必要アクションボタンのURLのコントローラ名だけを変更します。 を使用して簡単に行うことができます。urlCreator