This related to my other question https://forum.yiiframework.com/t/pass-pjax-id-to-modal-javascript/126740/2 but I thought I’d ask thing independently.
I have an index file listing all the projects with a Create button above the Pjax Gridview which is used to display the create view in a modal. If they update the modal and save, it commits the data and reload the pjax container.
The issue is, the first time they use the modal, all is good. However, subsequent call generate an increasing number of update requests. Seems to keep all the previous requests in memory and reissue them.
Now the solution I found, https://forum.yiiframework.com/t/solved-pjax-in-remote-modal-cause-multiple-request/82621, is to use a unique pjax container id in the index using uniqid(), but the issue I’m having with this is that then the modal form’s pjax.reload reloads the entire page and not just the pjax container.
What is the way to handle this situation? Is the uniqid the solution, if so, how to make the reload work properly? If not, how to avoid the multiple request issue?
Thank you for your help in advance,
Below is the code involved
My index code looks like
<div class="projects-index">
<p><div id="message"></div></p>
<p>
<?= Html::button('<i class="glyphicon glyphicon-plus"></i> '.Yii::t('app', 'New Booking'),
[
'id'=>'modalCreateButton',
'class' => 'btn btn-success modalButton',
'value'=>Url::to('index.php?r=projects/create'),
'title'=>Yii::t('app', 'New Booking'),
])
?>
</p>
<div id="gridContainer">
<?php
$pjaxContainerName = 'pjaxContainer_';
Pjax::begin([
'id' => $pjaxContainerName,
'enablePushState' => false,
// 'enableReplaceState' => false,
'options' => [
'data-pjax' => true,
'class' => 'pjaxProjectsContainer'
],
'scrollTo' => false,
'timeout' => 5000,
]); ?>
<?= GridView::widget([
'id' => 'grid',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
my js for the Create button is
$('.modalButton').click(function () {
$('#loading').show();
$('#modal').modal('show')
.find('#modalContent')
.load($(this).attr('value'));
document.getElementById('modalHeaderTitle').innerHTML = '<h4>' + $(this).attr('title') + '</h4>';
$.fn.modal.Constructor.prototype.enforceFocus = function() {}; //Fix for Select2 modal issue.
return false; //Stop default link reloading of page
});
and my modal is defined as
Modal::begin([
'id'=>'modal',
'class'=>'modal',
'size'=>'modal-lg',
'headerOptions' => ['id' => 'modalHeader'],
'header' => '<span id="modalHeaderTitle"></span>',
'closeButton' => [
'label'=>'Cancel',
'id'=>'close-button2',
'class'=>'btn btn-warning pull-right',
'title'=>'Close without saving',
],
'clientOptions' => [
'backdrop' => 'static', // true,
'keyboard' => false, // true,
]
]);
echo '<div id="modal-system-messages"></div>';
echo '<div id="passInfo" style="opacity: 1; display: none"><input type="hidden" name="pjaxId" id="pjaxId" value="'.$pjaxContainerName.'"/></div>';
echo "<div id='modalContent'></div>";
Modal::end();