我正在使用Yii2 Framework。我有GridView上的数据,每行都有一个删除按钮。这是我的代码:
'content'=>function($data){
return Html::a('<i class="glyphicon glyphicon-ban-circle"></i> Bekor qilish', ['delete'], ['class' => 'btn btn-danger', 'data-method'=>'post']);
我希望在用户点击按钮时触发post actionDelete。我的代码无效,它给了我错误#400:缺少必需的参数:id。
我从Yii v1中找到了以下代码:
echo CHtml::link("Delete", '#', array('submit'=>array('post/delete', "id"=>$data->id), 'confirm' => 'Are you sure you want to delete?'));
我想要Yii2的这个功能。 我是Yii Framework的新手。请帮助我。
答案 0 :(得分:6)
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'Sample_Column1',
'Sample_Column2',
['header' => 'Actions',
'class' => 'yii\grid\ActionColumn',
'template' => '{new_action}',
'buttons' => [
'new_action' => function ($url) {
return Html::a('<span class="glyphicon glyphicon-ban-circle"></span>', $url, [
'title' => Yii::t('app', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete?'),
'data-method' => 'post', 'data-pjax' => '0',
]);
}
], 'urlCreator' => function ($action, $model) {
if ($action === 'new_action') {
$url = Url::to(['your_controller/your_delete_action', 'id' => $model->Some_id_from_your_db]);
return $url;
}
}
],
],
]);
'your_controller/your_delete_action'
Some_id_from_your_db
替换为要从数据库中删除的元素ID。希望能帮助到你。欢呼声。修改:'Sample_Column1', 'Sample_Column2',
仅供您参考,因为您没有提及您的表格详情。因此,请相应地进行编辑。