how to add bootstrap modal to update action in YII2 - php

I know the below code creates bootstrap modal with ADD button. I want to make this work in update button. And below that is the yii2 action column of index page.
I need to open bootstrap modal when the update button is clicked.
Modal Code
Modal::begin([
'toggleButton' => [
'label' => '<i class="glyphicon glyphicon-plus"></i> Add',
'class' => 'btn btn-success'
],
'closeButton' => [
'label' => 'Close',
'class' => 'btn btn-danger btn-sm pull-right',
],
'size' => 'modal-lg',
]);
$newmodel = new Comment();
echo $this->render('/comment/create', ['model' => $newmodel]);
Modal::end();
Action Column in index page
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {update} {delete}',
/* 'buttons'=>[
'view' => function ($url, $comment) {
return Html::a('<span class="glyphicon glyphicon-plus">
</span>',$url,
[
'title' => Yii::t('yii', 'view'),
]);
}
], */
'urlCreator' => function ($action, $dataProvider, $key, $index) {
if ($action === 'view') {
$url = Yii::$app->urlManager->createUrl('option?id='.$key.'');
return $url;
}
if ($action === 'update') {
$url = Yii::$app->urlManager->createUrl('question/update?id='.$key.'');
return $url;
}
if ($action === 'delete') {
$url = Yii::$app->urlManager->createUrl('question/delete?id='.$key.'');
return $url;
}
}
],

Related

Yii2 custom delete ActionColumn alert twice

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

Yii2 Delete Alert box not working

I generated a model,controller and a view by Gii,And I enabled the actionColumn in index.php.
I have delete button to delete the records.The buttons are working good but,It doesn't shows me the Alert box to confirm my action.
I am attaching my code here.
Can anyone tell what I am missing?
Index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
$this->title = 'Cities';
$this->params['breadcrumbs'][] = '/ '.$this->title;
?>
<div class="cities-index">
<h2><?= Html::encode($this->title) ?>
<?php //echo $this->render('_search', ['model' => $searchModel]); ?>
<?= Html::a('Create Cities', ['create'], ['class' => 'btn btn-success','style'=>'float:right']) ?>
</h2>
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'country_id',
[
'attribute'=>'country_id',
'value'=>'country.country_name',
],
'city_name',
'city_tax',
[ 'class' => 'yii\grid\ActionColumn',
'header'=>'Action',
'template'=>'{view}{update}{delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-success icon-search bigger-120" style="margin-left:5px;"></span>', $url, [
'title' => Yii::t('app', 'View'),
]);
},
'update' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-info icon-pencil blue bigger-120" style="margin-left: 5px;"></span>', $url, [
'title' => Yii::t('app', 'Update'),'<span class="glyphicon glyphicon-search"></span>'
]);
},
'delete' => function ($url, $model) {
return Html::a('<span class="btn btn-xs btn-danger icon-trash bigger-120" style="margin-left: 5px;" ></span>',
$url, [
'title' => Yii::t('app', 'Remove'),'<span class="glyphicon glyphicon-search"></span>',
'data-confirm' => 'Are You sure',
'data-method' => 'post'
]);
},
],
],
],
]); ?>
</div>

Yii2 : pjax in window popup

first, sorry for my english...
i have a CRUD in window popup, but i can't update the grid in ajax success (insert, update and delete) without refresh page and closes modal.
In success ajax:
}).done(function(data) {
$('#div-form').html(data.form);
if(data.success){
$.pjax.reload({container:'#pjax-resposta-possivel'});
}
})
In action controller:
public function actionSalvarRespostaPossivel()
{
$return = [
'success' => false,
'form' => null
];
Yii::$app->response->format = Response::FORMAT_JSON;
$post = Yii::$app->request->post();
if ($post && $post['RespostaPossivelDetalhes']['id']) {
$model = RespostaPossivelDetalhes::findOne([
$post['RespostaPossivelDetalhes']['id']
]);
} else {
$model = new RespostaPossivelDetalhes();
}
if ($model->load($post) && $model->save()) {
$return['success'] = true;
$tblcacd_id = $model->tblcacd_id;
$model = new RespostaPossivelDetalhes();
$model->tblcacd_id = $tblcacd_id;
}
$return['form'] = $this->renderPartial('_formRespostaPossivel', [
'model' => $model
]);
return $return;
}
In view:
<?php Pjax::begin(['enablePushState' => false, 'id' => 'pjax-resposta-possivel'])?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'emptyCell' => ' ',
'summary' => "Exibindo {begin} - {end} de {totalCount}.",
'layout' => "{pager}\n{items}\n{summary}",
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'chave',
'desc_resposta',
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions'=>['style'=>'width: 10%;text-align:center;'],
'buttons' => [
'update' => function ($url, $model)
{
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', false, [
'data-id' => $model->id,
'class' => 'btn-update-resposta',
'style' => 'cursor:pointer;'
]);
},
'delete' => function ($url, $model)
{
return Html::a('<span class="glyphicon glyphicon-trash"></span>', false, [
'data-id' => $model->id,
'class' => 'btn-delete-resposta',
'style' => 'cursor:pointer;'
]);
}
]
],
],
]); ?>
<?php Pjax::end(); ?>
I can just update the grid without closing the modal ?
Thanks

Yii2 Call to a member function getImageUrl() on a non-object

I have a GridView where it displays data and one of them is an image. Here is my code in my GridView:
echo GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fullName',
[
'header' => '<a style="cursor: pointer;">Attachment</a>',
'format' => 'html',
'value' => function ($data) {
return Html::img($data->getImageUrl(), ['class' => 'reim-attach']);
},
],
'receipt_company',
'description',
'date',
'amount',
[
'attribute' => 'chargeable',
'value' => function ($model) {
return $model['chargeable'] ? 'Chargeable' : 'Non-chargeable';
},
],
'GST_amount',
'date_noted',
[
'attribute' => 'status',
'label' => 'Status',
'content' => function ($model, $key, $index, $column) {
if ($model['status'] == "Pending") {
return Html::button('Pending', ['class' => 'status-pending']);
} elseif ($model['status'] == "Draft") {
return Html::button('Draft', ['class' => 'status-pending']);
} elseif ($model['status'] == "Approved") {
return Html::button('Approved', ['class' => 'status-approved']);
} else {
return Html::button('Rejected', ['class' => 'status-rejected']);
}
}
],
[
'label' => 'Action',
'content' => function ($model, $key, $index, $column) {
if($model['status'] == "Pending") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-danger btn-responsive', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
} elseif($model['status'] == "Draft") {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Save Reimbursement', ['value' => $model['_id'], 'class' => 'btn btn-info btn-responsive', 'onclick'=>'saveReimbursement(value)', 'data-toggle'=>'tooltip','title'=>'Save Reimbursement', 'data' => [ 'confirm' => 'Are you sure you want to save this reimbursement?', 'method' => 'post', ]]);
} else {
return Html::button('<span class="glyphicon glyphicon-eye-open"></span>', ['value' => Url::to(['view']).'&id=' . (string)$model['_id'], 'class' => 'btn btn-warning btn-view btn-responsive','id' => 'modalButton2'])
.'&nbsp'
.Html::button('<i class="fa fa-check-circle-o"></i> Approve', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'approve(value)', 'data-toggle'=>'tooltip','title'=>'Approve', 'data' => [ 'confirm' => 'Are you sure you want to approve this reimbursement?', 'method' => 'post', ]])
.'&nbsp'
.Html::button('<i class="fa fa-ban"></i> Reject', ['value' => $model['_id'], 'class' => 'btn btn-default btn-responsive disable', 'onclick'=>'reject(value)', 'data-toggle'=>'tooltip','title'=>'Reject', 'data' => [ 'confirm' => 'Are you sure you want to reject this reimbursement?', 'method' => 'post', ]]);
}
}
]
],
]);
I get the following error on the line return Html::img($data->getImageUrl(), ['class' => 'reim-attach']):
Call to a member function getImageUrl() on a non-object
Here's a snippet of my controller:
$model = new Reimbursement();
$reimbursementQuery = new Query;
$reimbursementQuery->select([])->from('reimbursement')->andwhere(['company_id' => new \MongoId($session['company_id'])]);
if (isset(Yii::$app->request->getBodyParams()['period'])) {
if(Yii::$app->request->getBodyParams()['period'] != '') {
echo Yii::$app->request->getBodyParams()['period'];
$refreshData = true;
$selectedPeriodID = Yii::$app->request->getBodyParams()['period'];
$selectedPeriod = Periods::find()->where(['_id' => $selectedPeriodID])->one();
$reimbursementQuery->andWhere(['date_reimbursed' => array('$gte' => date('Y/m/d', strtotime($selectedPeriod->p_start)), '$lte' => date('Y/m/d', strtotime($selectedPeriod->p_end))) ]);
}
}
$reimbursements = $reimbursementQuery->all();
$dataProvider = new ArrayDataProvider([
'allModels' => $reimbursements,
]);
if($refreshData) {
return $this->renderPartial('_reports', [
'dataProvider' => $dataProvider,
]);
}
$dataProvider->pagination->pageSize = 10;
return $this->render('reports', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
'rModel' => $rModel,
'employees' => $employees,
'contacts' => $contacts,
'periods' => $periods,
]); exit;
Here's also my model:
public function getImageUrl()
{
return Url::to('#web/' . $this->attachment, true);
}
Hope somebody could point out where I missed and how to get rid of the error.
Its happen because you access none data in database or you didnt specify your function. . .
[ 'header' => '<a style="cursor: pointer;">Attachment</a>', 'format' => 'html','value' => function ($data) {
$attachment = YourModel::find($model->yourId)->one()->attachment;
return Html::img(Url::to('#web/' . $attachment, true), ['class' => 'reim-attach']);
},
],

Change the button action in Gridview based on a model attribute value Yii2

I generated a simple application by CRUD generator...
In the View page there is a action column assigned with some buttons like view, update, delete....
all I want is to create a status button....
If the status is inactive it should ask me and change the status into active and vice versa
This is my code:
'suspend' => function($url, $model) {
return Html::a(
'<span class="btn btn-xs btn-danger icon-remove bigger-80"style="margin-left:5px;"></span>',
$url,
['title' => Yii::t('app', 'Inactivate')]
);
},
'activate' => function($url, $model) {
return Html::a(
'<span class="btn btn-xs btn-success icon-ok bigger-80"style="margin-left:5px;"></span>',
$url,
['title' => Yii::t('app', 'Activate')]
);
},
Try with this..
[
'class' => 'yii\grid\ActionColumn',
'template' => '{activate}{deactivate}',
'buttons' => [
'activate' => function ($url, $model) {
if($model->status==1)
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('app', 'Activate'),
]);
},
'deactivate' => function ($url, $model) {
if($model->status==0)
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('app', 'Deactivate'),
]);
},
],
'urlCreator' => function ($action, $model, $key, $index) {
if ($action === 'acivate') {
$url =Url::toRoute(['controller/activate', 'id' => $model->id]);
return $url;
}
if ($action === 'deactivate') {
$url =Url::toRoute(['controller/deactivate', 'id' => $model->id]);
return $url;
}
}
],
You need to include use yii\helpers\Url; in your view
You May also Change Url and Icon Conditionally...:)
['class' => 'yii\grid\ActionColumn',
'buttons'=>[
'servicestatus' => function ($url, $model)
{
if($model->service_status =="Paid")
{
return Html::a(
'<span class="glyphicon glyphicon-remove red"></span>',
['service-payment/status', 'id' => $model->id],
[
'title' => 'Status',
'data-pjax' => '0',
]
);
}else
{
return Html::a(
'<span class="glyphicon glyphicon-ok green"></span>',
['service-payment/status', 'id' => $model->id],
[
'title' => 'Status',
'data-pjax' => '0',
]
);
}
},
],
'template'=>'{view} {delete} {servicestatus}',
'header'=>'<label>Action</label>',
],

Categories