DateTime is not the same value in the Data Base using angularJs - angularjs

This is The Data in the sql db
and this is the AngulaJJ Ui-Calendar
The Problem is: the data in the db presented with another values in the UI
'use strict';
app.controller('eventController', ['$scope', 'uiCalendarConfig', '$http', 'ngAuthSettings', function ($scope, uiCalendarConfig, $http, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
$scope.SelectedEvent = null;
var isFirstTime = true;
$scope.events = [];
$scope.eventSources = [$scope.events];
//get the events data from server
$http.get(serviceBase + 'api/Event/GetEvents', {
cache: true,
params: {}
}).then(function (data) {
//get and push events data to calendar here
console.log(data.data);
$scope.events.slice(0, $scope.events.length);
angular.forEach(data.data, function (value) {
$scope.events.push({
title: value.EventTitle,
description: value.EventDescription,
start:parseInt(value.StartDate),
end: parseInt(value.EndDate),
allDay: value.IsFullDay,
stick: true
});
});
});
//Calender configration in angular
$scope.uiConfig = {
calendar: {
height: 700,
editable: true,
displayEventTime: false,
header: {
left: 'month basicWeek basicDay agendaWeek agendaDay',
center: 'title',
right: 'today prev,next'
},
eventClick: function (event) {
$scope.SelectedEvent = event;
},
eventAfterAllRender: function () {
if ($scope.events.length > 0 && isFirstTime) {
//Focus first event
uiCalendarConfig.calendars.myCalendar.fullCalendar('gotoDate', $scope.events[0].start);
isFirstTime = false;
}
}
}
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<h1 class="form-login-heading">Calendar</h1>
<div ng-controller="eventController">
<div class="row">
<div class="col-md-12 col-lg-12">
<div id="calendar" ui-calendar="uiConfig.calendar" class="bold blue" ng-model="eventSources" calendar="myCalendar"></div>
</div>
<div class="row col-md-4 text-center center">
<div ng-show="SelectedEvent" class="alert alert-success" style="margin-top:50px">
<h2 style="margin-top:0px"> Selected Event:</h2>
<h3 style="color:#A9A50E">{{SelectedEvent.title}}</h3>
<p>{{SelectedEvent.description}}</p>
</div>
</div>
</div>
</div>
I used:
start: new Date(parseInt(value.StartDate)) > Not Working
and also used:
the data type in the db of this col > DateTime , Date , nvarchar. > Not Working and Same Error.
To sum up:
The problem is: in db the date = 2018-09-09 but in the Ui-view( using angularJS) date = 1-1-1970.
I need to know where is the problem, and how to solve it

The date you have received from service is in string format you need to convert it to javascript date object.
so in your case you can do it as follows
start: new Date(value.StartDate)

Related

Angular ui-calendar auto refresh on form submission

i am using angular ui-calendar for displaying a series of leaves.The data for this is fed from the REST services.I can add a leave to the database also.The problem is that when i add a leave detail to the database it does not automatically reflect in the calendar.If i refresh the page the data will be reflected. Basically what I want to do is when I submit the form (closing the modal) the data to be displayed in the calendar. Thank you in advance.Following is my code
My controller
app.factory('calendarSer', ['$http', '$rootScope', 'uiCalendarConfig', function($http, $rootScope, uiCalendarConfig) {
return {
displayCalendar: function($scope) {
$http.get("rest/leave/holidayList", {}).success(function(data, status, headers, config) {
$scope.holidayList = data;
$calendar = $('[ui-calendar]');
var date = new Date(),
d = date.getDate(),
m = date.getMonth(),
y = date.getFullYear();
$scope.changeView = function(view) {
$calendar.fullCalendar('changeView', view);
};
var m = null;
if ($scope.selectable == "Yes") {
m = true;
} else {
m = false;
}
/* config object */
$scope.uiConfig = {
calendar: {
lang: 'da',
height: 400,
editable: true,
selectable: m,
header: {
left: 'month basicWeek basicDay',
center: 'title',
right: 'today prev,next'
},
eventClick: function(date, jsEvent, view) {
$scope.alertMessage = (date.title + ' was clicked ');
alert("clicked" + date.title);
},
select: function(start, end, allDay) {
var obj = {};
obj.startDate = start.toDate();
obj.endDate = moment(end - 1 * 24 * 3600 * 1000).format('YYYY-MM-DD');
$rootScope.selectionDate = obj;
$("#modal1").openModal();
// calendar.fullCalendar('unselect');
},
dayRender: function(date, cell) {
var today = new Date();
today = moment(today).toDate();
var end = new Date();
end = moment(end).toDate();
end.setDate(today.getDate() + 7);
date = moment(date).toDate();
angular.forEach($scope.holidayList, function(value) {
if (((moment(value.holiday_date).format("YYYY-MM-DD")) == moment(date).format("YYYY-MM-DD"))) {
cell.css("background-color", "#2bbbad");
//$('.date').text('Today');
cell.prepend("<span style=\"max-width:200px;word-wrap:break-word;margin-top:10px;\">" + value.description + "</span>");
cell.prepend("<br>")
}
});
},
eventRender: $scope.eventRender,
}
};
console.log($scope.holidayList);
}).error(function(data, status, headers, config) {
alert("error from holidaylist");
});
$scope.events = [];
$scope.eventSources = [$scope.events];
$http.get($scope.url, {
cache: true,
params: {}
}).then(function(data) {
console.log(data);
$scope.events.slice(0, $scope.events.length);
angular.forEach(data.data, function(value) {
console.log(value.title);
if (value.approvalStatus == "Approved") {
var k = '#114727';
} else {
k = '#b20000'
}
$scope.events.push({
title: value.signum,
description: value.signum,
start: value.startDate,
end: value.endDate,
allDay: value.isHalfDay,
stick: true,
status: value.approvalStatus,
backgroundColor: k
});
});
});
}
}
}]);
g-include
<div id="controllerid">
<div class="row" >
<div class="col s10 m10 l10">
<div id="calendar" ui-calendar="uiConfig.calendar" ng-model="eventSources" calendar="myCalendar"></div>
</div>
</div>
<!-- Modal Structure -->
<div id="modal1" class="modal" ng-controller="MyAddController">
<div class="modal-content">
<h4>Apply Leave</h4>
<div class="row">
<form class="col s12" id="form1">
<div class="row modal-form-row">
<div class="input-field col s6">
<input id="num" type="text" class="validate" ng-model="test.num"> <label for="num">num</label>
</div>
<div class="input-field col s6">
<input id="ename" type="text" class="validate" ng-model="test.title"> <label for="ename">Employee Name</label>
</div>
</div>
<div class="row">
<form class="col s12">
<div class="row modal-form-row">
<div class="input-field col s5">
<input id="startDate" type="text" class="validate" value="{{selectionDate.startDate | date}}" readonly >
</div>
<div class="input-field col s5">
<input id="endDate" type="text" class="validate" value="{{selectionDate.endDate | date}}" readonly>
</div>
<div class="input-field col s1">
<p>
<input type="checkbox" id="test6" ng-model="test.isHalfDay" /> <label for="test6">Half Day</label>
</p>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="description" type="text" class="validate" ng-model="test.description"> <label for="description">Description</label>
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button class="btn waves-effect waves-light" type="submit" ng-click="add()" name="action"> Submit <i class="material-icons right">send</i>
</button>
</div>
</div>
and my add controller
app.controller("MyAddController", function($scope, $http,$rootScope,calendarSer) {
$scope.test = {};
$scope.add = function() {
$("#modal1").closeModal();
$scope.test1=$rootScope.selectionDate;
var jsonData = JSON.stringify($.extend({}, $scope.test, $scope.test1));
console.log(""+jsonData);
$http({
url: "rest/leave/create",
method: "POST",
data: jsonData,
headers: {'Content-Type': 'application/json'}
}).success(function(data, status, headers, config) {
if (data) {
console.log("Entered in the add controller");
$scope.data = data;
$scope.url="rest/leave/list";
$scope.selectable="Yes";
calendarSer.displayCalendar($scope);
$("#popupmodal").openModal();
console.log("Exited in the add controller");
}
}).error(function(data, status, headers, config) {
alert("error from create leave");
})
}
});
ANy help would be appreciated
In your "success" function after you run the "create" function, you can simply add the event to fullCalendar using the same data, via the built-in "renderEvent" function.
Something like this (I don't know Angular, so you may need to adjust this slightly to get your calendar element into context, but hopefully you understand the idea). I am also assuming that jsonData contains all the relevant event data which we can use for this:
.success(function(data, status, headers, config) {
if (data) {
console.log("Entered in the add controller");
$scope.data = data;
//add the event to the calendar UI without refreshing the events
$('[ui-calendar]').fullCalendar("renderEvent",
{
start: jsonData.startDate,
end: jsonData.endDate,
title: jsonData.title
},
true //make it stick even when the view changes
);
$scope.url="rest/leave/list";
$scope.selectable="Yes";
calendarSer.displayCalendar($scope);
$("#popupmodal").openModal();
console.log("Exited in the add controller");
}
You may need to add more fields, or you may need to get momentJS to parse the values in startDate / endDate, depending exactly what those fields output.
The "renderEvent" method can be found in the fullCalendar documentation here: https://fullcalendar.io/docs/event_rendering/renderEvent/

UI Grid won't display

Trying to follow the docs provided here on their website, but still not able to get it to work.
I am scanning a table using AWS DynamoDB, and trying to display the information in the table within this UI grid.
My controller:
angular.module('App')
.controller('GridCtrl', ['modelObjects', '$scope', 'dbCall', function(modelObjects, $scope, dbCall) {
$scope.gridOptions = {
enablesorting: true,
columndefs: [{
field: 'ref id'
}, {
field: 'group'
}, {
field: 'cost center'
}, {
field: 'cost center description',
}, {
field: 'recruitment firm',
enablesorting: false
}],
};
$scope.updateGrid = function(data) {
// for (var key in data) {
// var item = data[key];
// for (var key2 in item) {
// console.log(item[key2]);
// $scope.gridOptions.data = item[key2];
// }
// }
$scope.gridOptions.data = data;
};
$scope.scanPosition = function() {
var params = {};
return dbCall('addGrid', params, $scope.check);
};
$scope.check = function(err, data) {
if (err) {
$scope.results = "Failure: Unable To Connect To Database";
console.log(err);
}
else {
$scope.results = "Success: Position Table Scanned";
console.log(data);
$scope.updateGrid(data);
}
};
setTimeout(function() {
$scope.scanPosition();
}, 50);
}]);
My View:
<!DOCTYPE html>
<html>
<head></head>
<body ng-app="App">
<div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12">
<div class="container-fluid">
<br>
<div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
<div class="panel-heading" style="text-align: center;">
<h3>Grid Page</h3>
</div>
</div>
<div ui-grid="gridOptions" class="myGrid"></div>
</div>
</div>
</body>
</html>
My database is working; I am able to loop and display the information in the console. It just isn't being displayed in the grid. I have no console errors.
Thanks in advance for any help!
It looks like you need to set the grid data like this:
$scope.gridOptions.data = data.Items;
Since data.Items is the array of objects.
In Addition to #GDan, there's no controller specified in the HTML file. Only ng-app="App". Try adding ng-controller="GridCtrl" in one of the parent divs surrounding the ui-grid directive.
CodePen and Snippet below:
http://codepen.io/Lethargicgeek/pen/rLbZGp?editors=1010
angular.module('App', ['ui.grid']);
(function() {
angular.module('App').controller('GridCtrl', ctrlFn);
ctrlFn.$inject = ['modelObjects', 'dbCall', '$timeout'];
function ctrlFn(modelObjects, dbCall, $timeout) {
var $ctrl = this;
// BINDING
$ctrl.gridOptions = {
enableSorting: true,
columnDefs: [{
field: 'ref id'
}, {
field: 'group'
}, {
field: 'cost center'
}, {
field: 'cost center description',
}, {
field: 'recruitment firm',
enablesorting: false
}],
};
// END BINDING
// INIT
$timeout(function() {
scanPosition();
}, 1000);
// END INIT
function updateGrid(data) {
$ctrl.gridOptions.data = data;
};
function scanPosition() {
var params = {};
return dbCall.dbCall('addGrid', params, check);
};
function check(err, data) {
if (err) {
$ctrl.results = "Failure: Unable To Connect To Database";
console.log(err);
} else {
$ctrl.results = "Success: Position Table Scanned";
console.log(data);
updateGrid(data);
}
};
}
})();
// Mocked Service modelObjects
(function() {
angular.module('App').service('modelObjects', svcFn);
function svcFn() {}
})();
// Mocked Service dbCall
(function() {
angular.module('App').service('dbCall', svcFn);
function svcFn() {
var svc = this;
svc.dbCall = dbCall;
function dbCall(action, params, callBackFn) {
// Should really use a promise rather than a callbackFn
callBackFn(null, [{
'ref id': "fooRef",
'group': "fooGroup",
"cost center": "fooCostCenter"
}]);
}
}
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
<link href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<html>
<head></head>
<body ng-app="App">
<div class="col-lg-10 col-lg-offset-2 col-sm-9 col-sm-offset-3 col-xs-12" ng-controller="GridCtrl as $ctrl">
<div class="container-fluid">
<div>$ctrl.results: {{$ctrl.results}}</div>
<div class="panel panel-default" style="align: center; border:1px solid #d4d4d4;">
<div class="panel-heading" style="text-align: center;">
<h3>Grid Page</h3>
</div>
</div>
<div ui-grid="$ctrl.gridOptions" class="myGrid"></div>
</div>
</div>
</body>
</html>
Try changing "columndefs" to "columnDefs" and "enablesorting" to "enableSorting". That's what I see off of the top, but I'm still looking. You're also missing "ng-controller="GridCtrl" after ng-app="App" in your HTML. Also, make sure you included the script for the grid and injected into your module.

Angular ui calendar not showing time

In Angular ui calendar showing the date properly but it not showing the time in event scheduler.In browser there is no error on console but time not showing.How can i get time on calendar
angular js file:
dashboard.controller("ReservationController", ['$rootScope', '$scope', '$state', '$location','$http', 'dashboardService', 'Flash',
function ($rootScope, $scope, $state, $location, $http, dashboardService, Flash) {
var vm = this;
vm.reservation = {};
console.log("Reservation");
}]).factory('mmUrl', function($http)
{
return $http.get("Youin-api/v2/business/44/view_reservation");
}).controller('CalendarCtrl',
function($scope, $rootScope, ngDialog, $compile, $timeout, uiCalendarConfig, $location,$http, dashboardService, Flash,mmUrl) {
CalendarCtrl = {};
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var evnt = [];
$scope.events=[];
$scope.changeTo = 'Hungarian';
/* event source that calls a function on every view switch */
$scope.eventsF = function (start, end, timezone, callback)
{
var events ;
$scope.events=[];
mmUrl.success(function(data){
$scope.events = angular.fromJson(data.reservation_list);
callback($scope.events);
});
};
$scope.alertOnEventClick = function( date, jsEvent, view){
var event_id=date.id;
$http.post("Youin-api/v2/business/event_information",{event_id:event_id}).then(function(response) {
if(response.data.error="false")
{
console.log(response.data);
document.getElementById("title").value = response.data.title;
document.getElementById("amount").value = response.data.amount;
document.getElementById("deal_id").value = response.data.deal_id;
$('#EventInfo').modal('show');
}
});
};
$scope.changeView = function(view,calendar) {
uiCalendarConfig.calendars[calendar].fullCalendar('changeView',view);
};
$scope.renderCalender = function(calendar) {
$timeout(function() {
if(uiCalendarConfig.calendars[calendar]){
uiCalendarConfig.calendars[calendar].fullCalendar('render');
}
});
};
$scope.eventRender = function( event, element, view ) {
element.attr({'tooltip': event.title,
'tooltip-append-to-body': true});
$compile(element)($scope);
};
$scope.uiConfig = {
calendar:{
height: 450,
editable: false,
allDay: false,
ignoreTimezone: false,
header:{
left: 'title',
center: 'month',
right: 'today prev,next'
},
eventClick: $scope.alertOnEventClick,
eventDrop: $scope.alertOnDrop,
eventResize: $scope.alertOnResize,
eventRender: $scope.eventRender
}
};
$scope.eventSources = [$scope.events,$scope.eventsF];
$scope.eventSources2 = [$scope.events,$scope.eventsF];
});
My front end:
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 padding-right-5 padding-left-5" ng-controller="CalendarCtrl">
<div class="" align="left">
<div class="tab-wrap">
<input type="radio" name="tabs" id="tab1" checked ng-click="changeView('month', 'myCalendar3')" >
<div class="tab-label-content" id="tab1-content">
<label for="tab1" class="label1">Monthly View</label>
<div class="tab-content mdl-shadow--4dp">
<div class="calendar" ng-model="eventSources2" calendar="myCalendar3" ui-calendar="uiConfig.calendar"></div>
</div>
</div>
<div class="alert-success calAlert" ng-show="alertMessage != undefined && alertMessage != ''">
<h4>{{alertMessage}}</h4>
</div>
<input type="radio" name="tabs" id="tab2" ng-click="changeView('agendaDay', 'myCalendar3')">
<div class="tab-label-content" id="tab2-content">
<label for="tab2" class="label1">Daily View</label>
<div class="tab-content mdl-shadow--4dp">
<div class="calendar" ng-model="eventSources" calendar="myCalendar3" ui-calendar="uiConfig.calendar"></div>
</div>
</div>
<div class="slide"></div>
</div>
</div>
thanks in advance..!!

Angular: data read is undefined when trying to use pagination

I am trying to paginate a potentially very long list of divs. I have things working fine without the pagination however when I try to implement pagination (using this as a sort of template: http://plnkr.co/edit/81fPZxpnOQnIHQgp957q?p=preview) I receive an error: TypeError: cannot read property 'slice' of undefined . As far as I can tell this is an issue involving the $scope.data variable since I am calling slice on $scope.data . I am not sure how to resolve this and get it working. I will post the working version without pagination followed by the erroneous version with pagination. The only differences in the controller are from the line filteredTodos onward. I am calling fetchAllSamples() which populated $scope.data before any other work is done with pagination so I'm not sure why it would be undefined. All help is much appreciated.
Erroneous pagination html:
<div>
<div>
<div class="col-md-12">
<div style="border: 1px solid #000000">
<div ng-repeat="item in filteredTodos" style="margin-left: 14px">
{{item.name}}
<br> <span style="font-size: 20px">{{item.description}}</span>
<br>
<hr>
</div>
</div>
</div>
<pagination ng-model="currentPage" total-items="data.length" max-size="maxSize" boundary-links="true"> </pagination>
</div>
</div>
and correct non-paginated:
<div>
<div>
<div class="col-md-12">
<div style="border: 1px solid #000000">
<div ng-repeat="item in data" style="margin-left: 14px">
{{item.name}}
<br> <span style="font-size: 20px">{{item.description}}</span>
<br>
<hr>
</div>
</div>
</div>
</div>
</div>
which use the following controller:
app.controller('SamplesQueryController','$scope','$http', function($scope, $http) {
$scope.newSampleName = {
sampleName: ''
};
$scope.attributes = [{
name: '',
value: ''
}];
$scope.sampleCount = 0;
$scope.addAttribute = function() {
var attribute = {
name: '',
value: ''
};
$scope.attributes.push(attribute);
}
$scope.showModal = false;
$scope.toggleModal = function() {
$scope.showModal = !$scope.showModal;
};
$scope.headerCount = 0;
$scope.headers = new Array("Id", "Name", "URL", "Description");
$scope.fetchAllSamples = function() {
$scope.response = null;
$scope.method = 'GET';
$scope.url = '/api/samples';
$http({
method: $scope.method,
url: $scope.url
}).then(function(response) {
$scope.data = response.data;
angular.forEach(response.data,function(value,key) {
$scope.sampleCount += 1;
});
},
function(response) {
$scope.data = response.data || "Request failed";
}
);
};
$scope.createSample = function() {
$scope.response = null;
$scope.method = 'POST';
$scope.url = '/api/samples';
$http({
method: $scope.method,
url: $scope.url,
data: JSON.stringify({
name: $scope.newSampleName.sampleName,
attributes: $scope.attributes
})
}).then(function(response) {
$scope.fetchAllSamples();
});
};
$scope.fetchAllSamples();
$scope.filteredTodos = [], $scope.currentPage = 1, $scope.numPerPage = 10, $scope.maxSize = 5;
$scope.$watch('currentPage + numPerPage', function() {
var begin = (($scope.currentPage - 1) * $scope.numPerPage),
end = begin + $scope.numPerPage;
$scope.filteredTodos = $scope.data.slice(begin, end);
});
}]);

Open a Pop up window on the click of link on Kendo Grid column

I want to open a Pop Up window on the click of a link on a Kendo Grid column. The Pop window should contain the detailed data of the current row. Something like given in http://demos.telerik.com/kendo-ui/grid/custom-command link. But I want this to work on click of a link of an existing column rather than a new custom button.
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<div id="grid" kendo-grid k-options="kendoGrid"></div>
</div>
</div>
Controller:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', function ($scope, myService) {
$scope.kendoGrid = myService.getKGrid();
});
Service:
myApp.service('myService', function () {
this.getKGrid = function () {
var kGrid = {
dataSource: [{"Col1":"Val1","Col2":"Val2"}],
columns: [{
field: "Col1",
title: "Col1"
},
{
field: "Col2",
title: "Col2",
template: "<a href='\\#' class='link' **ng-click='TBD(Open Pop-up window with row details)'**>#=Col2#</a>"
}
]
};
return kGrid;
};
});
Please guide how to achieve this.
Thanks in Advance.
I am able to display the pop up kendo window with row details by making below changes.
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<div id="grid" kendo-grid k-options="kendoGrid"></div>
<div id="details"></div>
<div id="detailsTemplate" style="display: none;">
<div class="row">
<text>Data :</text> <text>#=Col1#</text><!--We can add more data here-->
</div>
</div>
</div>
</div>
Controller:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', function ($scope, myService) {
$scope.kendoGrid = myService.getKGrid();
$scope.showDetails = function (dataItem) {
myService.showWindow(dataItem);
}
});
Service:
myApp.service('myService', function () {
this.showWindow function () {
var window = angular.element(details)
.kendoWindow({
title: "Details",
modal: true,
visible: false,
resizable: false,
width: 600
}).data("kendoWindow");
var detailsTemplate = kendo.template(angular.element(detailsTemplate).html());
window.content(detailsTemplate(dataItem));
window.center().open();
};
this.getKGrid = function () {
var kGrid = {
dataSource: [{"Col1":"Val1","Col2":"Val2"}],
columns: [{
field: "Col1",
title: "Col1"
},
{
field: "Col2",
title: "Col2",
template: "<a href='\\#' class='link' ng-click='showDetails(dataItem)'>#=Col2#</a>"
}
]
};
return kGrid;
};
});

Resources