select2 not loading in bootstrap5 remote modal the first time. second time works - yii2

I recently moved from bootstrap3 to bootstrap5. Select2 in modal stopped working. I know there has been known issue in this which was resolved using
$.fn.modal.Constructor.prototype.enforceFocus = function() {}
Or using dropdownParent option for the select 2 to bind to the modal. But this didn't work for me.
So I am loading content (including modal) via ajax and populate Select2 inside it. When I click the link first time, select2 is invisible altogether. I close the modal and click the link again, it works fine the second time. I tried dame removing tabindex as well as tabindex='false'.
Clicking first time:
closed the modal and click the second time:
What am I missing here.
my Ajax code is :
HTML: <a href='url-to-controller-returning view' class='modal-remote'>Add</a>
$(document).on('click', '.modal-remote', function (e) {
e.preventDefault();
var $modalRemote = $('#modal-remote'),
url = $(this).attr('href');
$.ajax({
url: url,
beforeSend: function (data) {
if (!$modalRemote.length) $modalRemote = $('<div class="modal fade" id="modal-remote" aria-hidden="true"><div class="modal-dialog ' + modalClass + '"><div class="modal-content"></div></div></div>');
$modalRemote.find('.modal-content').html('<div class="modal-header"><h5 id="remoteModalLabel">Loading...</h5><button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button></div><div class="modal-body"><div class="modal-remote-indicator"></div></div>');
$modalRemote.modal('show');
},
success: function (data) {
$.fn.modal.Constructor.prototype._enforceFocus = function() {};
var $dom = $(document.createElement('html'));
$dom[0].innerHTML = data;
$dom.find('link').remove();
$modalRemote.find('.modal-header > h5').html($dom.find('title').text());
$modalRemote.find('.modal-body').html($dom.html());
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$modalRemote.find('.modal-header > h5').html('Error');
$modalRemote.find('.modal-body').html(XMLHttpRequest.responseText);
}
});
});

Related

Bootstrap spinner not working before and after ajax call

I want to show/hide the boostrap spinner in div #loading before and after loading more data in the ajax infinite scroll shown below. The ajax loads data perfectly well but the spinner does not show when scrolled to the point to trigger ajax. Pls help. Helping with some code example will be great.
<div class="p-3 text-center" id="loading" style="display: none" >
<button class="btn btn-danger" type="button" disabled>
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true">Loading more items...</span>
</button>
</div>
<script type="text/javascript">
let in_ajax = false;
var flag = 20;
$(window).scroll(function() { //scroll function
if (!in_ajax) {
if ($(window).scrollTop() >= $('#result').height() - $(window).height()) {
in_ajax = true;
$.ajax({
type: 'POST',
url: 'get_results.php', //get result page
data: {
'offset': flag,
'limit': 10
},
beforeSend: function() {
$("#loading").show(); //to show spinner before ajax sends data
},
success: function(data) {
$('#result').append(data);
flag += 10; //add 10 to last offset value after callback
in_ajax = false;
},
complete: function() {
$("#loading").hide(); //to hide spinner after ajax call
}
}); //close ajax
}
}
});
</script>

how to refresh data in modal1 after modal 2 save

First, I have a list table in each row have button edit, if button edit click show modal1 where have detail from each row and have button to edit2, if button edit2 click will show modal2 after button save from modal2 been click how to refresh data in modal1
So far where all modal been close and modal1 open data have correct update, but I want just modal2 close and data in modal1 refresh without close all modal
This is my button save code function save1()
{
$('#btnSave1').text('saving...'); //change button text
$('#btnSave1').attr('disabled',true); //set button disable
var url;
if(save1_method == 'add1') {
url = "<?php echo site_url('person/ajax_add1')?>";
} else {
url = "<?php echo site_url('person/ajax_update1')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form1').serialize(),
dataType: "JSON",
success: function(data)
{
if(data.status) //if success close modal and reload ajax table
{
$('#modal_form3').modal('hide');
// $('#modal_form3').on('hidden.bs.modal', function () {
// window.alert('hidden event resfreh!');
// });
reload_table();
}
$('#btnSave1').text('save'); //change button text
$('#btnSave1').attr('disabled',false); //set button enable
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave1').text('save'); //change button text
$('#btnSave1').attr('disabled',false); //set button enable
}
});
}

Chrome doesn't submit form when using AngularJS directive on submit button

I've written an AngularJS directive, shown below, which disables a HTML button on form submit, or when the button is clicked, and replaces the button's inner text. This works perfectly in IE and Firefox but in Chrome the code to modify the button executes successfully however the form won't submit, and no request is made to the server.
An example of the HTML button:
<button submit-button submit-title="Generating Report..."
type="submit" class="btn btn-primary">
<i class="fa fa-database mr-xs"></i>Generate Report
</button>
The AngularJS directive:
(function () {
function submitButton() {
return {
restrict: 'A',
require: '^form',
scope: {
submitTitle: '#'
},
link: function ($scope, $element, $attributes) {
$element.on('click', function () {
var element = $element[0];
element.innerHTML = element.innerHTML
.replace(element.innerText, $attributes.submitTitle);
element.disabled = true;
$element.addClass('disabled');
});
}
};
}
angular.module('adminApp', [])
.directive('submitButton', submitButton);
})();
I've used Fiddler to review the requests and responses and can confirm there is no request made in Chrome when the button is clicked or form submitted. Any help would be appreciated.
Chrome won't submit the form if you in the attached click event handler disable the element.
Use the submit event instead:
$element.on('submit', function() {
// Do stuff
});

Redirect a user when box is closed

I have a javascript popup context menu with a button below it with close. That button when pressed close will continue to the next page. If a user dont click the close button and decides to click outside the popup box it will not redirect the user. What function for javascript can i add if a user click outside the box it will redirect them without clicking the close button inside the box. using a osx-modal-content plugin
How its triggered*
<input type='button' name='osx' value='Click Here To Enter The Website!' class='osx demo'/></a>
plugin page (link)
OSX STYLE DIALOG ** OSX STYLE DIALOG ** OSX STYLE DIALOG **
http://www.ericmmartin.com/projects/simplemodal-demos/
This is something that i have for the close function**
close: function (d) {
var self = this; // this = SimpleModal object
d.container.animate(
{top:"-" + (d.container.height() + 20)},
500,
function () {
self.close(); // or $.modal.close();
}
This is how you can do stuff on close:
$("#element-id").modal({
onClose: function () {
window.location.href = "http://stackoverflow.com";
}
});
If you had an element added like:
// Load dialog on click
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content').modal();
return false;
});
Change that to
// Load dialog on click
$('#basic-modal .basic').click(function (e) {
$('#basic-modal-content').modal({
onClose: function () {
window.location.href = "http://stackoverflow.com";
}
});
return false;
});

jQuery dynamically load page and unload javascript on exit

I am trying to build a site that dynamically loads the pages. Certain pages have extra javascript that they load. I need to be able to unload this javascript when the page changes. Is that possible? My code so far (work in progress still):
//When page loads...
$("ul.toggle li:first").addClass("active").show(); //Activate first tab
var taburl = $("ul.toggle li:first a").attr("href").substr(1);
$(document).ready(function(e) {
$.ajax({
type: "POST",
url: taburl+'.php',
data: '',
dataType: "html",
success: function(d){
$('#main').html(d);
}
});
});
//On Click Event
$("ul.toggle li").click(function() {
$("ul.toggle li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$("#main").fadeOut(500);
$("#main").html('');
var activeTab = $(this).find("a").attr("href").substr(1) + '.php'; //Find the href attribute value to identify the active tab + content
$("#main").load(activeTab).fadeIn(500); //Fade in the active ID content
});
Let me know if any other info is needed.