Using Angular with Bootstrap Modal Multiple Issues - javascript

The short question...Is there any way to use ng-disable on a Boostrap Modal Dialog when a form in a Partial View displayed in the Modal is Invalid?
The long question...
I am working in Asp.net MVC. I need to include a Partial-View in a Modal form. I need the form to display details when an item is clicked so that it can be edited, or display a blank form for the user to add a new record. I can get everything to work just perfectly using Bootstrap Modal (data-target) to open the dialog. However, because I am using ng-click to save the changes to my database. I cannot close the dialog with data-dismiss because the button is in the partial view. If I remove the save button from the partial and put it on the parent it works fine, but then, my ng-disable doesn't work for form validation. I thought I had solved the whole thing by switching to Angular ui-Modal, but when I include that code, it works fine when displaying details of an exisiting record, but won't open the modal at all to display a blank form for a new record. This has been driving me crazy for hours.
So here it is...I need to either to close the Bootstrap Modal from my Angular Controller using ng-click, or I have to figure out how to display the blank form using Angular ui-Modal. Please help!
Here is my modal in my cshtml View:
<div class="container">
<!-- Modal -->
<div class="modal fade" id="addNewComment" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="background-color:#003A5D; color:white">
<h4 class="modal-title">Comment View</h4>
</div>
<div style="background-color:red;color:white;margin-top:-3px">Questionnaire is Incomplete</div>
<div class="modal-body">
#Html.Partial("_AddNewComment")
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</div>
Here is the Partial View that gets rendered:
<div class="panel panel-primary">
<div class="panel-heading">Add New Comment</div>
<div class="panel-body" ng-repeat="cm in selection.comment">
<form name="CommentForm">
<table style="width:100%; border:none">
<tr>
<td style="border:none">
Short Comment (displayed on the Contractor's homepage)<br />
<div style="font-size:8px">Max 50 characters</div><br />
<input name="ShortComment" type="text" style="width:250px" value="{{cm.vchShortComment}}" ng-model="cm.vchShortComment" required />
<div role="alert"> <span class="error" ng-show="CommentForm.ShortComment.$error.required">Short Comment is required!</span></div>
</td>
<td style="border:none;width:40%">
<div class="panel panel-default">
<div ng-controller="ContractorCtrl">
<br />
<p class="input-group">
<span style="padding-left:30px">Start Date:</span><span style="float:right">
<input type="text" style="width:200px; height:30px" class="form-control" datepicker-popup="shortDate" ng-model="startDate" is-open="status.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="false" close-text="Close" />
</span>
<span class="input-group-btn">
<button style="height:30px" type="button" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
<div ng-controller="ContractorCtrl">
<p class="input-group">
<span style="padding-left:30px">End Date:</span><span style="float:right">
<input type="text" style="width:200px; height:30px" class="form-control" datepicker-popup="shortDate" ng-model="endDate" is-open="status.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="false" close-text="Close" />
</span>
<span class="input-group-btn">
<button style="height:30px" type="button" class="btn btn-default" ng-click="open()"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
<br />
<span style="padding-left:30px">Created By: {{user}}</span>
</div>
</td>
</tr>
</table>
Long Comment (shown as Pop-up on the Contractor's homepage)<br />
<div style="font-size:8px">Max 2000 characters</div><br />
<textarea style="width:100%;height:200px" ng-model="cm.vchLongComment" required>
{{cm.vchLongComment}}
</textarea>
<br />
<div>
<span style="float:left"><button type="button" class="btn btn-danger">Delete Comment</button> <button type="button" class="btn btn-primary">Archive</button></span><span style="float:right"><button type="button" style="background-color:#adabab; color:White; border-color:#adabab" data-dismiss="modal" class="btn btn-default" ng-click="clickTest()">Cancel</button> <button type="button" class="btn btn-primary" data-dismiss="modal" ng-disabled="CommentForm.$invalid" ng-click="saveComment()">Save</button></span>
</div>
</form>
</div>
</div>
Here is the code from my Angular Controller:
$scope.clickTest = function (comment) {
debugger;
$scope.selection.comment.splice(comment);
};
$scope.ShowDetails = function (comment) {
debugger;
if (comment == undefined)
{
comment = [
{
id: 0,
vchShortComment: 'please add short comment',
vchLongComment: 'please add long comment'
}
]
}
$scope.selection.comment.push(comment);
//modalInstance = $modal.open({
// templateUrl: 'addNewComment',
// controller: ModalInstanceCtrl
// });
};
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};

I was able to solve all of my issues by wrapping the entire Modal in "" tags. I put the buttons in the modal-footer and now data-target, data-dismiss and form validation all work as desired. Below is the NEW HTML for the Modal. I hope this helps someone else!
<form name="CommentForm">
<div class="modal fade" id="addNewComment" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="background-color:#003A5D; color:white">
<h4 class="modal-title">Comment View</h4>
</div>
<div style="background-color:red;color:white;margin-top:-3px">Questionnaire is Incomplete</div>
<div class="modal-body">
#Html.Partial("_AddNewComment")
</div>
<div class="modal-footer">
<span style="float:left"><button type="button" class="btn btn-danger">Delete Comment</button> <button type="button" class="btn btn-primary">Archive</button></span><span style="float:right"><button type="button" style="background-color:#adabab; color:White; border-color:#adabab" data-dismiss="modal" class="btn btn-default" ng-click="clickTest()">Cancel</button> <button type="button" class="btn btn-primary" data-dismiss="modal" ng-disabled="CommentForm.$invalid" ng-click="saveComment()">Save</button></span>
</div>
</div>
</div>
</div>
</form>

Related

Bootstrap Modal refresh in Javascript

I am working on project, from datatable i have button, when user clicks on it, it will open popup and shows pdf, (which i am rendering). Once user closed modal, next time if he opens button, it will show previous data. its not refreshing.
I have tried $(this).removeData('bs.modal'); it doesn't worked for me.
Here is my Modal code. Any idea how we can refresh only modal?
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog modal-xl">
Modal content
<div class="modal-content">
<div class="modal-header">
Preview
<button type="button" class="close" data-bs-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div id="my_pdf_viewer">
<div id="navigation_controls" style="text-align:center">
<button class="btn btn-primary" id="go_previous">Previous</button>
<input id="current_page" value="1" min="1" max="5" type="number" />
<button class="btn btn-primary" id="go_next">Next</button>
</div>
<br />
<div id="zoom_controls" style="text-align:center">
<button class="btn btn-primary" id="zoom_in">Zoom In (+)</button>
<button class="btn btn-primary" id="zoom_out">Zoom Out (-)</button>
</div>
<div id="canvas_container">
<canvas id="pdf_renderer"></canvas>
</div>
</div>
<div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Bootstrap 4 modal not loading only giving faded screen

I am trying to load a bootstrap modal on a button click.
Already I am have one more Modal in the page which is working fine but on the button click but when I am trying to load another modal on a button click, its just loading faded screen.
<button class="btn btn-green btn-sm" data-toggle="modal" data-target="#addUserModal"><i class="fa fa-plus"></i> <span>Add User</span></button>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" role="dialog" aria-labelledby="addUserModalLabel" aria-hidden="true">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title">Add User</h6>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form action="" method="POST">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">User ID</span>
</div>
<input type="text" class="form-control" id="user_id" value="{{user.UserID}}">
</div>
<div class="input-group mb-3">
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
Could it be that there is a mistake in your -tags? That may cause an error in your modal-body.
<div class="modal-body">
<form action="" method="POST">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">User ID</span>
</div>
<input type="text" class="form-control" id="user_id" value="{{user.UserID}}">
</div>
//removed orphan tag
</form>
</div>
When removed, the modal works fine.
The code you posted is working fine
Codepen Link
Make sure you have added the jquery, popper.js.

jQuery click event works only once (Beginner)

I'm a beginner programmer in jQuery and I do not understand why this feature can not perform the second time. The first time the button content changes, after two seconds it returns to the previous state and the second time I can not do the same thing. Please help! Regards
$(document).ready(function() {
$('#registerButtonM').on('click', function() {
$(this).attr("disabled", true);
var btn = '<button type="button" class="btn btn-success" name="register" id="registerButtonM"><i class="fa fa-user-circle"></i> Register</button>';
if ($('#passRegisterM').val().length >= 8) {
$('#footerButtonsM').html('<i class="fa fa-spin fa-circle-o-notch"></i>');
} else {
$('#footerButtonsM').html('<i class="fa fa-spin fa-circle-o-notch"></i>');
setTimeout(function() {
$('#footerButtonsM').html('');
$('#footerButtonsM').html(btn);
}, 2000);
}
});
});
EDIT:
html:
<div class="modal fade hidden-sm hidden-md hidden-lg" id="registerM" tabindex="-1" role="dialog">
<div class="modal-dialog modal-xs" 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">Register on TaskSave</h4>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>User <span class="" id="vUserM"></span></p>
<input type="text" placeholder="John" name="user" id="userRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>E-mail <span class="" id="vEmailM"></span></p>
<input type="text" placeholder="mail#example.com" name="email" id="emailRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>Password <span class="" id="vPassM"></span></p>
<input type="password" placeholder="password" name="pass" id="passRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 register-captcha">
<div class="g-recaptcha" data-sitekey=""></div>
</div>
</div>
</div>
<div class="modal-footer" id="footerButtonsM">
<button type="button" class="btn btn-success" name="register" id="registerButtonM"><i class="fa fa-user-circle"></i> Register</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
Change your #footerButtonsM HTML to
<div class="modal-footer" id="footerButtonsM">
<i class="fa fa-spin fa-circle-o-notch" style='display:none'></i>
<button type="button" class="btn btn-success" name="register" id="registerButtonM"><i class="fa fa-user-circle"></i> Register</button>
</div>
and your jquery code to:
$(document).ready(function() {
var btnWrapper = $('#footerButtonsM');
var loading = $('#footerButtonsM .fa-circle-o-notch');
$('#registerButtonM').on('click', function() {
var btn = $(this);
btn.hide();
loading.show();
if ($('#passRegisterM').val().length <= 8) {
setTimeout(function(){
loading.hide();
btn.show();
}, 2000)
}
});
});
The result will be the same, but the approach is better.
To setup late binding, change
$('#registerButtonM').on('click', function() {
To this:
$(document).on('click', '#registerButtonM', function() {
Since you have remove the html button from the DOM the onclick handler is autmotically garbaged (this makes sense since the element present does not exist).
An alternative is to declare and function named clicker.. When you remove and insert the html simply add the function clicker to the new button
See snippet below
function clicker() {
$(this).attr("disabled", true);
var btn = '<button type="button" class="btn btn-success" name="register" id="registerButtonM"><i class="fa fa-user-circle"></i> Register</button>';
if ($('#passRegisterM').val().length >= 8) {
$('#footerButtonsM').html('<i class="fa fa-spin fa-circle-o-notch"></i>');
} else {
$('#footerButtonsM').html('<i class="fa fa-spin fa-circle-o-notch"></i>');
setTimeout(function() {
$('#footerButtonsM').html('');
$('#footerButtonsM').html(btn);
$("#registerButtonM").on('click', clicker)
}, 2000);
}
}
$(document).ready(function() {
$('#registerButtonM').on('click', clicker);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade hidden-sm hidden-md hidden-lg" id="registerM" tabindex="-1" role="dialog">
<div class="modal-dialog modal-xs" 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">Register on TaskSave</h4>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>User <span class="" id="vUserM"></span></p>
<input type="text" placeholder="John" name="user" id="userRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>E-mail <span class="" id="vEmailM"></span></p>
<input type="text" placeholder="mail#example.com" name="email" id="emailRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="register-input">
<p>Password <span class="" id="vPassM"></span></p>
<input type="password" placeholder="password" name="pass" id="passRegisterM" />
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 register-captcha">
<div class="g-recaptcha" data-sitekey=""></div>
</div>
</div>
</div>
<div class="modal-footer" id="footerButtonsM">
<button type="button" class="btn btn-success" name="register" id="registerButtonM"><i class="fa fa-user-circle"></i> Register</button>
</div>
</form>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
The reason this doesn't work is that you're removing the button on click of the button. This causes it to not be bound any more.
When you do: $('#footerButtonsM').html(''); this removes the button from the dom object and you lose the early binding that you've setup.
Then you put another button back on the page when you do $('#footerButtonsM').html(btn);
Since this is a completely new button it is no longer bound by the click handler that you have setup.
You have a couple options to fix this issue. You can either rebind or a new click handler either early or late binding to the new button.
A quick fix is to change it to late bind by changing :
$('#registerButtonM').on('click', function() {
to be:
$(document).on('click', '#registerButtonM', function() {
Here is a fiddle of this working

how to implement modal by using bootstrap?

I am using modal concept of bootstrap in my html file. In modal i am entering data after clicking on save this data should be displayed in html page where modal is called. By using which function i can include or display this data in html page.
Here's a working Plunker for your reference
The basic idea is that you can pass an object from a modal back to the controller which opens the modal with close() as follows
$uibModalInstance.close($ctrl.modal);
Then in the parent controller, you can obtain the object as follows
modalInstance.result.then(function(modal) {
$ctrl.modal = modal;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
I assume bootstrap "modal" style class used in the code to show popup from parent page. Here, JavaScript /jquery would help to update the content from modal div to other div in the same page.
Ex: https://jsfiddle.net/AMVijay/f46qLn8L/1/
HTML Content with Bootstrap and JQuery:
<form>
<div class="form-group">
<label for="inputName" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputName" placeholder="Name" disabled>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>
</div>
</div>
</form>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel">
<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="gridSystemModalLabel">Modal Title</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" id="inputFirstName" placeholder="First Name" />
</div>
</div>
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" id="inputLastName" placeholder="Last Name" />
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="modalSaveButton" type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
Java Script:
$(document).ready(function() {
$("#modalSaveButton").click(function() {
var fullName = $("#inputFirstName").val() + " " + $("#inputLastName").val();
console.log("Full Name :: " + fullName);
$("#inputName").val(fullName);
$('#myModal').modal('toggle');
});
});

How to display different Bootstrap Modal on toggling

I am working on Bootstrap Modal. I have two modals. The First Modal toggles/open the second modal dialog, when the user clicks on "Forgot Password" link. The second modal is the same as the first one except body.
What I want to do, on toggling from first modal to second one, the second modal will appear with different header and footer. Is it possible? How can I have this feature?
My code:
<div class="container">
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px; background-color: whitesmoke">
<button type="button" class="close" data-dismiss="modal">
<span title="close" class="glyphicon glyphicon-remove"></span>
</button>
<h2><span class="glyphicon glyphicon"></span> Login to our site</h2>
<p>Enter username and password to log on:</p>
</div>
<div class="modal-body" style="padding:40px 50px;">
<div id="modalTab">
<div class="tab-content">
<div class="tab-pane active" id="login">
<form role="form">
<div class="form-group">
<label for="usrname"><span class="glyphicon glyphicon-user"></span> Username</label>
<input type="text" class="form-control" id="usrname" placeholder="Enter email">
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-pencil"></span> Password</label>
<!--<div class="inner-addon right-addon">
</div>-->
<input type="password" class="form-control" id="password" placeholder="Enter password">
<i style="cursor: pointer" id="seePass" title="Click here to see password" class="glyphicon glyphicon-eye-open"></i>
</div>
<div class="checkbox">
<label>
<input type="checkbox" value="" checked>Remember me</label>
</div>
<button type="submit" class="btn btn-success btn-block"><span class="glyphicon glyphicon-off"></span> Login</button>
</form>
</div>
<div class="tab-pane fade" id="forgotpassword">
<form method="post" action='' name="forgot_password">
<p>Send us your email and we'll reset it for you!</p>
<input type="text" name="eid" id="email" placeholder="Email">
<p>
<button type="submit" class="btn btn-primary">Submit</button>
Wait, I remember it now!
</p>
</form>
</div>
</div>
</div>
</div>
<!--End Body-->
<div class="modal-footer" style="background-color: whitesmoke">
<!--<button type="submit" class="btn btn-danger btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>-->
<p class="text-center">Not a member? Sign Up</p>
<p class="text-center">Forgot Password?</p>
</div>
</div>
</div>
</div>
</div>
Here you go, I hope this code helps you:
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script type="text/javascript">
$(function () {
var showMessage = function (heading, message) {
$(".modal-body").empty();
$(".modal-title").empty();
$(".modal-title").html(heading);
$(".modal-body").html(message);
$('#messageBox').modal('show');
};
$("#popup1").click(function () {
showMessage("Heading 1", "Modal popup message 1");
});
$("#popup2").click(function () {
showMessage("Heading 2", "Modal popup message 2");
});
});
</script>
<input id="popup1" type="button" value="Show Popup" />
<input id="popup2" type="button" value="Show Another Popup" />
<div class="modal fade" id="messageBox" 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"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Output Popup 1:
Output Popup 2:
Just use that as an example and make the necessary changes to your code to make it work

Categories

Resources