Yii2 custom delete ActionColumn alert twice - php

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

Related

how to add button to yii gridview not the actionColumn a seperate column

goodday house please am trying to add an extra column "status" with buttons on each row to yii gridview not under the ActionColumn but a seperate column and also i am trying to pass values into the button, so that the button will display text from my database..
i have tried all have seen is to add it to the ActionColumn please i need ur help
i tried doing something like this but it isnt workin
// 'Date',
[
'class' => '',
'template' => '{status}',
'buttons' => [
'status' => function ($url) {
foreach ($reg_course as $reg) {
return Html::a(
'<button class="btn btn-info"><?= $reg->student_status?></button>',
$url,
[
'title' => 'Status',
'data-pjax' => '0',
]
);
}
},
],
],
['class' => 'yii\grid\ActionColumn'],
],
...
[
'label' => '',
'format' => 'raw',
'value' => function ($model) {
$btn = '<a href="' . Url::home() . 'site/index?id=' . $model->id .'"
data-toggle="tooltip" title="Members" data-placement="bottom" class="btn btn-sm
btn-info ">'.$reg->student_status.'</a>';
return $btn;
},
],
use below code -
[
'label'=>'Chapters',
'format' => 'raw',
'value'=>function ($data) {
return Html::a('show', '/admin/chapter?subject_id='.$data['id']);
},
],

Yii2: how to create a custom delete button?

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.

YII2 - How to merge row yii2 gridview?

i have gridview table like this :
i want to merge store 1(example) with 4 row detail information. is it possible to do it with?
i have a code in gridview :
<?php Pjax::begin(['id' => 'pjax_filesetting','timeout'=>false]) ?>
<?php
$this->registerJs(
"
$('.btneditfile').click(function(){
var hqid = $(this).attr('data-hqid');
var storeid = $(this).attr('data-storeid');
var filename = $(this).attr('data-filename');
location.href = '/pos/editfilesetting?hqid='+hqid+'&storeid='+storeid+'&filename='+filename;
return false;
});
");
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'options' => ['class' => 'grid-view active-table'],
'columns' =>
[
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Store Name',
'attribute' => 'store_name',
'encodeLabel' => false,
],
[
'label' => 'Filename',
'attribute' => 'filename',
'encodeLabel' => false,
],
[
'label' => 'Datecheck',
'attribute' => 'datecheck',
'encodeLabel' => false,
'value' => function($model){
$datecheck = $model["datecheck"];
if($datecheck)
{
return $model["datecheck"] = "Check";
}
else
{
return $model["datecheck"] = "Not Check";
}
}
],
[
'label' => 'Timecheck',
'attribute' => 'timecheck',
'encodeLabel' => false,
'value' => function($model){
$timecheck = $model["timecheck"];
if($timecheck)
{
return $model["timecheck"] = "Check";
}
else
{
return $model["timecheck"] = "Not Check";
}
}
],
[
'label' => 'Maintenance code',
'attribute' => 'maincode',
'encodeLabel' => false,
],
[
'label' => 'Final Filename',
'attribute' => 'usedfilename',
'encodeLabel' => false,
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '<div align="center">{update} {delete}</div>',
'buttons' => [
'update' => function ($url, $model) use ($controller) {
return Html::button(
'<span class="glyphicon glyphicon-pencil"></span>',
[
'class' => 'btn btn-primary btn-xs btneditfile',
'title' => Yii::t( $controller->transmodule, 'Edit' ),
'style' => ['padding-top' => '5px', 'padding-bottom' => '5px'],
'id' => 'editfile',
'data-storeid' => $model['id'],
'data-hqid' => $model['cmshqid'],
'data-filename' => $model['filename']
]
);
},
'delete' => function ($url, $model)use ($controller){
return Html::button(
'<span class="glyphicon glyphicon-trash"></span>',
[
'class' => 'btn btn-danger btn-xs btndeletefile',
'title' => Yii::t( $controller->transmodule, 'Delete' ),
'style' => ['padding-top' => '5px', 'padding-bottom' => '5px'],
'data-storeid' => $model['id'],
'data-hqid' => $model['cmshqid'],
'data-filename' => $model['filename']
]
);
},
],
]
],
]) ?>
<?php Pjax::end() ?>
what must i do to merge it?
what i need is if the store name is same, merge it column .
Is the store and other details in one table or separate table? If it is in separate tables, the $dataProvider should query from the store table. In the store model create a hasMany relation to the other table which contain the details. So in the view, in the gridview column function you can loop through those values and display it in the same row.

Yii gridview How to set editable option based on the data

This is my grid view code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'toolbar'=> [
['content'=>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['create'],['class'=>'btn btn-shadow btn-success'])
],
'{toggleData}',
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'Name',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'attribute'=>'Age',
'vAlign'=>GridView::ALIGN_MIDDLE,
'value'=>'Age',
'width'=>'5%',
'hAlign'=>'center',
],
[
'attribute'=>'Address',
'vAlign'=>GridView::ALIGN_MIDDLE,
],
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
'vAlign'=>GridView::ALIGN_MIDDLE,
'hAlign'=>GridView::ALIGN_CENTER,
'width'=>'15%',
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'options' => ['placeholder' => 'Search Status...'],
'pluginOptions' => [
'allowClear' => true
],
],
'value'=>function($model){
return ($model->Status=='0'?'Pending':($model->Status=='1'?'Accepted':'Rejected'));},
'filter'=>['Pending','Diterima'],
'label' => 'Status',
'refreshGrid'=>true,
'editableOptions'=>[
'format' => Editable::FORMAT_BUTTON,
'asPopover' => true,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>['0'=>'Pending', '1'=>'Accepted', '2'=>'Rejected'],
'options' => ['class'=>'form-control'],
]
],
[
'class' => 'kartik\grid\ActionColumn',
'template' => '
<li>{view}</li>
<li>{update}</li>
<li>{delete}</li>',
'dropdown'=>true,
'dropdownOptions'=>['class'=>'pull-right'],
'headerOptions'=>['class'=>'kartik-sheet-style'],
],
],
'panel'=>[
'type'=>GridView::TYPE_DEFAULT,
]
]); ?>
Help me with the Status attribute. The result from this gridview is like this :
what i want is like this :
So, it's like when the Status is Pending, the editableOption is on.But when the Status is Accepted or Rejected, the editableOption is disable. Is that possible ?
You need Change value option of Status Attribute.
Change in value option of Status Attribute.
[
'class' => 'kartik\grid\EditableColumn',
'attribute'=>'Status',
// ------- your code ------------
'format'=>'raw',
'value'=>function($model){
if($model->Status=='0')
{
// if status =0 than create button and append with string
$btn='<button type="button" id='.$model->id.' class="btn btn-default ><i class="fa fa-pencil-square-o"></i></button>';
return 'Pending'.$btn;
}
else
{
return($model->Status=='1'?'Accepted':'Rejected');
}
},
// ------- your code ------------
],
There are multiple ways to do what you have asked, if you have used editableOptions, then
For Editable::INPUT_DROPDOWN_LIST type -> use 'editableButtonOptions' => [ 'disabled' => $data->your_status_variable_condition],
For any other type add readonly property
'readonly' => function($data){
return $data->your_status_variable_condition;
},
I hope it helps someone who is looking for the same issue

YII2 Insert records selected from one grid to another

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 :)

Categories