Revision #241 has been created by rackycz on Oct 8, 2020, 2:34:24 PM with the memo:
Modal and ajax
« previous (#240) next (#242) »
Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
'username',
'email:email',
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'user_ajax_update_btn' => function ($url, $model, $key) {
return Html::a ( '<span class="glyphicon glyphicon-share" aria-hidden="true"></span> ',
['user/update', 'id' => $model->id],
['class' => 'openInM
yModal', 'data-m
yModal
-tTitl
e' => 'Some text']
);
},[...]
// This section can be moved to "\views\layouts\main.php"
yii\bootstrap\Modal::begin([
'header' => '<span id="myModalTitle">Title</span>',
'id' => 'm
yModalDialog
1',
'size' => 'modal-lg',
]);
echo "<div id='m
yModalContent'></div>";
yii\bootstrap\Modal::end();
$this->registerJs(
"// If you use $(document).on, it will handle also events on elements rendered by AJAX.
// $(document).on('click','a.openInM
yModal',function(e){
// And if you use $('a.openInM
yModal'), it will work only on standard elements
// $('a.openInM
yModal').click(function(e){
// Prevents the browsers default behaviour (such as opening a link)[...]
// Good idea is to set onclick='return false;' to the link if it is in a modal window
let title = $(this).attr('data-myModal
-tTitl
e');
if (title==undefined) { title = ''; }
$('#m
yModalDialog
1 #m
yModalTitle').text(title);
$('#m
yModalDialog
1').modal('show')
.find('#m
yModalContent')
.load($(this).attr('href'));
return false;
});",
yii\web\View::POS_READY,
'm
yModalHandler'
);
?>[...]
return "<script>"
. "$.pjax.reload({container:'#user-list-GridView'});"
. "$('#myModalDialog
1').modal('hide');"
. "</script>";
}
return $this->redirect(['view', 'id' => $model->id]);
}[...]