Yii v2 snippet guide

Comparing #238 with #239

Revision #239 was created by rackycz rackycz on Oct 8, 2020, 2:06:33 PM.

Modal and ajax

Content

[...]
]);
echo "<div id='modalContent'></div>";
yii\bootstrap\Modal::end();

$this->registerJs(
"
$('a.openInModal').click(function(e){  
 
  e.preventDefault();
// If you use $(document).on, it will handle also events on elements rendered by AJAX.
 
   // $(document).on('click','a.openInModal',function(e){  
 
   // And if you use $('a.openInModal'), it will work only on standard elements
 
  $('a.openInModal').click(function(e){  
 
  
 
  // Prevents the browsers default behaviour (such as opening a link)
 
  // ... but does not stop the event from bubbling up the DOM
 
  e.preventDefault(); 
 
  
 
  // Prevents the event from bubbling up the DOM
 
  // ... but does not stop the browsers default behaviour
 
  // e.stopPropagation(); 
 
  
 
  // Prevents other listeners of the same event from being called
 
  // e.stopImmediatePropagation(); 
 
  
 
  // Good idea is to set onclick='return false;' to the link if it is in a modal window
 
  
 
  let title = $(this).attr('data-modal-titl');
 
  if (title==undefined) { title = ''; }
 
  
$('#modalDialog1 #modalTitle').text('aaa'title);
$('#modalDialog1').modal('show')
.find('#modalContent')
.load($(this).attr('href'));
return false;
});",
[...]