Data-confirm before post in Yii2 - yii2

I have a basic crud based gii. I use modal bootstrap to view the form.
I have code like this :
if ($request->isGet) {
return [
'title' => "Cancel Incoming : $container",
'content' => $this->renderAjax('_form_cancel_incoming', [
'model' => $model,
'min_urut' => $min_urut,
'max_urut' => $max_urut,
'bundles' => $bundles,
'tanggal_real_masuk' => $tanggal_real_masuk
]),
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
Html::submitButton('Cancel', [
'data' =>[
'confirm' => "Apakah Anda Yakin Cancel ?"
],
'class' => 'btn btn-danger',
'title' => 'Cancel',
])
];
} else if ($model->load($request->post())) {
// Can I catch the data-confirm here ?
// If yes, operating the models
return [
'forceReload' => '#crud-datatable-pjax',
'title' => "Your Information",
'content' => "<h3 class='text-success'>Cancel Berhasil</h3>",
'footer' => Html::button('Close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"])
];
}
My problem is, data-confirm is show, but the operation in post still working, not wating the data-confirm.
Please advise.

Related

Yii2 gridview buttons problem (possibly after pjax)

<?= GridView::widget([
'id' => 'CompanyGrid',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'floatHeader'=>true,
'floatOverflowContainer'=>true,
'floatHeaderOptions'=>['top'=>'0'],
'pjax'=>true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
'enableReplaceState' => true,
]
],
'hover'=>true,
'toolbar' => [
'{export}',
'{toggleData}'
],
'panel' => [
'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> Companies</h3>',
'type'=>'secondary',
'before'=>Html::button('Create Company', ['value'=>Url::to('index.php?r=Company/company/create'), 'title' => 'Create Company', 'class' => 'btn btn-success', 'id' => 'modalButton']),
'after'=>false,
],
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
'CompanyID',
'CompanyName',
['class' => 'kartik\grid\ActionColumn',
'template' => '{view} {update} {delete}',
'buttons' => [
'view' => function($url, $model){
return Html::a('<span class="fa fa-eye"></span>', ['view', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'data-pjax'=>'w0',
'title' => Yii::t('yii', 'View Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'update' => function($url, $model){
return Html::a('<span class="fa fa-edit"></span>', ['update', 'id' => $model->CompanyID], [
'class' => 'activity-view-link',
'title' => Yii::t('yii', 'Edit Company: '.$model->CompanyID),
'data-toggle' => 'modal',
'data-target' => '#modal',
]);
},
'delete' => function($url, $model){
return Html::a('<span class="fa fa-trash"></span>', ['delete', 'id' => $model->CompanyID], [
'class' => '',
'data' => [
'confirm' => 'Are you absolutely sure? This action is not reversible',
'method' => 'post',
],
]);
}
],
],
],
]); ?>
In Controller
public function actionView($id)
{
$model = Company::findOne($id);
return $this->renderAjax('view', [
'model' => $model,
]);
}
I am new in yii2-advance-apps and i am trying to solve this problem. Already worked on this problem and cannot find any solution for this.
The problem is when I click on the gridview actions buttons, it works. However, after PJax, the button just don't work anymore.
However, if i reload the page, the button works again.
How to solve this problem. Thank you in advance.
'data-pjax' => 0, in action buttons options and not 'w0'

Toggle user confirmation not working

I am working on an application with user platform shown below:
Controller
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Model
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_login_id' => Yii::t('app', 'User Login ID'),
'user_password' => Yii::t('app', 'Password'),
'user_type' => Yii::t('app', 'User Type'),
'is_block' => Yii::t('app', 'Block Status'),
'is_confirmed' => Yii::t('app', 'Block Status'),
'confirmed_at' => Yii::t('app', 'Date Confirmed'),
'created_at' => Yii::t('app', 'Created At'),
'created_by' => Yii::t('app', 'Created By'),
'updated_at' => Yii::t('app', 'Updated At'),
'updated_by' => Yii::t('app', 'Updated By'),
'current_pass' => Yii::t('app','Current Password'),
'new_pass' => Yii::t('app','New Password'),
'retype_pass' => Yii::t('app', 'Retype Password'),
'admin_user' => Yii::t('app', 'Admin Username'),
'create_password' => Yii::t('app', 'Password'),
'confirm_password' => Yii::t('app', 'Confirm Password'),
];
}
View
<?php Pjax::begin() ?>
<div class="box box-primary">
<div class="box-body">
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout' => "{items}\n{pager}",
'columns' => [
'user_login_id',
'user_type',
[
'attribute' => 'created_at',
'value' => function ($data) {
return (!empty($data->created_at) ? Yii::$app->formatter->asDate($data->created_at) : ' - ');
},
],
[
'header' => Yii::t('urights', 'Confirmation'),
'value' => function ($model) {
if (!$model->is_confirmed) {
return '<div class="text-center"><span class="text-success">' . Yii::t('urights', 'Confirmed') . '</span></div>';
} else {
return Html::a(Yii::t('urights', 'Confirm'), ['confirm', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to confirm this user?'),
]);
}
},
'format' => 'raw',
],
[
'header' => Yii::t('urights', 'Block status'),
'value' => function ($model) {
if ($model->is_block) {
return Html::a(Yii::t('urights', 'Unblock'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-success btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to unblock this user?'),
]);
} else {
return Html::a(Yii::t('urights', 'Block'), ['block', 'id' => $model->user_id], [
'class' => 'btn btn-xs btn-danger btn-block',
'data-method' => 'post',
'data-confirm' => Yii::t('urights', 'Are you sure you want to block this user?'),
]);
}
},
'format' => 'raw',
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
],
],
]);
?>
</div>
</div>
<?php Pjax::end() ?>
As shown in the diagram, when I click on Confirm (Green Button), it shown disable the button and turn it to Confirmed. Then set is_confimred to 0 (zero).
Also, if I click on Block (Red Button), it should change the button caption to unblock and set is_block to 0.
However, I am not getting result, but I have the page shown below:
How do I resolve it?
You should add toggle functions for both confirm and block to the controller.
public function actionConfirm($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_confirmed = $model->is_confirmed ? false : true;
$model->update();
}
return $this->redirect(['index']);
}
public function actionBlock($id)
{
if(($model = User::findOne($id)) !== null) {
$model->is_block = $model->is_block ? false : true;
$model->update();
}
return $this->redirect(['index']);
}

Add Custom Button in Kartik Yii2 Kartik DetailView

I already search and try every forum and example.
I am using the Kartik\DetailViwew in Yii2 and I can´t manage to put a single custom button in the buttons1 option.
The code I am working on:
echo DetailView::widget([
'model' => $model,
'attributes' => $attributes,
'mode'=>Yii::$app->request->get('edit')=='t' ? DetailView::MODE_EDIT : DetailView::MODE_VIEW,
'panel'=>[
'heading'=>$this->title,
'type'=>DetailView::TYPE_INFO,
],
'buttons1' => '{update}',
'bordered' => 'true',
'striped' => $striped,
'condensed' => $condensed,
'responsive' => $responsive,
'hover' => $hover,
'hAlign'=>$hAlign,
'vAlign'=>$vAlign,
'fadeDelay'=>$fadeDelay,
'deleteOptions'=>[ // your ajax delete parameters
'params' => ['id' => $model->p_id, 'custom_param' => true],
'url'=>['delete', 'id' => $model->p_id],
]
]);
In the
'buttons1' => '{update}',
According to the example http://demos.krajee.com/detail-view, there is a way to customize.
But there is no example. And the documentation not explain how to do this.
Can anyone help?
After research and some tryings, I did it by this way, you have to enable the edit mode (to show buttons), and modify the defatul template ({buttons}{title}), as this template is replaced if the template exists in vender grideview constants, just inject your code to add any button u want. Is not the rigth solution, but is a workaround that works well
<?php
$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
'title' => 'Actualizar',
'class' => 'pull-right detail-button',
]);
?>
<?=
DetailView::widget([
'model' => $model,
'hover' => true,
'hideAlerts' => true,
'enableEditMode' => true,
'mode' => DetailView::MODE_VIEW,
'hAlign' => 'left',
'panel' => [
'heading' => '&nbsp',
'type' => DetailView::TYPE_DEFAULT,
'headingOptions' => [
'template' => "$button1 $button2 {title}"
]
],
'attributes' => [
'id',
'identificacion',
'nombre',
],
])
?>
Try it like this:
'buttons1' => Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]),
If you have more than one button, try it like this:
$button1 = Html::a('<i class="glyphicon glyphicon-trash"></i>', Url::to(['delete', 'id' => $model->id]), [
'title' => 'Eliminar',
'class' => 'pull-right detail-button',
'data' => [
'confirm' => '¿Realmente deseas eliminar este elemento?',
'method' => 'post',
]
]);
$button2 = Html::a('<i class="glyphicon glyphicon-pencil"></i>', Url::to(['actualizar', 'id' => $model->id]), [
'title' => 'Actualizar',
'class' => 'pull-right detail-button',
]);
'buttons1' => "{$button1} {$button2}",

Add more action in yii2 grid-actionColumn

I use yii2-grid made by awesome kartik.
My question is, in 'kartik]grid\actionColumn' have default action which is :
view, update, and delete.
I need to add another action, like 'print', 'email', etc.
How can I make do this.
[
'class' => 'kartik\grid\ActionColumn',
'width' => '100px',
'dropdown' => false,
'vAlign' => 'top',
'hiddenFromExport' => true,
'urlCreator' => function ($action, $model, $key, $index) {
return Url::to([$action, 'id' => $key]);
},
'viewOptions' => ['role' => 'modal-remote', 'class' => 'btn btn-xs ', 'title' => 'View', 'data-toggle' => 'tooltip'],
'updateOptions' => ['role' => 'modal-remote', 'class' => 'btn btn-xs btn-primary', 'title' => 'Update', 'data-toggle' => 'tooltip'],
'deleteOptions' => ['role' => 'modal-remote', 'class' => 'btn btn-xs btn-danger', 'title' => 'Delete',
'data-confirm' => false, 'data-method' => false, // for overide yii data api
'data-request-method' => 'post','data-toggle' => 'tooltip','data-confirm-title' => 'Are you sure?','data-confirm-message' => 'Are you sure want to delete this item'],
],
Please advise.
You should make use of the template and buttons options of the ActionColumn:
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{view} {update} {delete} {myaction}', // <-- your custom action's name
'buttons' => [
'myaction' => function($url, $model, $key) {
return Html::a('My action icon', [''my action url];
}
]
...
]

Yii 2 Export (Kartik) Menu - How can I make Dropdownlist (Exportcolumns) scrollable?

I'm using Yii2 basic and the extension kartik\export\ExportMenu.
How can I make Dropdownlist (Exportcolumns) scrollable? I have so many columns to choose, so it doesn't show me all of them when I'm working with a screen resolution of 100%. Some of them are cut off.
Dropdown columns:
<?php
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
['attribute'=>'Personen ID','value' => 'PID'],
['attribute'=>'Titel','value' => 'PTitel'],
['attribute'=>'Anrede','value' => 'PAnrede'],
['attribute'=>'Vorname','value' => 'PVorname'],
['attribute'=>'Nachname','value' => 'PNachname'],
['attribute'=>'Private Telefonnummer','value' => 'PTelefon'],
['attribute'=>'Private Mobiltelefonnummer','value' => 'PMobil'],
['attribute'=>'Privatmail','value' => 'PEmail'],
['attribute'=>'Private Webadresse','value' => 'PWebadresse'],
['attribute'=>'Firmen ID','value' => 'FID'],
['attribute'=>'Firmenname','value' => 'FName'],
['attribute'=>'Firmenbezeichnung','value' => 'FNamenszusatz'],
['attribute'=>'Ist Zentrale','value' => 'FIstZentrale'],
['attribute'=>'Beziehungs ID','value' => 'RID'],
['attribute'=>'Kommentar','value' => 'RKommentar'],
['attribute'=>'Funktion','value' => 'FUName'],
['attribute'=>'Zweitfunktion','value' => 'FUName'],
['attribute'=>'Branche','value' => 'BRName'],
['attribute'=>'Kategorie','value' => 'KName'],
['attribute'=>'Erstellt am','value' => 'RErstelltAm'],
['attribute'=>'Erstellt von','value' => 'RErstelltVon'],
['attribute'=>'Bearbeitet am','value' => 'RLetzteBearbeitung'],
['attribute'=>'Bearbeitet von','value' => 'RLetzterBearbeiter'],
['attribute'=>'Adress ID','value' => 'AID'],
['attribute'=>'Straße','value' => 'AStrasse'],
['attribute'=>'Hausnummer','value' => 'AHausnummer'],
['attribute'=>'Adresszusatz','value' => 'AAdresszusatz'],
['attribute'=>'PLZ','value' => 'APostleitzahl'],
['attribute'=>'Ort','value' => 'AOrt'],
['attribute'=>'Land','value' => 'ALand'],
['attribute'=>'Bundesland','value' => 'ABundesland'],
[ 'attribute'=>'Firmentelefon','value' => 'AFirmentelefon'],
[ 'attribute'=>'Firmen Email','value' => 'AFirmenEmail'],
[ 'attribute'=>'Webadresse','value' => 'AWebadresse'],
[ 'attribute'=>'Adresskennung','value' => 'AKennung'],
];
Export widget in view:
The class attributes in columnSelectorOptions and columnSelectorMenuOptions don't make any difference.
echo ExportMenu::widget([
'columnBatchToggleSettings'=>['label'=>'Alle Wählen'],
'dropdownOptions' =>
[
'label' => 'Exportieren',
'class' => 'btn btn-wkm'
],
'columnSelectorOptions'=>
[
'label' => 'Exportspalten',
'class' => 'btn btn-default dropdown-toggle',
'scrollable'=> true,
],
'columnSelectorMenuOptions' =>
[
'class' => 'dropdown-menu scrollable-menu',
'role'=> 'menu'
],
'dataProvider' => $dataProviderClone,
'columns' => $gridColumns,
'exportConfig' =>
[
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_PDF => false,
ExportMenu::FORMAT_EXCEL_X =>
[
'label' => 'EXCEL',
]
],
'container'=>['class'=>'btn-group pull-left', 'style'=> 'margin: 5px']
]);
?>
I found a solution :) You just have to add a style attribute to the columnSelectorMenuOptions. Changing the site.css had no impact.
'columnSelectorMenuOptions' =>
[
'style'=> 'overflow-y: scroll, height: auto;
max-height: 200px; overflow-x: hidden;',
]