how to popup window using angularjs - angularjs

I want a dialog comes up on a button click and ask for two dates to enter and give me days difference back on my page on a label using angularjs. Can anyone help me on this?

This is an excellent solution for Bootstrap 3, Angular UI and Modal dialogs.
http://codepen.io/m-e-conroy/pen/ALsdF

I tried using #user3360944 example and it works for me, How can I select multiple items from the popup and add it to a table on the parent page? Here is my code
Parent Template : Mainpage.html
<div ng-controller="ItemCtrl" class="modal-body">
<button class="btn btn-default" ng-click="open();">Open Dialog</button>
<div ng-show="selected">{{ selected.Name}}</div>
</div>
Popup Template : ItemSelectDlg.html
<table class="table table-hover grid">
<thead class="tableheader">
<tr>
<th>Name</th>
<th>Type</th>
<th>Payment</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in items" ng-click="selected.item = item">
<td>{{item.Name}}</td>
<td>{{item.Type}}</td>
<td>{{item.Payment}}</td>
</tr>
</tbody>
</table>
Selected:
<b>{{ selected.item.Name }} </b>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
Script code :ItemCtrl
$scope.items = $scope.ProductSet[0].Items;
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl:'ItemSelectDlg.html',
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
//cancel
});
};
var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};

Related

How could I update a Datatable in modal using AngularJS

I have been stuck in something simple that I think I haven't solved yet because of a lack of experience. Here is the catch I have a very simple controller which load the data from a service when I first Load the modal but once I tried to use the CRUD operations (hopefully working!!) the datatable doesn't update. I will share the code. Thanks of all.
I need that after insert, for example, the datatable update meaning rerender the current list of elements.
Call of the Modal
vm.openBooksModal = function (authorId) {
var modalInstance = $uibModal.open({
templateUrl: '/app/book/index.html,
controller: 'BooksController',
size: '',
resolve: {
authorId: function () {
return authorId;
}
}
});
Modal Controller
(function () {
'use strict';
angular.module('bookApp').controller('BooksController', BooksController);
BooksController.$inject = ['$uibModalInstance', '$scope', 'authorId', 'bookService', 'DTOptionsBuilder', 'DTColumnDefBuilder',
'DTColumnBuilder', 'dtUtils','alertService'];
function NotesController($uibModalInstance, $scope, authorId, bookService, DTOptionsBuilder, DTColumnDefBuilder,
DTColumnBuilder, dtUtils, alertService) {
$scope.authorId= authorId;
$scope.book= {
id: 0,
title:''
};
//if (angular.isDefined())
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
}
$scope.dtBooksColumnDefs = [
DTColumnDefBuilder.newColumnDef(0).notSortable()
];
$scope.dtBooksOptions = DTOptionsBuilder
.newOptions()
.withOption('bFilter', false)
.withOption('order', [[2, 'desc']])
.withOption('aaSorting', [])
.withOption('lengthMenu', [[5, 10, 25, 50], [5, 10, 25, 50]]);
$scope.dtBooksInstance = {};
$scope.LoadData = LoadData();
function LoadData(){
return bookService.getBook($scope.authorId).$promise
.then(getBookCompleted, handleError);
}
$scope.save = function (newTitle) {
$scope.book.title= newTitle;
bookService.saveBook($scope.book.id, $scope.authorId, $scope.book.title).$promise
.then(saveBookCompleted, handleError);
LoadData();
}
$scope.editBook= function( book){
$scope.book= book;
$scope.newTitle= book.title;
}
$scope.deleteBook = function( book){
bookService.deleteBook(book.id).$promise
.then(saveBookCompleted, handleError);
}
//private methods
function getBookCompleted(data) {
$scope.leadBookList = data;
debugger;
if (angular.isDefined($scope.dtBookInstance.rerender)) {
$scope.dtBooksInstance.rerender();
}
$scope.book= { };
$scope.newTitle = '';
}
function saveBookCompleted() {
bookService.getBook($scope.authorId).$promise
.then(getBookCompleted, handleError);
}
function handleError(response) {
alertService.error("Error trying to add a Note. Please try again later or contact IT.");
}
}
})();
HTML TEMPLATE
<div class="modal-header">
<h3 class="modal-title">Books</h3>
</div>
<div class="modal-body">
<div class="book-container">
<div class="head">
</div>
<div class="book">
<div class="form-group">
<div class="row">
<div class="col-lg-12">
<label class="control-label">{{headerLabel}}</label>
<textarea id="newTitle" class="comment-textarea" data-ng-model="newTitle" rows="8" cols="90"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table id="tblBooks" datatable="ng" class="table table-striped table-bordered font-xs"
dt-options="dtBookOptions" dt-column-defs="dtBookColumnDefs" >
<thead>
<tr>
<th></th>
<th></th>
<th>Title</th>
<th>Created By</th>
<th>Created Date</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="book in BookList">
<td>
<button class="btn btn-sm btn-warning" ng-click="editBook(book)">
<i class="fa fa-pencil"></i>
</button>
</td>
<td>
<button class="btn btn-sm btn-danger" ng-click="deleteBook(book)">
<i class="fa fa-trash-o"></i>
</button>
</td>
<td>{{book.title}}</td>
<td>{{book.createdBy}}</td>
<td>{{book.createdDate | date:'MM/dd/yyyy HH:mm:ss'}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer margin-top-0">
<button class="btn btn-primary" ng-click="save(newBook)" data-ng-disabled="newTitle === ''">Save</button>
<button class="btn btn-primary" ng-click="cancel()">Close</button>
</div>
Thanks
You're calling getBookCompleted twice. First after save in the saveBookCompleted function and second in the loaddata function
$scope.save = function (newTitle) {
$scope.book.title= newTitle;
bookService.saveBook($scope.book.id, $scope.authorId, $scope.book.title).$promise
.then(saveBookCompleted, handleError);
// remove this line since you're already call saveBookCompleted
//LoadData();
}

How to bind the data using ng-model in table row (inline editing)

I have 3 values in an object empID,empName,empEmail like this
my problem is in the function gettemplete function(),
var app = angular.module('myApp' , []);
app.controller('myCtrl' , function($scope){
$scope.count=0;
$scope.employees = [{empID:$scope.count+1,empName:'lin',empEmail:'b#1.in'},{empID:$scope.count+1,empName:'test',empEmail:'aaa#3.com'}];
$scope.employeeData = {};
$scope.selected = {};
$scope.addEmployee = function(){
$scope.employees.push({
empName : $scope.empName,
empEmail : $scope.empEmail
});
console.log($scope.employees);
};
$scope.deleteEmployee = function(employee){
var index = $scope.employees.indexOf(employee);
$scope.employees.splice(index,1);
console.log($scope.employees);
};
$scope.getTemplate = function (employee) {
if (employee.empID === $scope.selected.empID){
console.log(employee.empID);
console.log($scope.selected.empID);
console.log("edit");
return 'edit';
}
else {
console.log("display");
console.log(employee.empID);
console.log($scope.selected.empID);
return 'display';
}
};
$scope.reset = function () {
$scope.selected = {};
console.log($scope.selected);
};
$scope.add = function(){
$scope.addEmployee();
};
$scope.editEmployee = function (employee) {
$scope.selected = angular.copy(employee);
console.log($scope.selected);
console.log(employee);
console.log($scope.selected);
};
$scope.updateEmployee = function(employee) {
$scope.employees.push({
empName : $scope.empName,
empEmail : $scope.empEmail
});
console.log($scope.employees);
$scope.getTemplate();
$scope.reset();
};
});
Here goes my html code
<body ng-controller="myCtrl">
<div>
<h3>List Employees</h3>
<table class="table table-striped table-bordered">
<thead>
<th>empID</th>
<th>Employee Name</th>
<th>Employee Email</th>
<th>Action</th>
</thead>
<tbody>
<tr ng-repeat="employee in employees" ng-include="getTemplate(employee)">
<script type="text/ng-template" id="display">
<td>{{employee.empID}}</td>
<td>{{employee.empName}}</td>
<td>{{employee.empEmail}}</td>
<td>
<button type="button" class="btn btn-primary" ng-click="editEmployee(employee)">Edit</button>
<button type="button" class="btn btn-danger" ng-click="deleteEmployee(employee)">Delete</button>
</td>
</script>
<script type="text/ng-template" id="edit">
<td>{{employee.empID+ 1}}</td>
<td><input type="text" ng-model=employee.empName class="form-control input-sm"/></td>
<td><input type="text" ng-model=employee.empEmail class="form-control input-sm"/></td>
<td>
<button type="button" class="btn btn-primary" ng-click="updateEmployee(employee)">Save</button>
<button type="button" class="btn btn-danger" ng-click="reset()">Cancel</button>
</td>
</script>
</tr>
</tbody>
<table>
<button ng-click="add()"> Add row </button>
</div>
</body>
I am able to display my empName and empEmail but I dont know how to display empID. I have a button called addrow to create a row in the table, so when I click that button I need the empID as counted a 3,4,5,...etc like that.
Try changing your addEmployee function as below.
$scope.addEmployee = function(){
$scope.employees.push({
empId : $scope.count++,
empName : $scope.empName,
empEmail : $scope.empEmail
});
console.log($scope.employees);
};
If you are creating few sample employees, make sure you add them via addEmployee function. That way count increases automatically. However, if you want to statically create some employees, assign count correctly. cheers.
your empId is not a number, its a string
change this
empID:'1'
To this
empID:1
So your array
$scope.employees = [{empID:1,empName:'lin',empEmail:'b#1.in'},{empID:2,empName:'test',empEmail:'aaa#3.com'}];
<input type="number" ng-model="employee.empID" class="form-control input-sm"/>
to increment employeeId you can do like
<button type="button" class="btn btn-primary" ng-click="saveEmployee(employee); empId = empId+1" ng-init="empId=1">Save</button>
Current count = {{empId}}

Is it possible to fire ng-click of a button element from a button in another controller?

I want to use angular modal for CRUD operation, so for firing the modal we have button by id modalFire in ng-controller="ModalDemoCtrl" , this is my modal:
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">....</h3>
</div>
<div class="modal-body">
<form id="productForm" novalidate>
<div>
<label for="ProductName"> productName :</label>
<input type="text" name="ProductName" id="ProductName" ng-model="model.ProductName" value="" required />
</div>
<div style="margin:10px">
<label for="Price">price :</label>
<input type="text" name="Price" id="Price" ng-model="model.Price" />
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="Save()" ng-disabled="productForm.$invalid">save</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
</script>
<button type="button" id="modalFire" class="btn btn-default modalBtn" ng-click="open()">UpdateProduct</button>
</div>
and it's controller:
App.controller('ModalDemoCtrl', function ($scope, $modal) {
$scope.open = function (size) {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
};
});
App.controller('ModalInstanceCtrl', function ($scope, $modalInstance, $http) {
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
and i have a grid that shows ProductList :
<div ng-controller="ProductController" ng-init="GetAllProducts()">
<div class="row" style="margin-top:90px" ng-show="!ShowGrid">
<article class="widget">
<header class="widget__header">
<div class="widget__title">
<i class="pe-7s-menu"></i><h3>ProductList</h3>
</div>
<div class="widget__config">
<i class="pe-7f-refresh"></i>
<i class="pe-7s-close"></i>
</div>
</header>
<div class="widget__content table-responsive">
<table class="table table-striped media-table">
<thead style="background-color:rgba(33, 25, 36,0.1)">
<tr>
<th style="width:30%">price</th>
<th style="width:30%">productName</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in Products">
<td>{{product.Price}}</td>
<td>{{product.ProductName}}</td>
</tr>
</tbody>
</table>
</div>
</article>
</div>
</div>
so, i want a way to add a button or <a> element for each row in grid that can fire the modal, is there any way?
Unless I misunderstand your question, why not just add the button to your ng-repeat in your table. For example:
<table class="table table-striped media-table">
<thead style="background-color:rgba(33, 25, 36,0.1)">
<tr>
<th>Price</th>
<th>ProductName</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="product in Products">
<td>{{product.Price}}</td>
<td>{{product.ProductName}}</td>
<td><button ng-click="loadItem(product)>Edit</button></td>
</tr>
</tbody>
</table>
Then inside your controller, just pass the product into your modal load function. For example:
$scope.loadItem = function(product){
var modalInstance = $modal.open({
animation: true,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
product: function () {
return product;
}
}
});
};
See how you can pass the product to the function, and then use resolve to pass the product into the modal. In your modal controller you would attach the product to the $scope, and use it as you would any other view.

How to fire a function in AngularJS controller after HTML loaded

I am working on my angularJS tutorila project.
I need to fire function in controller when the HTML page loaded.
But I don't know how to implement it.
Here is my view:
<div class="row" ng-controller="SensorController as vm">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<h1 class="panel-title">Sensor Response</h1>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-5">
<table class='table borderless table-striped tableContentLeftAlign table-striped-column table-header-color'>
<thead>
<tr>
<th class="col-md-1">Data</th>
<th class="col-md-3">Data name</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-1">MessageNumber:</td>
<td class="col-md-3">{{vm.sensorData.MessageNumber || 'Empty' }}</td>
<tr>
</tr>
<td class="col-md-1">MessageID:</td>
<td class="col-md-3">{{vm.sensorData.MessageID || 'Empty'}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Here is controller:
(function () {
"use strict";
angular.module("sensorManagement").controller("SensorController",
["SensorResource",
"$location",
"toaster",
SensorController]);
function SensorController(SensorResource, $location, toaster) {
var vm = this;
function foo(){
SensorResource.get({ id: "12345678" }, function (data) {
vm.sensorData = data;
},
function (responce) {
});
};
vm.SaveData = function () {
if (!vm.sensorData) {
alert("No data!");
return;
}
SensorResource.save(vm.sensorData, function (data) {
alert("Saved");
},
function (error) {
//Here to implement exception!!!
});
};
vm.GetLog = function () {
SensorResource.query(function (data) {
vm.log = data;
}, function (error) {
//Here to implement exception!!!
});
};
}
})();
I need the function foo() in controller SensorController to be fired after HTML is loaded.
Any idea how can I implement it?
Try something like this:
$scope.$on('$viewContentLoaded', function(){
// TODO logic
});
This code is waiting to event viewContentLoaded. This handler should acts when the page has been loaded.
I hope you work.

angularjs ng-click not working inside the pop over directive

I added some buttons in the pop over template.
When the page finishes loading the first time, clicking on the element on the page shows the popover, and when clicking on the buttons in the popover, every one works fine. But after hiding and showing the popover again, the buttons do not work any more.
I know that the pop over recreates the DOM node every time it is shown/hidden. So I added $compile(content)(scope), but it only works the first time.
Here is my directive:
app.directive "popOverWidthOffset", ($templateCache, $compile)->
getTemplate = ()->
template = $templateCache.get('angular/templates/popOverCustomisationChangeWidthOffset.html')
restrict: 'A'
replace: true
scope: {
argument: '='
addwidth: '&'
decreasewidth: '&'
addoffset: '&'
decreaseoffset: '&'
}
link: (scope, element, attrs)->
content = getTemplate()
console.log(content)
popOverContent = $compile(content)(scope)
options = {
content: popOverContent,
placement: "top",
html: true,
trigger: "click"
}
$(element).popover(options)
Here is the template:
<table>
<tbody>
<tr>
<td>
<a class="btn btn-link" ng-click="addwidth(argument)">
<span class="glyphicon glyphicon-chevron-up">
</a>
</td>
<td> </td>
<td>
<a class="btn btn-link">
<span class="glyphicon glyphicon-chevron-up" ng-click="addoffset(argument)">
</a>
</td>
</tr>
<tr>
<td class="form-group" width="40px;">
<input class="form-control" ng-model="argument.position[1]" style="text-align: center;">
</td>
<td> </td>
<td class="form-group" width="40px;">
<input class="form-control" ng-model="argument.position[2]" style="text-align: center;">
</td>
</tr>
<tr>
<td>
<a class="btn btn-link" ng-click="decreasewidth(argument)">
<span class="glyphicon glyphicon-chevron-down">
</a>
</td>
<td> </td>
<td>
<a class="btn btn-link">
<span class="glyphicon glyphicon-chevron-down" ng-click="decreaseoffset(argument)">
</a>
</td>
</tr>
</tbody>
</table>
DEMO
(function(angular, $) {
'use strict';
PopoverCtrl.$inject = ['$scope', '$window'];
function PopoverCtrl($scope, $window) {
$scope.foo = 'scope foo';
$scope.bar = function() {
$window.alert('bar called');
};
}
PopoverDirective.$inject = ['$templateCache', '$compile'];
function PopoverDirective($templateCache, $compile) {
return {
controller: 'PopoverCtrl',
link: popoverLinkFn
};
function popoverLinkFn(scope, elem) {
/**
* Initialise popover
*/
function initPopover() {
// Read content
var content = $($templateCache.get('/popover.html'));
// Compile it
$compile(content)(scope);
// Popover options
var options = {
html: true,
content: content
};
elem.popover(options);
}
// Call init
initPopover();
// Cleanup on scope destroy
scope.$on('$destroy', function() {
elem.popover('destroy');
});
}
}
angular.module('app', [])
.controller('PopoverCtrl', PopoverCtrl)
.directive('popover', PopoverDirective);
})(window.angular, window.angular.element);

Resources