Skip to content

Commit

Permalink
added sweetalert2
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jul 31, 2020
1 parent 7d9ffeb commit 01a3c88
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": ">=5.5.0",
"yiisoft/yii2": "*",
"kartik-v/yii2-icons": "*",
"dominus77/yii2-sweetalert2-widget": "*"
},
"minimum-stability": "dev",
"prefer-stable": false,
Expand Down
49 changes: 49 additions & 0 deletions src/assets/js/grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
jQuery("[data-delete]").on("click", function (event) {
event.preventDefault();
pjaxgrid = jQuery(this).data('pjax');
success = jQuery(this).data('success');
title = jQuery(this).data('delete');
url = jQuery(this).attr('href');
yes = jQuery(this).data('yes');
no = jQuery(this).data('no');

swal({
title: title,
type: 'warning',
reverseButtons: true,
cancelButtonText: no,
showCancelButton: true,
confirmButtonText: yes,
closeOnConfirm: false,
showLoaderOnConfirm: true,
preConfirm: () => {
return jQuery.ajax({url: url});
}
})
.then( result => {
if (!result.value) {
throw null;
}
swal({
title: success,
type: 'success',
showConfirmButton: true,
timer: 1000,
onClose: () => {
if (pjaxgrid) {
jQuery.pjax.reload({container: pjaxgrid});
} else {
window.location.reload();
}
}
});
})
.catch( error => {
if (error){
swal({
title: error.responseText,
type: 'error',
});
}
});
});
20 changes: 18 additions & 2 deletions src/grid/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ class ActionColumn extends \yii\grid\ActionColumn

public $baseUrl = "";

public $pjax = null;

public $messages = [];

/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
ActionColumnAsset::register(Yii::$app->view);
}

protected function initDefaultButtons()
{
$this->initDefaultButton('go', 'external-link-alt', ['target' => '_blank']);
$this->initDefaultButton('view', 'eye');
$this->initDefaultButton('update', 'edit');
$this->initDefaultButton('delete', 'trash', [
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'data-success' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'success', 'Success!')),
'data-delete' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'delete', 'Are you sure you want to delete this item?')),
'data-yes' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'yes', 'Yes')),
'data-no' => Yii::t('yii', ArrayHelper::getValue($this->messages, 'no', 'No')),
'data-pjax' => $this->pjax
]);
}

Expand Down
19 changes: 19 additions & 0 deletions src/grid/ActionColumnAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace dynamikaweb\grid;

use yii\web\AssetBundle;

class ActionColumnAsset extends AssetBundle
{
public $sourcePath = '@vendor/dynamikaweb/yii2-grid/src/assets';

public $js = [
'js/grid.js'
];

public $depends = [
'yii\web\YiiAsset',
'dominus77\sweetalert2\assets\SweetAlert2Asset',
];
}

0 comments on commit 01a3c88

Please sign in to comment.