-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.js
99 lines (85 loc) · 1.96 KB
/
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
/*
table.js is a library for DOM element generator.
*/
/* tr node */
var tr = function tr(){
this.tr = new Object();
this.env = new Object();
return this;
}
tr.prototype.th = function(th) {
var head = new RAM().get('head');
for (var i = th.length - 1; i >= 0; i--) {
th[i] = "<th class='"+head.th+"'>"+ th[i]+"</th>";
}
this.tr.e=th;
this.env.isHead = true;
return this;
};
tr.prototype.td = function(td) {
var body = new RAM().get('body');
for (var i = td.length - 1; i >= 0; i--) {
td[i] = "<td class='"+body.td+"'>"+ td[i]+"</td>";
}
this.tr.e = td;
this.env.isHead = false;
return this;
};
tr.prototype.make = function() {
var node = "";
if (this.env.isHead) {
var head = new RAM().get('head');
node = "<tr class='"+head.tr+"'>";
}else{
var body = new RAM().get('body');
node = "<tr class='"+body.tr+"'>";
}
for (var i = this.tr.e.length - 1; i >= 0; i--) {
node += this.tr.e[i];
}
node += "</tr>";
return node;
};
/* tr node end */
/* table object */
var table = function table(tableID){
this.tableID = tableID;
this.trs = [];
return this;
}
table.prototype.css = function(){
return this;
};
table.prototype.head = function(styleCalss){
new RAM().set('head',styleCalss);
return this;
};
table.prototype.body = function(styleCalss) {
new RAM().set('body',styleCalss);
return this;
};
table.prototype.tr = new tr();
table.prototype.install = function() {
var html = "";
for (var i = this.trs.length - 1; i >= 0; i--) {
html += this.trs[i];
}
document.getElementById(this.tableID).innerHTML = html;
};
/* table object end */
// the main function
function iidoTable(tableID){
return new table(tableID);
}
/* demo
<table id="page">
</table>
<script>
var th = ['订单ID',"数量","总金额","日期","订单状态"];
var td = ['123618273786',"222","2345315","2017-9-30","已发货"];
var table = new iidoTable('page');
table.trs.push(table.tr.td(td).make());
table.trs.push(table.tr.th(th).make());
table.install();
</script>
*/