angularjs submit within Bootstrap Modal - javascript

I have a Bootstrap 3 Modal containing a list of items to choose from, the list is populated via Angular. When you click on an item in the list, I populate a hidden field to hold the chosen values using a bit of JS, see below. The hidden fields are tied to the $scope. When I submit the form, the items are not passed. I have seen a number of comments that suggest I need to refer to the parent scope, e.g. ng-model="$parent.Description", but this does not seem to work either. Any suggestions?
$scope.OpenFavsModal = function () {
var url = "/GetFavs/";
$http.get(url)
.success(function (data) {
$scope.myData = data;
$('#AddFavsModal').modal('show');
});
};
<form class="form-horizonatal" ng-submit="InsertFavourite()" id="frmAddFav">
<div class="modal fade" id="AddFavsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><strong>Add</strong></h4>
</div>
<div class="modal-body">
<input type="text" id="hidDescription" class="form-control" ng-model="myData.Description" />
<input type="text" id="hidSpectRefno" class="form-control" ng-model="myData.id" />
<ul style="height:300px; overflow-y:scroll; float:left;" id="ulS" >
<li ng-repeat="fav in myData" value="{{fav.id}}" >
{{fav.Description}}
</li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" id="btnSubmit" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</form>
<script type="text/javascript" >
$(document).ready(function () {
$("#ulS").on("click", "li", function () {
$("#hidDescription").val($(this).text());
$("#hidId").val($(this).val());
});
});
</script>

I managed to get this working by putting ng-click on the and passing the favourites object in the click event. Could have been the scope.
<ul style="height:300px; overflow-y:scroll; float:left;" id="ul" >
<li ng-repeat="fav in favourites" value="{{fav.Refno}}" ng-model="fav.Refno" ng-click="InsertFavourite(fav)">
{{fav.Description}}
</li>
</ul>

Related

Prevent Modal from closing on click submit

i have bootstrap modal and always close when i click "Reject".
Why is the preventDefault() not stopping the button from submitting when notes field is empty?
modal :
<div class="modal fade" id="modal_reject" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="form-group has-error">
<label class="col-sm-2 control-label"> Add Note <b style="color:red;">*</b></label>
<div style="margin-left: 5%;margin-right: 5%;">
<textarea id="NOTES_REJECT_SV" name="NOTES" class="form-control" required style="height: 100px;"></textarea>
</div>
</div>
<div class="modal-footer ">
<button type="button" id="submit" form="form" style="width:150px;height:40px;text-align:center;" class="btn btn-ok pull-right" data-dismiss="modal">Reject</button>
<button type="button" class="btn btn-light pull-right" style="width:120px;height:40px;" data-dismiss="modal" >CANCEL</button>
</div>
</div>
</div>
</div>
</div>
and my Javascript :
$('#modal_reject').on('click','#submit',function(event){
event.preventDefault();
var NOTES = document.getElementById("NOTES_REJECT_SV").value;
if(NOTES == ''){
$('#modal_reject').modal('hide')
return false;
}
You can simply .val() jQuery to get the value of your notes. I would not rec-emend mixing up the jQuery and JS together to avoid confusion.
The reason your modal was still hiding that you have data-dismiss="modal" in your modal reject button.
I have simplified your code and is working. If you do not put anything in notes area it will hide the modal or else it will stay and you can do the rest of stuff there.
Also using inline CSS is not a good practice. Bootstrap allow a lot of native class to used do you can want label * red you can just used text-danger class to do that instead of inline CSS.
You can read more on bootstrap Modal and other details here
Run snippet below to see it working.
$('#modal_reject').on('click', '#submit', function(event) {
event.preventDefault();
//get notes
var notes = $('#notes_reject_scv').val();
//if notes are empty hide the modal
if (!notes) {
$('#modal_reject').modal('hide')
console.log('Notes were empty! Modal disappeared')
return false;
}
})
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal_reject">
Open Modal
</button>
<!-- Modal -->
<div class="modal fade" id="modal_reject" 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 class="form-group has-error">
<label class="control-label"> Add Note <span class="text-danger">*</span></label>
<textarea id="notes_reject_scv" name="NOTES" class="form-control" required style="height: 100px;"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" id="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
You can listen to the click envent on tre #submit button as described below
$("#submit").click(function(event){
event.preventDefault();
var NOTES = document.getElementById("NOTES_REJECT_SV").value;
...
});

Issue in passing a data id to bootstrap modal

I have a listing page containing a link button to show modal repeating in a PHP loop. I have searched and found these:
Passing data to a bootstrap modal, Passing data to Bootstrap 3 Modal and Pass id to Bootstrap modal and some others but failed.
What I want
I have fetched the data id from database and pass it to bootstrap modal and there I will use this id to display more info through MySQL queries.
I have written all the code but the ID is not passing to the modal.
Modal Link Button code
<a class="btn btn-warning btn-minier" href="#modal-form-show" role="button" data-toggle="modal" data-target="#myModal" data-id="<?php echo $cottage_id ?>"><i class="icon-edit bigger-120"></i> Reservation </a>
Modal code
<!-- Modal -->
<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">Reservation Details</h4><br>
<h5>Cottage Id :</h5>
<h5>Cottage Title :</h5>
</div>
<div class="modal-body">
<p>ID WILL SHOW IN THIS TEXT FIELD.
<input type="text" name="cottage" placeholder="cottage id" value="" id="cottage" />
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
JS code
$('#modal-form-show').on('show.bs.modal', function(e) {
var c_id= $('a[href="#modal-form-show"]').data('id');
//var c_id= $(this).data(id) - checked both
$("#cottage").val(c_id);
});
I have taken a text input in modal named cottage and I want show that id in this input.
Try this snippet:
$('.btn-minier').click(function(){
var c_id = $(this).attr('data-id');
$('#cottage').val(c_id);
});
I have change little , when button click show the popup & i've set the value of data-id=2
Button
<a class="btn btn-warning btn-minier" id="btnModalPopup" href="" role="button" data-toggle="modal" data-target="" data-id="2"><i class="icon-edit bigger-120"></i> Reservation </a>
I add button in you modal
<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">Reservation Details</h4><br>
<h5>Cottage Id :</h5>
<h5>Cottage Title :</h5>
</div>
<div class="modal-body">
<p>
ID WILL SHOW IN THIS TEXT FIELD.
<input type="text" name="cottage" placeholder="cottage id" value="" id="cottage" readonly />
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" data-dismiss="modal" id="btnSubmit">Submit</button>
</div>
</div>
</div>
</div>
Javascript code
<script>
$(document).ready(function () {
$('#btnModalPopup').click(function () {
var c_id = $(this).data('id');
console.log(c_id)
$('#myModal #cottage').val(c_id)
$('#myModal').modal('show')
});
$('#btnSubmit').click(function () {
alert('submit button click')
// get the id , from input field.
var id = document.getElementById('cottage').value;
console.log(id)
alert(id)
//You can set the id - to php variable - then hit to sever by ajax
})
});
</script>
Try using attr method of JQuery
$('#modal-form-show').on('show.bs.modal', function(e) {
// instead of this
//var c_id= $('a[href="#modal-form-show"]').data('id');
// try this one
var c_id= $('a[href="#modal-form-show"]').attr('data-id');
//var c_id= $(this).data(id) - checked both
$("#cottage").val(c_id);
});

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');
});
});

Many buttons trigger one modal in html table

Node express app using EJS.
I'm creating a table - each cell has a button to trigger a modal. This code is inside a for loop:
<td>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Header-->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Catalog Numbers</h4>
</div>
<!-- Body -->
<div class="modal-body">
<% for(var j = 0; j < user.notifications.length; j++) { %>
<%= user.notifications[j] %>
<% } %>
</div>
<!-- Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="btn-group" role="group" aria-label="...">
<!-- Button trigger modal -->
<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">View Subscribed Products </button><br><br>
</div>
</td>
Every thing loads fine, the rendered html shows that each cell has it's own modal with it's own data but every button triggers myModal and they're all called myModal so every button loads the modal from the first cell.
Is there a way to dynamically create the div id like myModal1, myModal2, etc.?
I've tried
<div class="modal fade" id="<%=user.id%> ...>
Should I be doing this another way?
One such would be to use a single modal, but then vary the modal content based on the trigger button.
What happens is that an event is bound to the modal, which is triggered when the button opens the target modal. You can still hide the modal content inside the table, which the event will find and populate the modal body with.
Here is a simple example which you'll need to adapt to your code:
$(function() {
// when the modal is shown
$('#myModal').on('show.bs.modal', function(e) {
var $modal = $(this);
// find the trigger button
var $button = $(e.relatedTarget);
// find the hidden div next to trigger button
var $notifications = $button.siblings('div.hidden');
// transfer content to modal body
$modal.find('.modal-body').html($notifications.html());
})
});
<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.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Notifications</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>
<div class="hidden">
<ul>
<li>Product one</li>
<li>Product two</li>
<li>Product three</li>
</ul>
</div>
<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">View Subscribed Products</button>
</td>
</tr>
<tr>
<td>Maria</td>
<td>
<div class="hidden">
<ul>
<li>Product three</li>
<li>Product four</li>
<li>Product five</li>
</ul>
</div>
<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">View Subscribed Products</button>
</td>
</tr>
</tbody>
</table>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Header-->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Catalog Numbers</h4>
</div>
<!-- Body -->
<div class="modal-body"></div>
<!-- Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

Get modal caller

I have a Twitter Bootstrap list whose items have badged to show the amount of each item. I want to open a modal dialog to edit that amounts. But since it is going to be a long dynamic list, I can't write a function for each badge, but an only function which is able to determine who called the modal and:
Get the actual amount of the caller badge to be shown at the modal
Store the new amount when user changes the modal
HTML:
<div class="panel panel-primary">
<div class="panel-heading">Shopping cart</div>
<div class="panel-body">
<ul id="cart-list" class="list-group">
<li href="#" class="list-group-item" data-type="banana">Bananas
<div class="pull-right"> <span id="badge" class="badge">5</span>
</div>
</li>
<li href="#" class="list-group-item" data-type="pear">Pears
<div class="pull-right"> <span id="badge" class="badge">2</span>
</div>
</li>
</ul>
</div>
</div>
<!-- Input-spinner modal dialog -->
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="input-group spinner">
<input type="text" class="form-control" value="42">
<div class="input-group-btn-vertical">
<button class="btn btn-default"><i class="fa fa-caret-up"></i>
</button>
<button class="btn btn-default"><i class="fa fa-caret-down"></i>
</button>
</div>
</div> <span class="help-block">Set the number of items</span>
</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 -->
JS:
// Create shopping cart list
var cart_list_element = document.getElementById("cart-list");
var cart_list = new Sortable(cart_list_element, {
group: {
name: "fruit_group",
pull: true,
put: true
},
});
// Configure click action over the badges
jQuery(".badge").click(function() {
console.log('Clicked badge, showing modal');
$("#myModal").modal('show');
});
// Input-spinner function
(function ($) {
$('.spinner .btn:first-of-type').on('click', function () {
$('.spinner input').val(parseInt($('.spinner input').val(), 10) + 1);
});
$('.spinner .btn:last-of-type').on('click', function () {
$('.spinner input').val(parseInt($('.spinner input').val(), 10) - 1);
});
})(jQuery);
This is a jsfiddle with my attempt. I don't know why the modal does not show up, it works localy. I suppose I made some mistake writting the example, o maybe jsfiddle just don't like modals. Anyway I think it shows what am I trying to achieve.
Any help?
something like this? http://jsfiddle.net/upjy4s5b/
using jquery and formatting your html structure with classes allows you to find different things in relation to other things. calling the bootstrap modal via jquery call instead of the inline call allows for easy transfer of the information you want.
$(document).ready(function() {
$('.edit').on('click', function() {
var name = $(this).closest('.item').find('.name').html();
var count = $(this).closest('.item').find('.count').html();
$('#myModal').find('.modal-body').find('.itemName').html(name);
$('#myModal').find('.modal-body').find('.itemCount').html(count);
$('#myModal').modal('show');
});
});
<ul class="list">
<li class="item">
<span class="name">Item 1</span>
<span class="count">Count 1</span>
Edit
</li>
<li class="item">
<span class="name">Item 2</span>
<span class="count">Count 2</span>
Edit
</li>
<li class="item">
<span class="name">Item 3</span>
<span class="count">Count 3</span>
Edit
</li>
</ul>
<div class="modal fade" id="myModal">
<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">
Name: <span class="itemName"></span>
Count: <span class="itemCount"></span>
</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 -->

Categories

Resources