I am using bootstrap 4 modal, when I click on "Ok" button I call function and after that the modal disappear, but the back-ground still remains. whereas for the close button the modal and the back-ground dissapers its because I am using "data-dismiss="modal", but if I use this same for "Ok" button this is not working. I have used (data-backdrop="false"), but it completely removes the back-ground which i donot want. so can someone please tell me how to do that. I have uploaded the following code.
<div class="modal" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{title}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>{{question}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" (click)="onConfirm($event)">Ok</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
.ts File
export class ModalComponent {
#Input() question: string = ''
#Input() title: string = ''
#Output() confirm = new EventEmitter<void>()
constructor() { }
onConfirm(event: Event) {
event.stopPropagation()
this.confirm.emit()
}
}
Related
I have a button with some validation function written in click event of jquery, which needs to trigger a modal window on successful validation.I have placed my modal trigger function within an IF condition which is not triggering my modal window. Can you please help me to find the issue and solve.
Note: when placing my modal trigger function on the very first line within the document.ready function the same modal window works, but not triggering when placed in click function.
Header and html code of modal
<head>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
</head>
<div id="submit_modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
jquery code for click function
$(".subdomain_check").click(function(){
var form = $("#msform");
form.validate();
if(form.valid() === true){
var doamin_check = $("#subdomain_input").val();
if (doamin_check == '' ) {
$("#subdomain_available").text("");
$("#subdomain_error").text("Please Enter a domain name");
return false;
}
else {
$("#submit_modal").modal('show'); // Modal triger function
$("#subdomain_error").text("");
$("#subdomain_available").text("Domain Available");
current_fs = $(this).parent();
next_fs = $(this).parent().next();
}
}
});
I have an array drivers in my component. I'm showing a ngx-bootstrap modal with a value fullname from the drivers array when i click a button in the component. Now i want to get the data back from the modal like which name is selected. How to do that?
<button type="button" class="btn btn-success btn-round" (click)="staticModal.show()">{{'Assign Driver' | translate}}
</button>`
<div class="modal fade" bsModal #staticModal="bs-modal"
tabindex="-1" role="dialog" aria-labelledby="dialog-static-name">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h4 id="dialog-static-name" class="modal-title pull-left">Select Drivers</h4>
<button type="button" class="close pull-right" aria-label="Close" (click)="staticModal.hide()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div *ngFor= "let dri of drivers">
<input type="radio" id="customRadio1" name="customRadio">
<label>{{dri.fullname}}</label>
</div>
<div class = "modal-footer">
<button type="button" class="btn btn-primary" (click)="staticModal.hide()" >Select Driver</button>
<button type="button" class="btn btn-default" (click)="staticModal.hide()">Cancel</button>
</div>
</div>
</div>
</div>
</div>
I want to get value of the selected radio button in the component. And when the modal is closed i want to perform the method placeorder() which i already How to do that?
open() {
const modalRef = this.modalService.open(NgbdModalContent);
modalRef.componentInstance.data = //'your data want to sent in model';
modalRef.result.then((response) => {
console.log(response);
});
In Model component
#input() data : any;
closel() {
let data = [1,2,3,4,5,6];
this.activeModal.close(data);
}
}
}
The this.obj data is populated after modal window load. So, how can I sync both data and modal?
Acomponent.ts
this.dialog.showModal(this.referenceNumber);
dialogComponent.ts
export class DialogComponent {
obj:any;
showModal(name:String) {
if(name!==null) {
this.obj=name;
$("#myModal").modal('show');
}
}
hideModal():void {
document.getElementById('close-modal').click();
}
errorModal() {
$("#errorModal").modal('show');
}
}
dialog.html
<div id="myModal" class="modal fade" role="dialog" >
<div class="modal-dialog ">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>Record saved Successfully{{obj}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
The Angular approach is to control visibility of the modal via the javascript.
So something like:
<div *ngIf="showModal" id="myModal" class="modal fade" role="dialog" >
<div class="modal-dialog ">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" (click)="showModal=false">×</button>
</div>
<div class="modal-body">
<p>Record saved Successfully{{obj}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Note the *ngIf="showModal" and (click) event handler on the button.
Your .ts file would need a showModal: boolean; property, and the button click handler would need run change detection if you are using push change detection.
Edit:
And yes, don't use jQuery in Angular. I recommend the ng-bootstrap library.
<div class="modal fade in" id="acc-del-form" role="dialog" style="background: rgba(0, 0, 0, 0.55);" [ngStyle]="{'display':display}" data-keyboard="false" data-backdrop="false">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header" style="background: #05668D; color:#fff">
<button type="button" class="close" data-dismiss="modal" (click)="onCloseDeleteTagging()">×</button>
<h4 class="modal-title">Update!</h4>
</div>
<div class="modal-body">
<h5>Do you want to update tags</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-info btn-fill btn-sm" data-dismiss="modal" (click)="updateTaggingData()">Yes</button>
<button type="button" class="btn btn-default btn-warning btn-fill btn-sm" data-dismiss="modal" (click)="onCloseDeleteTagging()">No</button>
</div>
</div>
</div>
</div>
This is my above modal popup it closes when i click on onCloseDeleteTagging() method
onCloseDeleteTagging() {
this.display = 'none';
}
i want to close the modal pop to close when i click anywhere on the screen rather than clicking "X" button
<div class="pull-right" style="padding-top:15px;" [ngStyle]="{'display':TaggingUserDisplay}">
<button type="button" title= "Save Tags" class="btn btn-warning btn-fill btn-xs" id="save_btn" (click)= "updateTaggingDataPopup()" [ngStyle]="{'display': 'updateTagBoList.length !== 0 '}"><i class="fa fa-save"></i></button>
</div>
this is my code to open modal popup
updateTaggingDataPopup() {
this.display = "block";
}
can any one help me in closing the popup when i click anywhere on the screen.
You need to use #HostListener (HostListener Docs)
#HostListener('document:click', ['$event'])
clickOnDocument(event) {
if(SOME_TEST_DO_YOU_WANT_TO_MAKE_PROBABLY_WITH_THE_event_TARGET){
this.display = 'none';
}
}
Maybe this helps, it's made with vanilla JS.
document.addEventListener('click', (evt) => {
target = evt.target;
var modalContainer = document.getElementById('acc-del-form');
var modal = document.getElementById('modal_block');
if (target !== modal) {
modalContainer.style.display = "none"
}
})
When clicking anywhere outside the modal block it will remove the display attribute.
I have 2 modals that are triggered by selection of a drop down menu. If I have the "multi-part-delete modal" first and the "multi-part-move" modal second then the "multi-part-move" one does not show. The screen goes dark but the modal does not appear.
If I swap them around then the move one works and the delete one doesn't show. Anyone know how I can get them both to work.
<!-- Multi-Part Delete Modal -->
<div class="modal fade" id="multipartdeletemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Delete Selected Parts?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<i data-icon="ei-trash" data-size="l" class="trash"></i>
<h5>Are you sure you want to delete these parts?<br>
This process cannot be undone</h5>
<form action="{% url 'multi-delete' project.id %}" method="post" id="multidelete">{% csrf_token %}</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button form="multidelete" type="submit" value="Submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
<!-- Multi-Part Move Modal -->
<div class="modal fade" id="multimovemodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Move Selected Parts?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<i data-icon="ei-trash" data-size="l" class="trash"></i>
<h5>Please select group?</h5>
<form action="{% url 'multi-delete' project.id %}" method="post" id="multimove">{% csrf_token %}</form>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button form="multimove" type="submit" value="Submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
use this IIFE function and you will have mulitple stackable modal support on bootstrap 4
(function multiple_modal() {
$(document).on('show.bs.modal', '.modal', function () {
var zIndex = 1040 + (10 * $('.modal:visible').length);
$(this).css('z-index', zIndex);
setTimeout(function () {
$('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
}, 0);
});
$(document).on('hidden.bs.modal', '.modal', function () {
$('.modal:visible').length && $(document.body).addClass('modal-open');
});
})()
Yes, stacked modals are currently not working in Bootstrap 4.1. What I did was make them sequentially close/open each other. For example, when a child("stacked") modal opens from a parent modal, the parent modal closes, and when a child modal closes, it re-opens a parent(previously opened) modal - so there's always one modal open at a time.
On the child modal, I have a data attribute data-parent-modal="#parentExampleModal"
I have the Javascript as below:
$('.modal').on('shown.bs.modal', function (e) {
let $this = $(this),
$parentModal = $this.parent().find($this.data('parent-modal') + '.modal');
if ($parentModal.length) {
$parentModal.modal('hide');
$this.on('hidden.bs.modal', function (e) {
$parentModal.modal('show');
});
}
});
If you need further explanation, please let me know.
You could do it with CSS by selecting the second modal
.modal:nth-of-type(even) {z-index: 1052 !important;}
.modal-backdrop.show:nth-of-type(even) {z-index: 1051 !important;}