-
Notifications
You must be signed in to change notification settings - Fork 1
/
work.js
170 lines (152 loc) · 4.92 KB
/
work.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
166
167
168
169
170
//////////////////////////////////////////////////////////////
// //
// //
// Generating Excel Sheet Using Native Javascript //
// //
// //
// //
//////////////////////////////////////////////////////////////
//Initialisation //
var table = {
rowcount :0,
columncount :0,
addNewRow : function () {
this.rowcount++;
},
addNewColumn : function() {
this.columncount++;
},
delNewRow : function () {
this.rowcount--;
},
delNewColumn : function() {
this.columncount--;
},
drawTable : function(a,b) {
if(typeof a==='undefined') this.rowcount = 10;
else this.rowcount=a;
if(typeof b==='undefined') this.columncount = 15;
else this.columncount=b;
draw();
}
};
// Function to Draw the table based on the Values Provided.
var draw = function() {
var tableHead = document.createElement('th');
tableHead.setAttribute('id','hid-');
document.getElementById('heading').appendChild(tableHead);
document.getElementById('hid-').appendChild(document.createTextNode("Nos"));
for (var i = 0; i < table.columncount; i++) {
var tableHead = document.createElement('th');
tableHead.setAttribute('id','hid-'+i);
tableHead.setAttribute('onclick','sortData('+i+')');
var check = i,
cap = [];
while(check>=0) {
cap.unshift(String.fromCharCode(65 + (check%26)));
check = (check/26) - 1;
}
textnode=document.createTextNode(cap.join(''));
document.getElementById('heading').appendChild(tableHead);
document.getElementById('hid-'+i).appendChild(textnode);
}
for (var i= 0;i<table.rowcount;i++){
addrowcode(i);
}
};
// Function to add one Row based on Number of Columns
var addrowcode = function(i) {
var newRows = document.createElement('tr');
newRows.setAttribute('id','id-'+i);
document.getElementById('dynamictable').appendChild(newRows);
var cell = document.createElement('td');
cell.setAttribute('id','htdid-' + i);
document.getElementById('id-'+i).appendChild(cell);
document.getElementById('htdid-'+i).appendChild(document.createTextNode(i));
for (var j = 0; j < table.columncount; j++) {
var cell = document.createElement('td');
cell.setAttribute('id','id-'+i+'-'+j);
document.getElementById('id-'+i).appendChild(cell);
var inputCell = document.createElement('input');
inputCell.setAttribute('type','text');
inputCell.setAttribute('onclick','this.select()');
inputCell.setAttribute('class','form-control');
document.getElementById('id-'+i+'-'+j).appendChild(inputCell);
};
};
// Function to Add a New Column
var addNewColumn = function(){
var j = table.columncount,
check = j,
cap = [],
newHead = document.createElement('th');
newHead.setAttribute('id','hid-'+j);
while(check>=0) {
cap.unshift(String.fromCharCode(65 + (check%26)));
check = check/26 - 1;
}
var textnode = document.createTextNode(cap.join(''));
document.getElementById('heading').appendChild(newHead).appendChild(textnode);
for (var i = 0; i < table.rowcount; i++) {
var newCell = document.createElement('td');
newCell.setAttribute('id','id-'+i+'-'+j);
document.getElementById('id-'+i).appendChild(newCell);
var inputCell = document.createElement('input');
inputCell.setAttribute('type','text');
inputCell.setAttribute('onclick','this.select()');
inputCell.setAttribute('class','form-control');
document.getElementById('id-'+i+'-'+j).appendChild(inputCell);
}
table.addNewColumn();
};
// Function to Delete the Last Column.
// Code can be modified to delete a specific column.
var delColumn = function() {
i = table.columncount - 1;
document.getElementById('hid-'+i).remove();
for (var j = 0; j < table.rowcount; j++)
{
document.getElementById('id-'+j+'-'+i).remove();
}
table.delNewColumn();
};
//function to Add new Row at the Last
var addNewRow = function() {
i = table.rowcount;
addrowcode(i);
table.addNewRow();
};
// Function to Delete Last Row
var delRow = function() {
i = table.rowcount - 1;
document.getElementById('id-'+i).remove();
table.delNewRow();
};
// A very simple sorting technique. Can only
// Sort a specific Column.
var sortData = function(j){
var tsort = [],len;
for(var i =0;i<table.rowcount; i++) {
var id = 'id-' + i + '-' + j;
var data = document.getElementById(id).getElementsByTagName('input')[0].value;
if(data != '') tsort.push(data);
}
len = tsort.length;
if(len==0) {
alert('Empty Column');
return;
}
// Sort function called twice to erase the condition of
// either only sorting a number or character.
tsort = tsort.sort();
tsort = tsort.sort(function(a,b){
return a - b;
});
for(var i=0;i<table.rowcount;i++) {
var id = 'id-' + i + '-' + j;
if(i<len)
document.getElementById(id).getElementsByTagName('input')[0].value = tsort[i];
else
document.getElementById(id).getElementsByTagName('input')[0].value = '';
}
}