I want each of my modal to have a unique ID. But what I am actually doing is giving me:
Syntax error, unrecognized expression: #myModal(7)
Below is my code:
<tbody class="tbodyBookTable">
#foreach (var item in Model)
{
<tr class="trBookTable">
<th>#Html.DisplayFor(modelItem => item.Title)</th>
<th>#Html.DisplayFor(modelItem => item.Author)</th>
<th>#Html.DisplayFor(modelItem => item.DateRented)</th>
<th>#Html.DisplayFor(modelItem => item.ReturnDate)</th>
<th>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal(#Html.DisplayFor(modelItem => item.BookID))">Yes</button>
<!-- Modal -->
<div class="modal fade" id="#myModal(#Html.DisplayFor(modelItem => item.BookID))" 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>
<h4 class="modal-title">Return confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to return this book?.</p>
<p>Book title:#Html.DisplayFor(modelItem => item.Title)</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</th>
</tr>
}
</tbody>
As you can see, I am setting my data-target as
#myModal(#Html.DisplayFor(modelItem => item.BookID))
and the id of my model in the same way. I don't understand why I am getting this error. Any help please?
Remove the hashtag symbol (#) from the id declaration:
<div id='myModal(' + #Html.DisplayFor(item => item.BookID) + ')'></div>
The data-target uses the hashtag symbol because it is then placed directly into jQuery to find the element. When writing the id of an HTML element, you do not write the # symbol.
You aren't building the id correct. Use these:
<div id='myModal(' + #Html.DisplayFor(item => item.BookID) + ')'></div>
Since # can be conflictive, don't use in elements id.
Related
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);
}
}
}
I'm building a page that's supposed to display entries from a database in the form of textboxes, and each has a button that makes a call to the controller to delete that entry, based on its Id key. My boss then wanted me to add a modal to warn the user - your typical "Do you really want to...?"
To do so, I need to generate a modal codesnippet for each entry, and assign the relevant ids and data-targets automatically.
My current design looks like this:
#model System.Data.DataRow
<div class="input-group my-1">
<input type="text" class="form-control border-secondary tinybox" value="#Model[1]" name="ArtCategory">
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary tinybox py-0" name="CatDelete" value="#Model[0]" data-toggle="modal" data-target="#("#" + Model[0] + "DeleteCategoryModal")">
<i class="fas fa-times"></i>
</button>
</div>
</div>
<div class="modal fade" id="#(Model[0] + "DeleteCategoryModal")">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Vill du verkligen radera denna kategori?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Kategorinummer: #Model[0]
</div>
<!-- Modal footer -->
<div class="modal-footer">
#using (Html.BeginForm("DeleteCategory", "Backoffice", FormMethod.Post, new { id = Model[0] }))
{
<button type="submit" id="DeleteCategory" Name="CatDelete" value="#Model[0]" class="btn btn-dark">Bekräfta</button>
<script>
$('#DeleteArticle').click(function () {
$('#DeleteArticleModal').modal('hide');
});
</script>
}
<button type="button" class="btn btn-dark" data-dismiss="modal">Avbryt</button>
</div>
</div>
</div>
</div>
This code is submitted as a partialview and rendered a number of times. Model[0] is a numerical value. The idea is that the modals' id codes will be "3DeleteCategoryModal" for deleting category 3, etc.
The problem? The modals won't open. I click a button, nothing happens. For whatever reason, it can't find the modal segment with the right name. Curiously, when I wiew the compiled source code, it looks just fine:
<div class="col-12 px-1">
<div class="input-group my-1">
<input type="text" class="form-control border-secondary tinybox" value="Stuff" name="ArtCategory">
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary tinybox py-0" name="CatDelete" value="3" data-toggle="modal" data-target="#3DeleteCategoryModal">
<i class="fas fa-times"></i>
</button>
</div>
</div>
<div class="modal fade" id="3DeleteCategoryModal">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Vill du verkligen radera denna kategori?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Kategorinummer: 3
</div>
So. How do I make this work?
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;}
I've got two Bootstrap 3 modals as below. One modal is switching to another - and there is a button - "Go back" to previous modal. Unfortunatelly modals are long and when I'm switching back - scrolling works but for website, not for previous modal. Anyone knows how to fix it?
I'm using the default bootstrap css and js files, google chrome
Complete HTML code as below:
<head>
<!-- scripts: jquery.min.js; bootstrap.min.js css: bootstrap.min.css -->
</head>
<body>
<button type="button" data-toggle="modal" data-target="#addOffer">Add offer</button>
<button type="button" data-toggle="modal" data-target="#viewOffer">View offer</button>
first modal:
<div class="modal fade" tabindex="-1" id="viewOffer" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">View offer</h4>
</div>
<div class="modal-body">
lots of text about offer here:
<button type="button" class="btn btn-info" data-dismiss="modal" data-toggle="modal" data-target="#addOffer">Go back</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
second modal:
<div class="modal fade" id="addOffer" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Add offer</h4>
</div>
<div class="modal-body">
lots of forms for new offer here:
<input id= "see_offer" name="see_offer" type="submit" class="btn btn-info" value="View offer" data-dismiss="modal" data-toggle="modal" data-target="#viewOffer" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Add .modal { overflow-y:auto; } to your css file.
The regular data-toggle:"modal" button will do, as suggested, toggle the modal visibility. The buttons in your page may remain so. However, you seem to want your modal buttons to hide one modal and show another, so set a function to them manually like so:
$("[hide-modal]").click(function () {
$(this).attr("hide-modal").modal("hide");
$(this).attr("display-modal").modal("show");
});
Then in your modal buttons:
<button type="button" class="btn btn-info" hide-modal="#viewOffer" display-modal="#addOffer">Go back</button>
<button type="button" class="btn btn-info" hide-modal="#addOffer" display-modal="#viewOffer">View offer</button>
I want a model to open when I click on a button in a web-grid. I have created a button which targets a modal by its ID. For each item in the model(a list), I want to create a modal with the id of the item. Below is my code:
<div id="divCurrentRented">
#{
WebGrid obj = new WebGrid(Model, rowsPerPage: 5);
}
#obj.Table(htmlAttributes: new
{
id="tableCurrentRented",
#class = "table"
},
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
columns: obj.Columns(
obj.Column("Title", header: "Title"),
obj.Column("Author", header: "Author"),
obj.Column("Avaible", header: "Available", canSort:false),
obj.Column(header: "Rent", format:#<button type="button" class="btn btn-default" data-toggle="modal" data-target="##item.BookID">Yes</button>)
))
As you can see the data target of the button is equal to the id of the item.
my modal code as below:
<div class="modal fade" id="#Model.First().BookID" ( 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>
<h4 class="modal-title">Return confirmation</h4>
</div>
<div class="modal-body">
<p class="modalBody">Are you sure you want to return this book?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
As you can see in the first line, I am fetching the bookID of the first item. What I need is to fetch the id for each item of the list so that each button click would open its corresponding modal. Any idea how to do that?
To pass information from page to modal while staying to same page, you can do with bootstrap modal event listener show or shown and pass the data attributes value to modal
e.g to pass id and title to modal as data attributes inside modal trigger button.
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" data-id="123" data-title="I'm First Modal Title">Open Modal</button>
and use event property relatedTarget to get the data-attribute value e.g id inside modal event listener
var id = $(e.relatedTarget).data('id');
and pass it to modal e.g to <div class="modal-body"></div>
$(".modal-body").html(id);
example with id and title as data attributes passing to modal
$('#myModal').on('show.bs.modal', function (e) {
var id = $(e.relatedTarget).data('id');
var title = $(e.relatedTarget).data('title');
//pass date-title to modal header
$(".modal-title").html(title);
//Pass Id to modal body or any element in modal
$(".modal-body").html(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" data-id="123" data-title="I'm First Modal Title">Open Modal</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal" data-id="456" data-title="I'm Second Modal Title">Open Modal</button>
<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" aria-label="Close"> <span aria-hidden="true">×</span></button>
<h4 class="modal-title" aria-labellbyid="writeLabel"></h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer" id="edit-footer">
<input type="submit" data-dismiss="modal" class="btn btn-success" value="Submit">
</div>
</div>
</div>
</div>
Fiddle