Skip to content

Instantly share code, notes, and snippets.

@maximishchenko
Created August 17, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maximishchenko/feedc565eefae9e97581898ad90af931 to your computer and use it in GitHub Desktop.
Save maximishchenko/feedc565eefae9e97581898ad90af931 to your computer and use it in GitHub Desktop.
yii2_proceedGridviewCheckBoxes.js
// included script. For example js/gridViewProceedCheckBoxes.js
/**
* [proceedCheckBoxes description]
* @param {[type]} btnID [id of button]
* @param {[type]} gridID [id of GridView]
* @param {[type]} pjaxID [id of pjax-container]
* @param {[type]} emptyAlertText [text for alert "Nothing Checked"]
* @param {[type]} confirmAlertText [text for confirmation alert]
* @param {[type]} proceedURL [controller action ro proceed]
*/
function proceedCheckBoxes(btnID, gridID, pjaxID, emptyAlertText, confirmAlertText, proceedURL) {
$(btnID).click(function(){
/**
* [PosId array of checked GridView checkboxes]
* @type {array}
*/
var PosId = $(gridID).yiiGridView('getSelectedRows');
/**
* [if array of chekcboxes is empty return alert]
* @param {[array]} PosId [array of checked GridView checkboxes]
* @return {[string]} [alert notification]
*/
if (PosId=="") {
alert(emptyAlertText);
}
/**
* [if alert confirm true - send $_POST request to controller action, reload pjax-container on success]
* @param {string} confirm(confirmAlertText) [if alert proceed - send $_POST request, else - do nothing]
*/
else if (confirm(confirmAlertText)) {
$.ajax({
/**
* [data request type]
*/
type: 'POST',
/**
* [url for send]
*/
url : proceedURL,
/**
* [array of id's]
*/
data : {row_id: PosId},
/**
* [function on request success]
*/
success: function(){
/**
* [reload container on success]
*/
$.pjax.reload({container:pjaxID})
},
});
}
});
}
// in controller (for Example Soft Delete Behavior Multiple Delete)
public function actionMultipledelete()
{
/**
* [$pk get id's from $_POST request]
* @type {array}
*/
$pk = Yii::$app->request->post('row_id');
/**
* [$model get records by id]
*/
$model = Model::findAll($pk);
/**
* [delete each record]
* @param {array} $k array-key
* @return {array} $v array-value
*/
foreach ($model as $k => $v) {
/**
* run safeDelete() action for each records
*/
$v->safeDelete();
}
}
// in view file
/**
* [$proceedCheckboxes include script file]
*/
$proceedCheckboxes = file_get_contents('js/gridViewProceedCheckBoxes.js');
$this->registerJs($proceedCheckboxes);
/**
* [$multipleDelete run function with arguments from script file]
*/
$multipleDelete = <<< JS
proceedCheckBoxes("#buttonID", "#gridID", "#pjaxID", "EmptyCheckBoxesText", "ProceedConfirm", "/url/multipledelete");
JS;
$this->registerJs($multipleDelete);
/**
* [button onclick run multipledelete action]
*/
echo Html::a(Yii::t('app', '{icon} DELETE', [
'icon' => '<i class="fa fa-trash"></i>'
]
), ['#'], ['class' => 'btn btn-danger', 'id' => 'buttonID']);
/**
* [Pjax with GridView]
*/
Pjax::begin(['id' => 'pjaxID']);
/**
* [GridView widget]
*/
GridView::widget([
'id' => 'gridID',
'columns' => [],
]);
Pjax::end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment