I'm using Yii2 Framework. I have data on GridView and each row has a delete button. Here's my code:
'content'=>function($data){
return Html::a('<i class="glyphicon glyphicon-ban-circle"></i> Bekor qilish', ['delete'], ['class' => 'btn btn-danger', 'data-method'=>'post']);
I want post actionDelete to be fired when user clicks the button. My code isn't working, and it's giving me Error #400: Missing required parameters: id.
I found the following code that I believe from Yii v1:
echo CHtml::link("Delete", '#', array('submit'=>array('post/delete', "id"=>$data->id), 'confirm' => 'Are you sure you want to delete?'));
I want this very function for Yii2.
I'm new to Yii Framework. Please, kindly help me.
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;
}
}
],
],
]);
Make sure you set a delete action in your controller and replace the 'your_controller/your_delete_action'
Replace the Some_id_from_your_db to the element id you want to delete from your db. Hope it helps. Cheers.
Edit: The 'Sample_Column1', 'Sample_Column2', is just for your reference because you didn't mention your table details. Therefore edit it accordingly.
Related
$query =Cms::find();
$dataProvider = new ActiveDataProvider([
'query' => $query ,
]);
Hi, I'm trying to hide certain rows whose 'Status_id' is set to 'Inactive' which is done using a custom action button I've created in the Actions column. I figured I might be able to do it if I could add my own query in the $dataprovider but I don't know how to do that too. Please help if it's the right way and if not how to do I do it? Thanks in advance.Here's my widget.
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'Contract_id',
'Status_id',
['header' => 'Actions',
'class' => 'yii\grid\ActionColumn',
'template' => '{new_action}',
'buttons' => [
'new_action' => function ($url) {
return Html::a('<span class="glyphicon glyphicon-fire"></span>', $url, [
'title' => Yii::t('app', 'Burn Contract'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to burn this contract?'),
'data-method' => 'post', 'data-pjax' => '0',
]);
}
], 'urlCreator' => function ($action, $model) {
if ($action === 'new_action') {
$url = Url::to(['cms/burn', 'id' => $model->Contract_id]);
return $url;
}
}
],
],
]);
i think this may work:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'Contract_id',
'Status_id',
['header' => 'Actions',
'class' => 'yii\grid\ActionColumn',
'template' => '{new_action}',
'buttons' => [
'new_action' => function ($url) {
return Html::a('<span class="glyphicon glyphicon-fire"></span>', $url, [
'title' => Yii::t('app', 'Burn Contract'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to burn this contract?'),
'data-method' => 'post', 'data-pjax' => '0',
]);
}
], 'urlCreator' => function ($action, $model) {
if ($action === 'new_action') {
$url = Url::to(['cms/burn', 'id' => $model->Contract_id,'burn'=>true]);
return $url;
}
}
],
],
]);
controller:
$query =Cms::find();
if (\Yii::$app->request->post('burn'))
{
$query->where(['<>','Status_id', 'Inactive']);
}
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
That's how I am rendering the values on a grid view
but instead of links I can see the textual value.
How can I make it render html instead of text?
In link column configuration add:
'format' => 'html',
or if you want some extra markup there
'format' => 'raw',
In case of raw remember to encode values coming from outside users because it's not done automatically.
A better way of doing this in Yii.
'value' => function ($data) {
return Html::a($data->name, [$data->url, 'someData' => $data->someData]);
}
Yii Doc: https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#a()-detail
A little late on the post but, hope it helps the in future.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'email:email',
'timestamp:date',
[
'attribute'=>'Resume',
'format' => 'raw',
'class' => 'yii\grid\DataColumn', // can be omitted, as it is the default
'value' => function ($data) {
$url = "www.sample.com/contactform/resumes".$data->resumepath;
return Html::a('<i class="glyphicon glyphicon-download-alt"></i>', $url);
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I have a custom action column with just the delete button:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{delete}',
'buttons' => [
'delete' => function($url, $data){
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['orders-lines/delete', 'id' => $data->id], [
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]);
}
]
],
and whenever i click the trash icon, then accept the alert box, the same alert box is triggered again so I have to accept twice the alert box.
What could i be missing?
Thanks
Try this
['class' => 'yii\grid\ActionColumn',
'template' => '{delete}',
'buttons' => [
'delete' => function ($url, $data) {
return Html::a('<span class="glyphicon glyphicon-trash" title="Delete"></span>', ['orders-lines/delete', 'id' => $data->id], ["data-pjax" => 0, 'onClick' => 'return confirm("Are you sure you want to delete this item?") ']);
},
],
],
i'm new in Yii2, previously i use self-made-mvc-php + extjs 4.2
So i working on Yii2 now, i'm starting to get the hang of it for simple CRUD, my pobrem starts when i need to make entry form for parent-child tables.
I have 3 tables
commission
commission_id
commission_name
commission_amount
commission_percent
commission_scheme
cscheme_id
cscheme_name
cscheme_amount
cscheme_percent
cscheme_bonus
cscheme_description
commission_scheme_detail
cscheme_detail_id
commission_id
cscheme_id
The entry form is for commission_scheme and commission_scheme_detail.
I have generated all the crud using gii
i modify _form.php, add a gridview of commission_scheme_detail after the active form
i also add modal containing selection grid from commission
this is _form.php
gridview
<?php Pjax::begin(['id' => 'pjaxCschemedetail',
'timeout' => false,
'enablePushState' => false,
'clientOptions' => ['method' => 'POST']
]);
echo GridView::widget([
'id' => 'gridCschemedetail',
'dataProvider' => $detailData,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'CSCHEME_DETAIL_ID',
'CSCHEME_ID',
'COMMISSION_ID',
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'update' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['commission-scheme-detail/view','id' => $model->CSCHEME_DETAIL_ID,'edit'=>'t']), [
'title' => Yii::t('yii', 'Edit'),
]);}
],
],
],
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
'panel' => [
'type'=>'info',
'before'=>
'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset List', ['index'], ['class' => 'btn btn-info']),
Html::button('Add', [
'id' => 'addDetailButton',
'class' => 'glyphicon glyphicon-plus btn btn-success btn-ajax-modal',
'value' => Url::to('#web/commission/listselect'),
'data-target' => '#modal_cschemedetail',
]),
'showFooter'=>false
],
]); Pjax::end();
Modal
Modal::begin([
'id' => 'modal_cschemedetail',
'header' => '<h4>Category</h4>',
]);
echo '<div id="modal-content"></div>';
echo Html::button('Add Selected', [
'id' => 'addCommissionsButton',
'class' => 'btn btn-success btn-ajax-modal'
]);
Modal::end();
?>
</div>
</div>
<div class="box-body">
</div>
</div>
selection grid
<?php Pjax::begin(); echo GridView::widget([
'id' => 'gridCommissionSelection',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'COMMISSION_ID',
'visible' => false
],
'COMMISSION_NAME',
[
'attribute' => 'TRANSACTION_TYPE_ID',
'value' => 'transactionTypeName'
],
'COMMISSION_DESC:ntext',
'COMMISSION_AMOUNT',
]
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
]); Pjax::end(); ?>
Is it possible to insert the selected records from the modal to the detail gridview, then after user click the create button, the form and the inserted records in gridview saved to respective tables?
How can i insert the selected records to gridview (not to db table, it will be done after user click create)?
I suggest you to simplify your flow. You can easily save your models (performing an insert/update) at Modal and then refresh the page or call the $.pjax.reload('#gridCommissionSelection') to redraw the grid-view with new entries.
Another way is to append new lines with JS and then save them via AJAX request. It will be rather dirty and unclear solution, I guess :)
I am trying to make an Ajax GridView using Pjax with delete button. Deleting goes with no Ajax. I am new to Yii2 so any help would be appreciated. Thank you.
index.php
<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
]);
},
]
],
],
]); ?>
<?php Pjax::end() ?>
Controller
public function actionDelete($id)
{
$model = new Category();
$this->findModel($id)->delete();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
This is public function actionIndex() in the Controller
public function actionIndex()
{
$model = new Category();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Category();
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
data-method and data-confirm don't let you create ajax request via pjax, you should implements your own confirmation dialog and drop POST verb filter, or you can implements your own ajax plugin with confirmation dialog and specifying http method.
Also, i think, there must be way to extend pjax plugin by confirmation dialog, but Yii2 don't provide this by default.
first of all remove 'data-confirm' and 'data-method' => 'post'. pjax not going to work.
If you want to implement a confirm box with action button, here is what I would do in my view index.php file ..
<?php Pjax::begin(['id' => 'pjax-container']);
echo GridView::widget([
'test' => function ($url, $dataProvider) {
return Html::a('Test',
['/site/test'],
['title'=>'Test',
'onclick' => "if (confirm('ok?')) {
$.ajax('/site/test', {
type: 'POST'
}).done(function(data) {
$.pjax.reload({container: '#pjax-container'});
});
}
return false;
",
]);
},
])
Pjax::end();
?>
and in my controller
public function actionTest()
{
if (!Yii::$app->request->isAjax) {
return $this->redirect(['index']);
}
}
This way you would have confirmation etc. as well. If you want you may use other third party bootstrap confirmation etc. and will work fine.
<?php Pjax::begin(['id' => 'model-grid']);
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//...
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['class' => 'action-column'],
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => 'Delete',
'data-pjax' => '#model-grid',
]);
},
],
],
],
]);
Pjax::end(); ?>
In controller
public function actionDelete($id)
{
$this->findModel($id)->delete();
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Please try modify actionDelete()
public function actionDelete($id)
{
$this->findModel($id)->delete();
return \yii\web\Response::redirect(['index'] , 302 , false);
// return $this->redirect(['index']);
}
because Controller->redirect() can not disable ajaxCheck , you need use Response to do this.
I have create the same issue in https://github.com/yiisoft/yii2/issues/11058.
Can use like this:
in view:
'delete' => function ($url, $model, $key) {
$options = [
'title' => Yii::t('common', 'delete'),
'aria-label' => Yii::t('common', 'delete'),
'data-pjax' => 'w0',//id
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'class' => 'btn btn-xs btn-danger'
];
return Html:: a('<i class="fa fa-fw fa-trash"></i>', [
'delete',
'id' => $model -> id
], $options);
}
in controller:
$this -> findModel($id) -> delete ();
$searchModel = new AdminSearch();
//get the referer url
$url = Yii::$app -> request -> referrer;
$arr = parse_url($url, PHP_URL_QUERY);
parse_str($arr, $output);//get the $_GET array
$dataProvider = $searchModel -> search($output);
return $this -> render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
First add Pjax::end(); at end of the gridview
then specify:
'delete' => function ($url, $model, $key)
{
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
]);
},
Note that you don't need to specify 'data-pjax' => '0' because it disables the pjax link.
For more details check this link
Your controller should be:
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
Don't set data-method and data-confirm because Pjax not supported that.
After removing both still not workign,yes because of below code of your controler does not allow Pjax get Request.
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'], // **remove this**
],
],
];
You need to use Pjax Post Method
Apply this in Your Pjax
'clientOptions' => ['method' => 'POST']