popup modal opens in full sreen in javascript - javascript

I have a table from which i am trying on row button click to open a modal containing the details of the object with the rowId. when my button is pressed i go into code behind and do a query that searches the data from the database, then in javascript i send the data to the modal and open it. The table is placed inside a partial view on it's own so that i can pass here a model. the problem that i am facing is that the modal is not opening as a modal but it is opening as a partial view inside the page.
here is my javascript:
$(document).ready(function () {
$('.btn').click(function () {
debugger;
var url = $('#rowDetails').data('url');
$('#rowDetailsBody').html(data);
$('#rowDetails').modal('toggle');
$('#rowDetails').modal('show');
});
});
row detail is the name of my modal that is contained in a partial view, and rowDetailBody is the Modal-Body.
this the function that i do to get data:
public ActionResult ShowDialog(int id )
{
PSEntities _context = new PSEntities(true);
var data = //some query here
return PartialView("ShowDialog", data);
}
i return to a partial view named showDialog the data that is fetched from the database.
and this is my patialView:
<div class="modal fade" draggable="true" id="rowDetails" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">SMS #item.SmsMT.Id</h3>
</div>
<div class="modal-body" id="rowDetailsBody">
<table style="width:100%;" class="table table table-hover table-striped table-condensed export-table" border="0">
<tr>
<td align="left">
<label>#ViewRes.SharedStrings.SmsID</label>
</td>
<td>
#Html.DisplayFor(modelItem => item.SmsMT.Id)
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-sm" type="button" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
plz can someone help me and tell how to open the window in a modal instead of full screen

Looks like you have missed modal-dialog and modal-content wrapper from your modal markup.
<div class="modal fade">
<div class="modal-dialog">
<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">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

Related

php - table click button, popup to display details

How to show details data(using popup) when I click the view button in table in each rows?
button code
echo '<button href="student.php?id="'.$row['mactrixNo'].'" onclick=document.getElementById("id02").style.display="block">View</button>';
popup code
<div id="id02" class="logform">
<table border="1" class="tg w-100">
<tr>
<?php if(isset($_GET['id'])){
$query='select *
from student
where mactrixNo="'.$_GET['id'].'"';
}
?>
I can't get the 'id' when popup
Bootstrap provides the HTML, CSS, and JavaScript you need to achieve this.
This is from v4 https://getbootstrap.com/docs/4.1/components/modal/
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">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">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
So long as you have the CSS and JS in your <head> section, all you need to do is edit the content!

Creating a modal with bootstrap in ASP.NET MVC

I'm using .NET Framework 4.5, Bootstrap v3.3.6 on a ASP.NET MVC project.
What I want to do is create a modal form
I tried the following:
Created a modal container in the main layout
<div id="modal-container" class="modal fade" tabindex="-1" role="dialog" style="border: 5px solid #3A87AD;">
x
<div class="modal-content" style="width:500px !important; margin: 10px auto !important;">
<div class="modal-body"></div>
</div>
</div>
Added js to make internal Ajax call to inject content in a partial view
$(function () {
$('body').on('click', 'modal-link', function (e) {
e.preventDefault();
$(this).attr('data-target', '#modal-container');
$(this).attr('data-toggle', 'modal');
});
$('body').on('click', 'modal-close-btn', function() {
$('modal-container').modal('hide');
});
$('#modal-container').on('hidden.bs.modal', function(){
$(this).removeData('bs.modal');
});
});
Created a partial view that to load in a modal dialog
<div class=" ">
<div class="modal-title" style="color: #3A87AD;">
<h3> Add Content</h3>
</div>
<br />
#using (Html.BeginForm("Create", "Place", FormMethod.Post))
{
<div class="row-fluid">
Enter Content
</div>
<div class="row-fluid">
#Html.textAreaFor(m => m.Text, new { style="width:400px; height:200px;"})
</div>
<div class="row-fluid">
<div class="col-md-4 col-md-offset-4">
<button type="submit" id="btnSave" class="btn btn-sm">Save</button>
<button type="button" class="btn btn-sm" data-dismiss="modal">Cancel</button>
</div>
</div>
}
</div>
<script type="text/javascript">
$(function () {
$('#btnSave').click(function () {
$('#modal-container').modal('hide');
});
});
</script>
Created action to return the partial view and to process the results of the post
public ActionResult CreateModal()
{
return PartialView("_CreateModal");
}
[HttpPost]
public ActionResult CreateModal(Place model)
{
return RedirectToAction("Index");
}
Created an action link to show the modal when clicked
#Html.ActionLink("Click me", "CreateModal", "Place", null, new { #class = "img-btn-addcontent modal-link", title = "Add Content" })`
My problem is that my modal doesnt look like a modal
Simply add bootstrap.min.js and bootstrap.css to Your bundles for showing/hiding modal.
Your modal body should look like this:
<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">
#Html.Partial("My partial View")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
`
and then you call Your modal by:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
Also remember to insert Your partial View into Your modal body.
My solution. You can write this code in cshtml.
Step1:
<script type="text/javascript">
function ShowModal() {
$("#exampleModal").modal('show');
}
</script>
Step2:
<button type="button" class="btn btn-sm btn-default" onclick="ShowModal()">Show modal</button>
Step3:
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">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">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

stackable popup bootstrap modal close issue

I'd like to display Stackable Modal in my view :
Principal view
.....some code
#*Pop Up ajout demande*#
<div class="modal fade" tabindex="-1" data-focus-on="input:first" role="dialog" id="addModal" style="height: 100%">
<div class="modal-dialog" style="width: 100%">
<div class="modal-content" style="width: 100%">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<label class="modal-title center-block" style="color:red; font-size:larger"></label>
</div>
<div class="modal-body">
#Html.Partial("AjouterDemandePopUp")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" id="btnEscape">Quitter</button>
</div>
</div>
</div>
</div>
Partial view AjouterDemandePopUp
...........some code
<div class="modal " tabindex="-2" id="VehModal">
<div class="modal-dialog" style="width: 100%">
<div class="modal-content" style="width: 100%">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<label class="modal-title center-block" style="color:red; font-size:larger">Choix de véhicule</label>
</div>
<div class="modal-body">
<p><label id="labchauffeurs">Chauffeurs disponibles</label> <select id="lstchauffeurs" name="lstchauffeurs" style="width:250px; "></select> </p>
<p>
<table id="tblauto" class="table table-bordered table-striped">
<thead>
<tr style="color:white;background-color:#3c8dbc" id="autoheader">
<th></th>
<th>Chauffeur</th>
<th>Marque</th>
<th>Plaque d’immatriculation</th>
<th>Kilométrage</th>
<th>Année de fabrication</th>
<th>Carte de carburant </th>
<th>Observation</th>
</tr>
</thead>
<tbody id="tblautoBody"></tbody>
</table>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" id="btnSetVehicle">Valider</button>
<button type="button" class="btn btn-default" data-dismiss="modal" id="btnQuit">Annuler</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
The problem is when I validate or close the second pop ( pop up inside partial view) the first pop up will be closed !!!
I need to know the reasons of this behavior? How can I fix this?
When you click on the button with attribute data-dismiss="modal" then it will call bootstrap function to close all opened modals.
Instead of using built-in function - create your own function, which will close specific modal. Use attribute like data-dismiss-modal="#modal_id" - then you will dismiss modal only with id #modal_id
$(document).on('click', '[data-dismiss-modal]', function () {
var target = $(this).attr('data-dismiss-modal');
$(target).modal('hide');
});
Example: https://jsfiddle.net/73mdx4jm/

Onclick function triggering multiple times

I am having an issue when saving inside of the add-comment modal. When I click on "save" inside of the modal it is triggers depending on how many times I have previously opened that modal.
The form I am working with is dynamic so there could be 1 to many comments to be added. I only want to use one modal to insert comments in a hidden field (not shown)
I am very confused to why when i click on the
TEST CASE
Launch Page
Open the comment modal using the comment icon
Click Save
Repeat open and saving the comment modal multiple times.
Look inside of console to see
"In Save Comment: " + i
Where i equals the number of times it is in that function.
HTML
<form id="requestForm">
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#detailsModal"><span class="glyphicon glyphicon-share" aria-hidden="true"></span></button>
<button type="button" class="btn btn-default" data-add-comment><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
</form>
<div class="modal fade" id="detailsModal" tabindex="-1" role="dialog" aria-labelledby="detailsModalLabel">
<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="detailsModalLabel">User Details</h4>
</div>
<div class="modal-body">
<table class="table table-bordered">
<tbody>
<tr>
<td class="col-sm-3"><strong>Name</strong></td>
<td class="col-sm-4" id="info-name"></td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- End Details Modal -->
<!-- Comments Modal -->
<div class="modal fade" id="commentModal" tabindex="-1" role="dialog" aria-labelledby="commentModalLabel">
<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="commentModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<textarea id="modalComTxt" class="form-control" rows="4" maxlength="512"></textarea>
<p>Characters left: <span id="txtAreaCount">512</span></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="save-comment" type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>
</div>
</div>
</div>
</div>
JAVASCRIPT
$("#requestForm").on('click', '[data-add-comment]', function() {
var curComBtn = $(this);
var curComment = curComBtn.prev().val();
var modalTxt = $('#modalComTxt');
var commentModal = $('#commentModal');
modalTxt.val(curComment); //set the modal value with existing comment
var i = 1;
$('#txtAreaCount').text((512 - modalTxt.val().length)); //change text count in modal
commentModal.modal('toggle');
//Function when clicking save in modal
$('#save-comment').click(function() {
console.log("In Save Comment: " + i)
i++
console.dir(curComBtn.prev());
curComBtn.prev().val(modalTxt.val());
});
});
Move the code below outside the $("#requestForm").on('click', function(){...} handler. What is happening, is that you bind an additional handler to save-comment button click event every time the [data-add-comment] button of the form gets clicked.
$('#save-comment').click(function() {
var curComBtn = $('#requestForm').find('[data-target="#detailsModal"]');
curComBtn.val( $('#modalComTxt').val() );
});
Every time someone clicks #requestForm you bind the click eventlistener to #save-comment. So after opening the modal for a second time there will be two click eventlisteners binded to #save-comment.
Since #save-comment isn't added dynamically, you can bind the click event outside of the #requestForm click handler:
$('#requestform').click(function(){
// do stuff
});
$('#save-comment').click(function(){
// do other stuff
});

Parameter from button to modal and then call Spring controller

I'm enough new with html/javascript so I would need an advice. I have to call modal from button and in this modal I would like to see a table obtained from database.
So my idea is to call modal through button so:
<tbody>
<tr th:each="fleet : ${fleets}">
<td th:text="${fleet.application}"></td>
<td th:text="${fleet.cubic}"></td>
<td th:text="${fleet.power}"></td>
<td th:text="${fleet.euroClass}"></td>
<td th:text="${fleet.engineType}"></td>
<td th:text="${fleet.traction}"></td>
<td th:text="${fleet.transmission}"></td>
<td><button data-id="showCarsButton" type="button"
class="btn btn-primary" data-toggle="modal"
data-target="#modalShowCar">Show cars</button></td>
<td><button id="addCarButton" type="button"
class="btn btn-primary">Add car</button></td>
</tr>
temporary modal:
<div class="modal fade" id="modalShowCar" 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">Fleet cars</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">Close</button>
</div>
</div>
</div>
temporary js:
$(document).ready(function(){
$('#modalShowCar').on('show.bs.modal', function (e) {
var idFleet = $(e.relatedTarget).data('id');
$.ajax({
type : "GET",
url : "cars/"+ idFleet,
contentType : 'application/json',
success : function(data) {
if (data.status==200){
//I have to pass to modal the data.body
} else {
//show error
}
},
error : function(data) {
window.location.href = "/500"; //Redirect to page 500
}
});
});
});
and then from modal call the Spring controller with data-url into table tag.
I need to pass parameter (id) to perform the query, so from the button to the url into modal, how can I do it? Is this the best way?
thanks,regards
There are many ways to achieve what you are trying, with your appraoch, you can use bootstrap modal event and pass the id to modal
change id="showCarsButton" to data-id="showCarsButton"
<button data-id="showCarsButton" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modalShowCar">Show cars</button></td>
JS
$('#modalShowCar').on('show.bs.modal', function (e) {
var carid = $(e.relatedTarget).data('id');
// Do Whatever you like to do,
});
Example Code
$(document).ready(function(){
$('#modalShowCar').on('show.bs.modal', function (e) {
var carid = $(e.relatedTarget).data('id');
alert(carid);
$(".modal-body").html(carid);
// Do Whatever you like to do,
});
});
<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="#modalShowCar" data-id="showCarsButton">Open Modal</button>
<div id="modalShowCar" 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">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Categories

Resources