Bootstrap how to show data from row in modal window - html

Want to show modal window in the center of webbrowser and show user information from my bootstrap td row which user clicked. Can you help me out? Below find my current code. I can't find the way
Here below you see i made tr as modal and put data-id to 1:
....
<tbody>
#For Each item In Model.Transports
Dim currentItem = item
#<tr data-toggle="modal" data-id="1" data-target="#orderModal">
<td class="col-lg-4 text-center vert-align_td">#item.Name</td>
<td class="col-lg-4 text-center vert-align_td">#item.Surname</td>
<td class="col-lg-4 text-center vert-align_td">#item.Age</td>
....
Heres my modal window where i want to put all information:
#<div id="orderModal" class="modal hide fade" role="dialog" aria-labelledby="orderModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
Information for lp number:
Name:
Surname:
Age:
</div>
<div id="orderDetails" class="modal-body"></div>
<div id="orderItems" class="modal-body"></div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
and here is my JS:
$(function () {
$('#orderModal').modal({
keyboard: true,
backdrop: "static",
show: false,
}).on('show', function () {
var getIdFromRow = $(event.target).closest('tr').data('Lp');
//make your ajax call populate items or what even you need
$(this).find('#orderDetails').html($('<b> Information for LP: ' + getIdFromRow + '</b>'))
});
});
EDITED (for further duscussions:)
...
#For Each item In Model.Transports
Dim currentItem = item
#<tr data-toggle="modal" data-target="#myModal" data-id="12">
<td class="col-lg-1">#item.Lp</td>
<td class="col-lg-1">#item.Name</td>
Modal:
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body" id="orderDetails">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
JS:
$('#myModal').on('show.bs.modal', function (event) {
//var getIdFromRow = $(event.relatedTarget).attr('data-id');
// if you wnat to take the text of the first cell
var getIdFromRow1 = $(event.relatedTarget).find('td:first-child').text();
var getIdFromRow2 = $(event.relatedTarget).find('td:second-child').text();
$(this).find('#orderDetails').html($('<b> Information for LP: ' + getIdFromRow1 + '</b>'))
$(this).find('#orderDetails').html($('<b> Information for DataPrzyjazdu: ' + getIdFromRow2 + '</b>'))
});
EDIT2:
$('#myModal').on('show.bs.modal', function (event) {
var getIdFromRow1 = $(event.relatedTarget).find('td:first-child').text();
$(this).find('#orderDetails').html($('<b> Information for Lp: ' + getIdFromRow1 + '</b>'))
var getIdFromRow2 = $(event.relatedTarget).find('td:nth-child(2)').text();
$(this).find('#orderDetails').html($('<b> Information for Data przyjazdu: ' + getIdFromRow2 + '</b>'))
});
Modal:
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body" id="orderDetails">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
table:
#<tr data-toggle="modal" data-target="#myModal" data-id="12">
<td class="col-lg-1 text-center vert-align_td">#item.Lp</td>
<td class="col-lg-1 text-center vert-align_td">#item.DataPrzyjazdu</td>

You have some problems with your code:
You should use show.bs.modal event instead of show event. docs.
You should use $(event.relatedTarget) (return the tr) instead of $(event.target) (return the modal).
$('#myModal').on('show.bs.modal', function (event) {
//var getIdFromRow = $(event.relatedTarget).attr('data-id');
// if you wnat to take the text of the first cell
var getIdFromRow = $(event.relatedTarget).find('td:first-child').text();
$(this).find('#orderDetails').html($('<b> Information for LP: ' + getIdFromRow + '</b>'))
});
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
<tr data-toggle="modal" data-target="#myModal" data-id="12">
<td>ID is 12</td>
<td>Col 2</td>
<td>Col 3</td>
<td>Col 4</td>
</tr>
</table>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body" id="orderDetails">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Related

i want to get the id of the clicked user and delete it when i click yes on bootstrap modal

this is the html file, I want to get the id of the particular student click on and delete the student using the popup modal
<div style="margin-top: 50px;">
<table datatable="ng" class="row-border hover" [dtOptions]="dtOptions" [dtTrigger]="dtTrigger">
<thead>
<tr>
<th>S/N</th>
<th>Full Name</th>
<th>Class</th>
<th>Sex</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of students; let i = index">
<td>{{i+1}}</td>
<td><a [routerLink]="['/view-student', student.id]" >{{student.lastName + ' ' + student.firstName}}</a></td>
<td>{{student.grade.name}}</td>
<td>{{student.sex.name}}</td>
<td><a [routerLink]="['/edit-student', student.id]" class="btn btn-primary" >Edit</a></td>
<td><a data-toggle="modal" data-target="#myModal" class="btn btn-danger">Delete</a></td>
</tr>
</tbody>
</table>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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" id="myModalLabel">Warning</h4>
</div>
<div class="modal-body">
Are you sure you want to delete..
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" (click)="" data-dismiss="modal" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
</div>
this is the delete function I have in the component to use for deleting the student I click on. thank you for your help in advance
delete(id){
this.studentService.delete(id).subscribe(() => {
this.students.splice(id, 1);
this.loadAllStudents();
});}
Simply save a current student id in component property.
html(table):
...
<td><a (click)="setId(student.id)" data-toggle="modal" data-target="#myModal" class="btn btn-danger">Delete</a></td>
...
ts:
public id = ''
setId(id) {
this.id = id
}
html(modal):
...
<button type="button" (click)="delete(id)" data-dismiss="modal" class="btn btn-primary">Yes</button>
...

Bootstrap Modal: Content from html page does not show within the modal

I have a created a button when you click it a modal appears.
Here is the code:
<li style="float: right;">
<button id="myBtn" data-target="#myModal" ng-click="printDivModal('rollup-tab')">Modal Test</button>
</li>
In the js, the ng-click should open a model which contains data from stackModal.html
Here is the code in the js file:
$scope.printDivModal = function(divName) {
console.log('opening pop up');
var ModalInstance = $uibModal.open({
//animation: $ctrl.animationsEnabled,
templateUrl: 'app/views/modals/stackedModal.html',
size: 'lg',
/*
controller: function($scope) {
$scope.name = 'top';
}
*/
});
}
Here is the code for the model (stackedModal.html):
<!-- Modal -->
<div class="modal fade" id="myModal" 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">The Modal Header</h4>
</div>
<div class="modal-body">
<p>Text inside the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
When I click the Model-Test button, all I see is a small white rectangle show on the screen. Nothing appears from the stackedModal html page. Why is this? Thanks.
Just remove modal wrappers div's, bootstrap-modal does it for you.
You just need to put modal content:
stackedModal.html:
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">The Modal Header</h4>
</div>
<div class="modal-body">
<p>Text inside the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

Bootstrap 3 nested modals – backdrop z-index and close modal on click

I’m working on a web application (Asp.Net MVC 5) where the user has to crate many data records with mappings to other records. The user must be able to create a mapping to an existing or newly created record.
A typical case would be the creation of an address:
User opens the site to create an address
He enters the normal fields like street and street no etc.
He can select an existing city (in which the address is located) from a table
If the required city does not already exist he can create a new city
… For the city, he can select a country or create on (same as for address and city)
The selection is implemented using bootstrap modals.
Pressing on a select button (like select city) shows a modal containing a table showing all the selectable records in a table.
The modal with the table contains an add new button to add new entities, which opens another modal.
To be able to reuse the views I’ve split the code in partial views like this:
_CreateAddress => Contains only the form to create an address
_CreateAddressModal => A modal containing _CreateAddress
_SelectAddressModal => A modal containing a table to showing all addresses. The modal contains also _CreateAddressModal which will be displayed when the user clicks on the add new button
_CreateCity => Contains only the form to create a city
_CreateCityModal => A modal containing _CreateAddressModal
_SelectCityModal => A modal containing a table to showing all cities. The modal contains also _CreateCityModal which will be displayed when the user clicks on the add new button
…and so on
I’ve two problems:
Clicking on a backdrop does not close the open modal
The backdrop of the topmost modal has a lower z-index than the all other modals and does therefore not hide the other modals
I’ve tried to set the z-index of the backdrops on modal show, to make sure they hide the other modals but this didn’t work => the backdrops get displayed above the current modal.
Here is a fiddle showing the problem: jsFiffle
SO does not let me link to a JSFiddle without adding the code to the question, so here is the code:
HTML:
<!-- Create Address -->
<div id="CreateAddress" class="modal fade fullScreen" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Create Address</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Placeholder</strong> Here is the form to create an address
<button class="btn btn-default" id="selectCityButton">select city</button>
</div>
<button class="btn btn-primary">OK (not implemented)</button>
<button class="btn btn-default modalCloseButton"
data-modalid="CreateAddress">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Select City -->
<div id="SelectCity" class="modal fade fullScreen" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Select a City</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Placeholder</strong> Here is a table to select an existing city
</div>
<button class="btn btn-success btn-block" id="createCityButton">Create new city</button>
<button class="btn btn-primary">OK (not implemented)</button>
<button class="btn btn-default modalCloseButton"
data-modalid="SelectCity">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Create City -->
<div id="CreateCity" class="modal fade fullScreen" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Create City</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Placeholder</strong> Here is the form to create a city
<button class="btn btn-default" id="selectCountryButton">select country of city</button>
</div>
<button class="btn btn-primary">OK (not implemented)</button>
<button class="btn btn-default modalCloseButton"
data-modalid="CreateCity">Cancel</button>
<!-- Select Country -->
<div id="SelectCountry" class="modal fade fullScreen" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Select a Country</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Placeholder</strong> Here is a table to select an existing country
</div>
<button class="btn btn-success btn-block" id="createCountryButton">Create new country</button>
<button class="btn btn-primary">OK (not implemented)</button>
<button class="btn btn-default modalCloseButton"
data-modalid="SelectCountry">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Select Country END -->
<!-- Create Country -->
<div id="CreateCountry" class="modal fade fullScreen" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<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">Create Country</h4>
</div>
<div class="modal-body">
<div class="alert alert-info">
<strong>Placeholder</strong> Here is the form to create a country
</div>
<button class="btn btn-primary">OK (not implemented)</button>
<button class="btn btn-default modalCloseButton"
data-modalid="CreateCountry">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Create Country END -->
</div>
</div>
</div>
</div>
<button class="btn btn-primary btn-block" id="openModal">Open Modal</button>
JS:
var zIndex = 10000;
// Displys the given modal on top of everything else
// modal: The modal to display as jQuery object
// Does not work => remove thw first two lines to see the problem
function displayModalOnTop(modal) {
// remove me
modal.modal('show');
return;
// end remove
zIndex += 2;
modal.css('z-index', zIndex);
modal.modal('show');
setTimeout(function () {
$('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
}, 0);
}
$(function(){
$('#openModal').on('click', function(){
displayModalOnTop($('#CreateAddress'));
});
// generic close modal handler
$('.modalCloseButton').on('click', function(){
var $this = $(this);
var modalId = $this.attr('data-modalid');
$('#' + modalId).modal('hide');
})
// Select city click
$('#selectCityButton').on('click', function(){
displayModalOnTop($('#SelectCity'));
});
// Create city click
$('#createCityButton').on('click', function(){
displayModalOnTop($('#CreateCity'));
});
// Select country click
$('#selectCountryButton').on('click', function(){
displayModalOnTop($('#SelectCountry'));
});
// Create country click
$('#createCountryButton').on('click', function(){
displayModalOnTop($('#CreateCountry'));
});
});
CSS:
.modal.fullScreen {
padding-left: 0 !important;
padding-right: 0 !important;
margin-top: 48px;
}
.modal.fullScreen .modal-content {
height: auto;
min-height: 100%;
border-radius: 0;
}
.modal.fullScreen .modal-body.noPadding {
padding: 0;
}
.modal.fullScreen .modal-dialog {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.modal.fullScreen .modal-header {
background-color: #3276b1;
color: #fff;
}
.modal.fullScreen .modal-header .close {
color: #FFF;
text-decoration: none;
cursor: pointer;
opacity: 0.4;
font-size: 30px;
}
.modal.fullScreen .modal-header .close:hover {
opacity: 1 !important;
}
The problem was that the modal covered the hole screen. The backdrop was visible due to the margin of the modal but was not clickable because it was totally covered by the modal.
Removing the margin from the modal and adding it to the modal dialog fixed the problem.
Rather then writing this much of code on your own and maintaining everything by your self what you can do is simply given in below snippet.
function simpleSHow(title)
{
var msg=$('#simple-div');
BootstrapDialog.show({
title : title,
message: $('#simple-div'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
function complexSHow(title)
{
var msg=$('#complex-div');
BootstrapDialog.show({
title : title,
message: $('#complex-div'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
function showDiv1(div_id)
{
var msg=$('#lavel-2_div-1');
BootstrapDialog.show({
title : 'Level 2 Title',
message: $('#lavel-2_div-1'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
function showDiv2(div_id)
{
var msg=$('#lavel-2_div-2');
BootstrapDialog.show({
title : 'Level 2 Title',
message: $('#lavel-2_div-2'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
function showDiv3(div_id)
{
var msg=$('#lavel-2_div-3');
BootstrapDialog.show({
title : 'Level 2 Title',
message: $('#lavel-2_div-3'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
function showDiv4(div_id)
{
var msg=$('#lavel-2_div-4');
BootstrapDialog.show({
title : 'Level 2 Title',
message: $('#lavel-2_div-4'),
onhide : function(){
$('#hidden-div').append(msg);
}
});
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.35.3/css/bootstrap-dialog.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.35.3/js/bootstrap-dialog.min.js"></script>
<!-- This is a container Div which contains all the div to show when bootstrap dialog opens -->
<div style="display : none" id="hidden-div">
<div id="simple-div">
<h1>This is H1 Heading</h1>
This is most simple coding
<br>
<button type="button" class="btn btn-primary" onclick="showDiv1()">Lavel-2 div-1</button>
<button type="button" class="btn btn-primary" onclick="showDiv2()">Lavel-2 div-2</button>
</div>
<div id="lavel-2_div-1">
<h3>This is Level 2 Header</h3>
<span style="color : blue;">This is Level 2 Message 1</span>
</div>
<div id="lavel-2_div-2">
<h3>This is Level 2 Header</h3>
<span style="color : greenyellow;">This is Level 2 Message 2</span>
</div>
<div id="lavel-2_div-3">
<h3>This is Level 2 Header</h3>
<span style="color : pink;">This is Level 2 Message 3</span>
</div>
<div id="lavel-2_div-4">
<h3>This is Level 2 Header</h3>
<span style="color : green;">This is Level 2 Message 4</span>
</div>
<div class="container-fluid" id="complex-div">
<button type="button" class="btn btn-primary" onclick="showDiv3()">Lavel-2 div-3</button>
<button type="button" class="btn btn-primary" onclick="showDiv4()">Lavel-2 div-4</button>
<h2>Panels with Contextual Classes</h2>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">Panel with panel-default class</div>
<div class="panel-body">Panel Content</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Panel with panel-primary class</div>
<div class="panel-body">Panel Content</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">Panel with panel-success class</div>
<div class="panel-body">Panel Content</div>
</div>
<div class="panel panel-info">
<div class="panel-heading">Panel with panel-info class</div>
<div class="panel-body">Panel Content</div>
</div>
<div class="panel panel-warning">
<div class="panel-heading">Panel with panel-warning class</div>
<div class="panel-body">Panel Content</div>
</div>
<div class="panel panel-danger">
<div class="panel-heading">Panel with panel-danger class</div>
<div class="panel-body">Panel Content</div>
</div>
</div>
</div>
</div>
       
<button type="button" class="btn btn-primary" onclick="simpleSHow('Hello 1234')">Simple Div Show</button>
       
<button type="button" class="btn btn-primary" onclick="complexSHow('Complex 1234')">Complex Div Show</button>

Bootstrap and AngularJS: Modal confirmation deleting first row

I got a table which repeats a delete button for each and every member. The confirmation button does work when I don't use the modal, however if i use the Modal, the first row only gets deleted.
As I researched, it could be that since the delete is getting repeated, its value stays for the first row.
Table HTML Code:
<table class="table table-bordered" id="customForms" ng-show="showMembers">
<thead><tr> <th> ID </th> <th>Name</th> <th>Surname</th> <th>Date Of Affiliation</th> <th>Status</th> <th>Email</th> <th>Summary</th></tr></thead>
<tbody>
<tr ng-repeat="member in members">
<td>{{member.memberID}}</td>
<td>{{member.name}}</td>
<td>{{member.surname}}</td>
<td>{{member.dateOfAffiliation}}</td>
<td>{{member.status}}</td>
<td>{{member.email}}</td>
<td>{{member.summary}}</td>
<td><button class="btn btn-primary" data-toggle="modal" data-target="#myModal"> DELETE </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">×</button>
<h4 class="modal-title">Delete Confirmation</h4>
</div>
<div class="modal-body">
Are you sure you wish to delete this row?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="deleteMember(member)" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
AngularJS Code:
$scope.deleteMember = function(member) {
$http.post('model/deleteMember.php', member).success(function(data) {
$scope.getMember();
});
}

Getting a particular ID from a web-grid for a modal

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