-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-table.js
165 lines (151 loc) · 5.96 KB
/
one-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
var app = angular.module('oneTableApp', []);
(function(app) {
app.controller('oneTableCtrl', oneTableCtrl);
oneTableCtrl.$inject = ['$scope', '$timeout', 'excelExportService'];
function oneTableCtrl($scope, $timeout, excelExportService) {
//variable declaration
var globalIndex = 0;
$scope.headerSource = $scope.oneTableProperties.headerSource;
$scope.dataSource = $scope.oneTableProperties.dataSource;
$scope.getCommaSeparatedValue = function(data) {
if (!isNaN(data) && data != null && data != '' && $scope.oneTableProperties.commaSeparated)
return parseFloat(data).toLocaleString();
else if (isNaN(data) || data != null)
return data;
else if (data == null)
return '';
else
return 0;
}
if ($scope.oneTableProperties.grandTotal)
getSumOfData();
$scope.pushNewSubTotalObject = function(index) {
var currentObj = {};
var nextObj = {};
var totalObj = {};
currentObj = $scope.tempData[index];
nextObj = $scope.tempData[index + 1];
if (nextObj == undefined || (currentObj[$scope.oneTableSubTotalColumn] != nextObj[$scope.oneTableSubTotalColumn])) {
totalObj = Object.create(currentObj);
for (var k in totalObj) {
if (!isNaN(totalObj[k]) && totalObj[k] != null)
totalObj[k] = 0;
else if (isNaN(totalObj[k]) && totalObj[k] != null)
totalObj[k] = '';
}
$.each($scope.tempData, function(key, val) {
if (val[$scope.headerSource[0]] == currentObj[$scope.oneTableSubTotalColumn]) {
for (var k in val) {
if (!isNaN(val[k]) && val[k] != null) {
totalObj[k] += parseFloat(val[k]);
} else if (k == $scope.oneTableSubTotalColumn && isNaN(val[k])) {
totalObj[k] = 'Sub-Total';
} else if (isNaN(val[k]) && val[k] != null) {
totalObj[k] = null;
}
}
}
})
index = globalIndex + index;
totalObj['background'] = '#ffc56f';
totalObj['fontweight'] = 'bold';
$scope.dataSource.splice(index + 1, 0, totalObj);
globalIndex++;
}
}
function getSumOfData() {
var total = 0;
$scope.oneTableTotal = [];
for (var i = 0; i < $scope.headerSource.length; i++) {
total = 0;
$.each($scope.dataSource, function(k, v) {
if (!isNaN(v[$scope.headerSource[i]]) && v[$scope.headerSource[i]] != null) {
total += parseFloat(v[$scope.headerSource[i]]);
} else if (i == 0 && isNaN(v[$scope.headerSource[i]])) {
$scope.oneTableTotal.push('Grand-Total');
return false;
}
})
if (total > 0 && i > 0) {
if ($scope.oneTableProperties.commaSeparated)
$scope.oneTableTotal.push(total.toLocaleString());
else
$scope.oneTableTotal.push(total);
} else if (i > 0) {
$scope.oneTableTotal.push('');
}
}
}
//Excel Export
$scope.exportToExcel = function() {
var exportHref = excelExportService.tableToExcel('#' + $scope.oneTableProperties.name, 'Exported Data');
$timeout(function() {
location.href = exportHref;
}, 100);
}
}
app.directive('oneTable', oneTable);
function oneTable() {
return {
restrict: 'E',
replace: true,
scope: {
oneTableProperties: "="
},
link: function(scope, elem, attrs) {},
controller: 'oneTableCtrl',
controllerAs: 'ctrl',
template: '<div>' +
'<button class="btn btn-link pull-right" ng-click="exportToExcel();" ng-if="oneTableProperties.exportToExcel == true">Export to Excel</button>' +
'<br/>' +
'<table ng-class="oneTableProperties.cssClass" id="{{oneTableProperties.name}}" style="font-size:13px;">' +
'<thead>' +
'<tr style="background-color: #605ca8; color: white; ">' +
'<th ng-repeat="header in oneTableProperties.headerSource">' +
'{{header}}' +
'</th>' +
'</tr>' +
'</thead>' +
'<tbody>' +
'<tr ng-repeat="data in oneTableProperties.dataSource" style="background-color:{{data[' + "'" + 'background' + "'" + ']}}; font-weight:{{data[' + "'" + 'fontweight' + "'" + ']}};">' +
'<td ng-repeat="header in oneTableProperties.headerSource">' +
'{{getCommaSeparatedValue(data[header])}}' +
'</td>' +
'</tr>' +
'<tr style="background-color:#605ca8; font-weight: bold; color: white;" ng-if="oneTableProperties.grandTotal == true">' +
'<td ng-repeat="total in oneTableTotal track by $index">' +
'<b>{{total}}</b>' +
'</td>' +
'</tr>' +
'</tbody>' +
'</table>' +
'</div>'
}
}
app.factory('excelExportService', excelExportService);
excelExportService.$inject = ['$window'];
function excelExportService($window) {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
base64 = function(s) {
return $window.btoa(unescape(encodeURIComponent(s)));
},
format = function(s, c) {
return s.replace(/{(\w+)}/g, function(m, p) {
return c[p];
})
};
return {
tableToExcel: function(tableId, worksheetName) {
var table = $(tableId),
ctx = {
worksheet: worksheetName,
table: table.html()
},
href = uri + base64(format(template, ctx));
return href;
}
};
}
})(angular.module('oneTableApp'));