1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0//EN">
6 <style type=
"text/css">
9 font-family: verdana
, sans-serif
;
29 text-decoration:underline
;
34 text-decoration: none
;
47 border-bottom-width:1px;
48 border-bottom-style:dotted
;
64 <table width=
"90%" border=
"0" cellspacing=
"1" cellpadding=
"5">
67 <big><tt>Global.helma.Markup.TableWriter
(numberOfColumns,
attr)
</tt></big><br>
73 utility object to provide an easy way
74 for programmatically creating an x/html table.
<br><br>
77 <li><b>Parameter
</b> <code>Number
</code>:
<br/>
78 the number of columns in the table
<li><b>Parameter
</b> <code>Object
</code>:
<br/>
79 the table's, its rows' and cells' attributes
80 <li><b>Returns
</b><br>
81 Object an instance of TableWriter
92 <table width=
"90%" border=
"0" cellspacing=
"1" cellpadding=
"5">
94 <td>Sourcecode in helmaTools.zip/Global/helma.Markup.js:
95 <pre><font color=
"#aaaaaa">426:
</font> helma.Markup.TableWriter = function(numberOfColumns, attr) {
96 <font color=
"#aaaaaa">427:
</font> this.ncols = numberOfColumns;
97 <font color=
"#aaaaaa">428:
</font> if (isNaN(this.ncols))
98 <font color=
"#aaaaaa">429:
</font> throw
<font color=
"#9999aa">"Illegal argument in TableWriter(): first argument must be a number
"</font>;
99 <font color=
"#aaaaaa">430:
</font> if (this.ncols
< 1)
100 <font color=
"#aaaaaa">431:
</font> throw
<font color=
"#9999aa">"Illegal argument in TableWriter(): first argument must be
> 1"</font>;
101 <font color=
"#aaaaaa">432:
</font> this.written =
0;
102 <font color=
"#aaaaaa">433:
</font> <font color=
"#33aa00"> // if no attributes object given, create an empty one
103 <font color=
"#aaaaaa">434:
</font> </font> if (!attr)
104 <font color=
"#aaaaaa">435:
</font> attr = {};
105 <font color=
"#aaaaaa">436:
</font> if (!attr.trEven) attr.trEven = attr.tr;
106 <font color=
"#aaaaaa">437:
</font> if (!attr.trOdd) attr.trOdd = attr.tr;
107 <font color=
"#aaaaaa">438:
</font> if (!attr.trHead) attr.trHead = attr.trEven;
108 <font color=
"#aaaaaa">439:
</font> if (!attr.tdEven) attr.tdEven = attr.td;
109 <font color=
"#aaaaaa">440:
</font> if (!attr.tdOdd) attr.tdOdd = attr.td;
110 <font color=
"#aaaaaa">441:
</font> if (!attr.thEven) attr.thEven = attr.th;
111 <font color=
"#aaaaaa">442:
</font> if (!attr.thOdd) attr.thOdd = attr.th;
112 <font color=
"#aaaaaa">443:
</font> this.attr = attr;
113 <font color=
"#aaaaaa">444:
</font>
114 <font color=
"#aaaaaa">445:
</font> <font color=
"#33aa00"> // write header row? set to true to use
<font color=
"#9999aa">"th
"</font> tags for first row
115 <font color=
"#aaaaaa">446:
</font> </font> this.writeHeader = false;
116 <font color=
"#aaaaaa">447:
</font> <font color=
"#33aa00"> // write to string buffer rather than response?
117 <font color=
"#aaaaaa">448:
</font> </font> this.writeString = false;
118 <font color=
"#aaaaaa">449:
</font>
119 <font color=
"#aaaaaa">450:
</font> /**
120 <font color=
"#aaaaaa">451:
</font> * Write a table cell.
121 <font color=
"#aaaaaa">452:
</font> * @param String the table cell content as text
122 <font color=
"#aaaaaa">453:
</font> * @param attr an optional attributes holder for the td tag
123 <font color=
"#aaaaaa">454:
</font> */
124 <font color=
"#aaaaaa">455:
</font> this.write = function(text, attr) {
125 <font color=
"#aaaaaa">456:
</font> <font color=
"#33aa00"> // set up some variables
126 <font color=
"#aaaaaa">457:
</font> </font> var isHeaderRow = (this.writeHeader
&& this.written
< this.ncols);
127 <font color=
"#aaaaaa">458:
</font> var isNewRow = (this.written % this.ncols ==
0);
128 <font color=
"#aaaaaa">459:
</font> var isEvenRow = ((this.written / this.ncols) %
2 ==
0);
129 <font color=
"#aaaaaa">460:
</font> var isEvenCol = ((this.written % this.ncols) %
2 ==
0);
130 <font color=
"#aaaaaa">461:
</font> <font color=
"#33aa00"> // write out table and table row tags
131 <font color=
"#aaaaaa">462:
</font> </font> if (this.written ==
0) {
132 <font color=
"#aaaaaa">463:
</font> if (this.writeString)
133 <font color=
"#aaaaaa">464:
</font> res.push();
134 <font color=
"#aaaaaa">465:
</font> helma.Markup.openTag(
<font color=
"#9999aa">"table
"</font>, this.attr.table);
135 <font color=
"#aaaaaa">466:
</font> helma.Markup.openTag(
<font color=
"#9999aa">"tr
"</font>, this.attr.trHead);
136 <font color=
"#aaaaaa">467:
</font> } else if (isNewRow) {
137 <font color=
"#aaaaaa">468:
</font> helma.Markup.closeTag(
<font color=
"#9999aa">"tr
"</font>);
138 <font color=
"#aaaaaa">469:
</font> if (isEvenRow)
139 <font color=
"#aaaaaa">470:
</font> helma.Markup.openTag(
<font color=
"#9999aa">"tr
"</font>, this.attr.trEven);
140 <font color=
"#aaaaaa">471:
</font> else
141 <font color=
"#aaaaaa">472:
</font> helma.Markup.openTag(
<font color=
"#9999aa">"tr
"</font>, this.attr.trOdd);
142 <font color=
"#aaaaaa">473:
</font> }
143 <font color=
"#aaaaaa">474:
</font> <font color=
"#33aa00"> // get the attribute object for the table cell
144 <font color=
"#aaaaaa">475:
</font> </font> if (!attr) {
145 <font color=
"#aaaaaa">476:
</font> <font color=
"#33aa00"> // no explicit attribute given
146 <font color=
"#aaaaaa">477:
</font> </font> if (isEvenCol)
147 <font color=
"#aaaaaa">478:
</font> attr = isHeaderRow ? this.attr.thEven : this.attr.tdEven;
148 <font color=
"#aaaaaa">479:
</font> else
149 <font color=
"#aaaaaa">480:
</font> attr = isHeaderRow ? this.attr.thOdd : this.attr.tdOdd;
150 <font color=
"#aaaaaa">481:
</font> }
151 <font color=
"#aaaaaa">482:
</font> <font color=
"#33aa00"> // write out table cell tag
152 <font color=
"#aaaaaa">483:
</font> </font> helma.Markup.openTag(isHeaderRow ?
<font color=
"#9999aa">"th
"</font> :
<font color=
"#9999aa">"td
"</font>, attr);
153 <font color=
"#aaaaaa">484:
</font> <font color=
"#33aa00"> // write out table cell contents
154 <font color=
"#aaaaaa">485:
</font> </font> if (text)
155 <font color=
"#aaaaaa">486:
</font> res.write(text);
156 <font color=
"#aaaaaa">487:
</font> <font color=
"#33aa00"> // close table cell
157 <font color=
"#aaaaaa">488:
</font> </font> helma.Markup.closeTag(isHeaderRow ?
<font color=
"#9999aa">"th
"</font> :
<font color=
"#9999aa">"td
"</font>);
158 <font color=
"#aaaaaa">489:
</font> if (attr
&& !isNaN(attr.colspan))
159 <font color=
"#aaaaaa">490:
</font> this.written += attr.colspan;
160 <font color=
"#aaaaaa">491:
</font> else
161 <font color=
"#aaaaaa">492:
</font> this.written +=
1;
162 <font color=
"#aaaaaa">493:
</font> return;
163 <font color=
"#aaaaaa">494:
</font> };
164 <font color=
"#aaaaaa">495:
</font>
165 <font color=
"#aaaaaa">496:
</font> /**
166 <font color=
"#aaaaaa">497:
</font> * Close all open table tags.
167 <font color=
"#aaaaaa">498:
</font> */
168 <font color=
"#aaaaaa">499:
</font> this.close = function() {
169 <font color=
"#aaaaaa">500:
</font> if (this.written
> 0) {
170 <font color=
"#aaaaaa">501:
</font> while (this.written++ % this.ncols !=
0)
171 <font color=
"#aaaaaa">502:
</font> res.write(
<font color=
"#9999aa">"<td
></td
>"</font>);
172 <font color=
"#aaaaaa">503:
</font> res.write(
<font color=
"#9999aa">"</tr
></table
>"</font>);
173 <font color=
"#aaaaaa">504:
</font> this.written =
0;
174 <font color=
"#aaaaaa">505:
</font> }
175 <font color=
"#aaaaaa">506:
</font> if (this.writeString)
176 <font color=
"#aaaaaa">507:
</font> return res.pop();
177 <font color=
"#aaaaaa">508:
</font> return;
178 <font color=
"#aaaaaa">509:
</font> };
179 <font color=
"#aaaaaa">510:
</font> return this;
180 <font color=
"#aaaaaa">511:
</font> }