我已在控制器中创建有关更新和视图的操作,但此操作在索引页中的操作列上不会更改
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,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 33
[
'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;
}
}
],
Run Code Online (Sandbox Code Playgroud)
通常,您需要在操作按钮的URL中更改控制器名称.您可以使用urlCreator轻松完成
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function ($action, $model, $key, $index) {
return Url::to(['another-controller-name/'.$action, 'id' => $model->id]);
}
],
Run Code Online (Sandbox Code Playgroud)
在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']);
},
]
]
Run Code Online (Sandbox Code Playgroud)
既然问题是
如何在yii2中更改视图,更新和删除动作列上的URL
我通过添加删除操作来改善@ insane-skull的答案
[
'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',
]);
},
]
Run Code Online (Sandbox Code Playgroud)
]
归档时间: |
|
查看次数: |
32517 次 |
最近记录: |