I'm using Bootstrap to display my modal in my webpage. My data is stored in database. I used ng-repeat to get my array data from database. I want to pass my room.room_name into modal. How can I do that?
HTML:
<tr data-ng-repeat="room in data">
<td>{{room.room_id}}</td>
<td>{{room.room_name}}</td>
<td>{{room.max_pax}}</td>
<td>{{room.no_booked}}</td>
<td>
<button class="btn btn-warning" data-toggle="modal" data-target="#editRoom"></button>
<button class="btn btn-danger" data-ng-click="delRoom(room.room_id)"></button>
</td>
</tr>
My Modal:
I want to pass room_room.name value and insert into input text value.
I tried to use ng-value but failed.
<div id="editRoom" 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">Edit Library Discussion Room</h4>
</div>
<div class="modal-body">
<form>
<label for="editName">Room Name: </label>
<input type="text" name="editName" data-ng-model="room.name" id="editName" data-ng-value="{{room.room_name}}" />
<br />
<label for="editMaxPax">Maximum Person: </label>
<input type="text" name="editMaxPax" data-ng-model="room.maxPax" id="editMaxPax" value="room.max_pax" />
<button type="submit" class="btn btn-default" data-ng-click="editRoom(room)">Edit</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
AngularJS (Controller):
My get the value from database and store it into array named "data".
var app = angular.module("myApp", ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
"use strict";
$routeProvider
.when("/", {
templateUrl: "facilities.html"
});
}]);
app.controller("myCtrl", function ($scope, $http) {
"use strict";
$scope.state = "";
$scope.addRoom = function (room) {
$http.post("add_library_room.php", {'room_name': room.name, 'max_pax': room.maxPax})
.then(function () {
$scope.msg = "Room is inserted into database.";
});
};
$scope.displayRoom = function () {
$http.get("view_library_room.php")
.then(function (response) {
var data = response.data;
$scope.data = data;
console.log(data);
});
};
$scope.delRoom = function (roomID) {
$http.post("del_library_room.php", {'room_id': roomID})
.then(function () {
$scope.msg = "Room is deleted successfully.";
});
};
$scope.footer = function (page) {
if (page === "login") {
$scope.state = page;
} else {
$scope.state = "";
}
return $scope.state;
};
});
Related
I have a page and when I click on this page with "trigger" I can print the "#content" page. but I want it to return to the "home" I want with the "BACK" button on the page that comes up, but it just doesn't work. Where am I making the mistake?
index.php
<div class="app-content content" id="content">
<?php require 'home' ?></div>
home.php
<div id="kasaButton">
<a type="button" href="#" data-target="contact"> Contact</a>
<a type="button" href="#" data-target="gallery"> Gallery</a>
</div>
<div class="card">
<table class="datatables-basic table">
<thead>
<tr>
<th>İD</th>
<th>NAME</th>
</tr>
</thead>
<tbody>
<tr>
<td>İD</td>
<td>NAME</td>
</tr>
</tbody>
</table>
</div>
gallery
<div class="card-body">
<form action="" method="post">
<input type="text" class="form-control" placeholder="Cari Adı" value=""/>
<button id="save" type="submit">SAVE</button>
<button id="back" type="button">BACK</button>
</form>
</div>
$(document).ready(function () {
var trigger = $("#kasaButton a"),
container = $("#content");
trigger.on("click", function () {
var $this = $(this)
target = $this.data('target');
container.load(target);
return false;
});
});
$(document).ready(function () {
$("#back").on("click", function () {
$("#content").load('home');
});
})
Just add a div with the id of "content"
here is an example of gallery.php:
<div class="card-body">
<form action="" method="post">
<input type="text" class="form-control" placeholder="Cari Adı" value=""/>
<button id="save" type="submit">SAVE</button>
<button id="back" type="button">BACK</button>
</form>
</div>
<div id="content"></div>
<script>
$(document).ready(function () {
var trigger = $("#kasaButton a"),
container = $("#content");
trigger.on("click", function () {
var $this = $(this)
target = $this.data('target');
container.load(target);
return false;
});
});
$(document).ready(function () {
$("#back").on("click", function () {
$("#content").load('home.php');
});
})
</script>
I have a view controller that is returning ViewComponent which returns a View (.cshtml) containing everthing I need.
In another view I want to use that, so i make ajax call to the controller to get raw html in the response.
Modal
<!-- 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">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>
JS:
$.ajax({
url: 'getModal/blabla',
type: 'GET',
data: {
Id: id
},
success: function (data) {
$('#myModal').modal('show').html(data);
},
error: function (error) {
console.error(error);
},
});
In my ViewComponent I return View like this:
return View("~/Views/MyModal.cshtml", new MyViewModel()
{
Id = obj.id
});
Here is a demo:
TestViewComponent.cshtml(you want to pass data to controller,so you need to use type: 'POST' in ajax):
<!-- 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">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>
#section scripts{
<script>
$(function () {
var Id = 1;
$.ajax({
url: '/Test1/RetunSample1ViewComponent',
type: 'POST',
data: {
Id: Id
},
success: function (data) {
$('#myModal').modal('show').html(data);
},
error: function (error) {
console.error(error);
},
});
})
</script>
}
Controller:
public IActionResult TestViewComponent() {
return View();
}
public IActionResult RetunSample1ViewComponent(int Id) {
return ViewComponent("Sample1", new Sample1Model { Id = Id , Name="sample1"}); ;
}
ViewComponents/Sample1:
public class Sample1:ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(Sample1Model s)
{
return View("~/Views/Shared/Default.cshtml",s);
}
}
Views/Shared/Default.cshtml:
#model Sample1Model
Id:<input asp-for="Id" />
Name:<input asp-for="Name" />
result:
You can also use partial view:
Controller:
public IActionResult ReturnPartialView(int Id) {
return PartialView("~/Views/Shared/Default.cshtml", new Sample1Model { Id = Id, Name = "sample1" });
}
View:
$(function () {
var Id = 1;
$.ajax({
url: '/Test1/ReturnPartialView',
type: 'POST',
data: {
Id: Id
},
success: function (data) {
$('#myModal').modal('show').html(data);
},
error: function (error) {
console.error(error);
},
});
})
I am using a bootstrap model to create and update groups. on the backend side, there are no issues. Below I have my ts and HTML code I used. the modal works fine, but I don't know how to return any values from my form to my ts file so i can use it in an API call.
also for an image I need formdata
in short:
What's the issue: Returning user inputs(+image) in angular 2
!edit! the error i get is:
_co.save is not a function
html:
<app-modal #modal>
<div class="app-modal-header">
header
</div>
<div class="app-modal-body">
<form #modalform="ngForm" (ngSubmit)="save(modalform.value)" >
First name: <input type="text" name="FirstName" ngModel><br>
Last name: <input type="text" name="LastName" ngModel><br>
image: <input type="file" name="image" ngModel><br>
</form>
</div>
<div class="app-modal-footer">
<button type="button" class="btn btn-default" (click)="modal.hide()">Close</button>
<button type="button" class="btn btn-primary" (click)="modal.hide()">Save changes</button>
</div>
</app-modal>
TS:
#Component({
selector: 'app-modal',
template: `
<div (click)="onContainerClicked($event)" class="modal fade" tabindex="-1" [ngClass]="{'in': visibleAnimate}"
[ngStyle]="{'display': visible ? 'block' : 'none', 'opacity': visibleAnimate ? 1 : 0}" style=" background: rgba(0,0,0,0.6);">
<div class="modal-dialog" style="padding-top: 25%;">
<div class="modal-content">
<div class="modal-header">
<ng-content select=".app-modal-header"></ng-content>
</div>
<div class="modal-body">
<ng-content select=".app-modal-body"></ng-content>
</div>
<div class="modal-footer">
<ng-content select=".app-modal-footer"></ng-content>
</div>
</div>
</div>
</div>
`
})
export class ModalComponent {
public visible = false;
public visibleAnimate = false;
public show(): void {
this.visible = true;
setTimeout(() => this.visibleAnimate = true, 100);
}
public save(): void{
}
public hide(): void {
this.visibleAnimate = false;
setTimeout(() => this.visible = false, 300);
}
public onContainerClicked(event: MouseEvent): void {
if ((<HTMLElement>event.target).classList.contains('modal')) {
this.hide();
}
}
}
fixed it by placing the save function in the other component
I have a form to create an employee.
When I click the submit button I need to confirm with the confirm dialog box as 'do you want to submit' form?
In angularjs and bootstrap design, if it's possible.
<form ng-submit="create()">
<input type="text" ng-model="fstname">
<input type="text" ng-model="lstname">
<input type="submit" value="submit">
</form>
When I click the submit button, if the form is valid then I want to confirm with confirm box. If I click ok, that means I want to submit the form else it should not submit.
finally i found my answer for this question.my view page code look like below
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="ModalDemoCtrl" class="modal-demo">
<br>
<form name="form" novalidate>
<input type="text" style="width:200px" class="form-control" name="name" ng-model="text.name" required>
<span ng-show="form.$submitted && form.name.$error.required">name is required</span><br>
<input type="text" style="width:200px" class="form-control" name="name1" ng-model="text.name1" required>
<span ng-show="form.$submitted && form.name1.$error.required">name1 is required</span><br>
<input type="submit" ng-click="open(form)">
</form><br>
<p ng-hide="!msg" class="alert" ng-class="{'alert-success':suc, 'alert-danger':!suc}">{{msg}}</p>
</div>
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title" id="modal-title">Your Details</h3>
</div>
<div class="modal-body" id="modal-body">
<p>Are you sure, your name <b>{{name }}</b> is going to submit?
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ok()">Submit</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
</script>
<script>
and my controller code looks below
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope,$uibModal, $log, $document) {
var $ctrl = this;
$scope.animationsEnabled = true;
$scope.text = {};
$scope.open = function (form) {
if(form.$valid)
{
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
values: function () {
return $scope.text;
}
}
});
modalInstance.result.then(function () {
console.log($scope.text);
$scope.msg = "Submitted";
$scope.suc = true;
}, function(error) {
$scope.msg = 'Cancelled';
$scope.suc = false;
});
}else{
alert('');
}
};
});
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope,$uibModalInstance, values) {
var $ctrl = this;
$scope.name= values;
$scope.ok = function () {
$uibModalInstance.close('ok');
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
my updated plunkr here demo
I have this website that is almost a 100% complete but it is based on a customized bootstrap 2.X to meet my needs (bad practice, the fault is entirely on my end.) and what I would like to do is to come up with a modal login that looks like this:
DEMO
The live preview above is based on bootstrap 3.X
You can check with the below link.
Fiddle
<a data-target="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
<div class="modal fade hide" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-remote="/mmfansler/aQ3Ge/show/">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div style="margin-left:35%;>
<modal title="Login form" visible="showModal">
<form role="form">
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="Enter email" />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</modal>
</div>
</div>
</div>
Please Try This one:
$(function() {
var $formLogin = $('#login-form');
var $formLost = $('#lost-form');
var $formRegister = $('#register-form');
var $divForms = $('#div-forms');
var $modalAnimateTime = 300;
var $msgAnimateTime = 150;
var $msgShowTime = 2000;
$("form").submit(function () {
switch(this.id) {
case "login-form":
var $lg_username=$('#login_username').val();
var $lg_password=$('#login_password').val();
if ($lg_username == "ERROR") {
msgChange($('#div-login-msg'), $('#icon-login-msg'), $('#text-login-msg'), "error", "glyphicon-remove", "Login error");
} else {
msgChange($('#div-login-msg'), $('#icon-login-msg'), $('#text-login-msg'), "success", "glyphicon-ok", "Login OK");
}
return false;
break;
case "lost-form":
var $ls_email=$('#lost_email').val();
if ($ls_email == "ERROR") {
msgChange($('#div-lost-msg'), $('#icon-lost-msg'), $('#text-lost-msg'), "error", "glyphicon-remove", "Send error");
} else {
msgChange($('#div-lost-msg'), $('#icon-lost-msg'), $('#text-lost-msg'), "success", "glyphicon-ok", "Send OK");
}
return false;
break;
case "register-form":
var $rg_username=$('#register_username').val();
var $rg_email=$('#register_email').val();
var $rg_password=$('#register_password').val();
if ($rg_username == "ERROR") {
msgChange($('#div-register-msg'), $('#icon-register-msg'), $('#text-register-msg'), "error", "glyphicon-remove", "Register error");
} else {
msgChange($('#div-register-msg'), $('#icon-register-msg'), $('#text-register-msg'), "success", "glyphicon-ok", "Register OK");
}
return false;
break;
default:
return false;
}
return false;
});
$('#login_register_btn').click( function () { modalAnimate($formLogin, $formRegister) });
$('#register_login_btn').click( function () { modalAnimate($formRegister, $formLogin); });
$('#login_lost_btn').click( function () { modalAnimate($formLogin, $formLost); });
$('#lost_login_btn').click( function () { modalAnimate($formLost, $formLogin); });
$('#lost_register_btn').click( function () { modalAnimate($formLost, $formRegister); });
$('#register_lost_btn').click( function () { modalAnimate($formRegister, $formLost); });
function modalAnimate ($oldForm, $newForm) {
var $oldH = $oldForm.height();
var $newH = $newForm.height();
$divForms.css("height",$oldH);
$oldForm.fadeToggle($modalAnimateTime, function(){
$divForms.animate({height: $newH}, $modalAnimateTime, function(){
$newForm.fadeToggle($modalAnimateTime);
});
});
}
function msgFade ($msgId, $msgText) {
$msgId.fadeOut($msgAnimateTime, function() {
$(this).text($msgText).fadeIn($msgAnimateTime);
});
}
function msgChange($divTag, $iconTag, $textTag, $divClass, $iconClass, $msgText) {
var $msgOld = $divTag.text();
msgFade($textTag, $msgText);
$divTag.addClass($divClass);
$iconTag.removeClass("glyphicon-chevron-right");
$iconTag.addClass($iconClass + " " + $divClass);
setTimeout(function() {
msgFade($textTag, $msgOld);
$divTag.removeClass($divClass);
$iconTag.addClass("glyphicon-chevron-right");
$iconTag.removeClass($iconClass + " " + $divClass);
}, $msgShowTime);
}
});
DEMO
More Demos