Global.helma.Html.TableWriter (numberOfColumns, attr)
utility object to provide an easy way for programmatically creating an x/html table.

  • Parameter Number:
    the number of columns in the table
  • Parameter Object:
    the table's, its rows' and cells' attributes
  • Returns
    Object an instance of TableWriter
Sourcecode in /home/hannes/helma/modules/helma/Html.js:
562:   this.TableWriter = function(numberOfColumns, attr) {
563:           this.ncols = numberOfColumns;
564:           if (isNaN(this.ncols))
565:               throw "Illegal argument in TableWriter(): first argument must be a number";
566:           if (this.ncols < 1)
567:               throw "Illegal argument in TableWriter(): first argument must be > 1";
568:           this.written = 0;
569:           // if no attributes object given, create an empty one
570:           if (!attr)
571:              attr = {};
572:          if (!attr.trEven) attr.trEven = attr.tr;
573:          if (!attr.trOdd)  attr.trOdd = attr.tr;
574:          if (!attr.trHead) attr.trHead = attr.trEven;
575:          if (!attr.tdEven) attr.tdEven = attr.td;
576:          if (!attr.tdOdd)  attr.tdOdd = attr.td;
577:          if (!attr.thEven) attr.thEven = attr.th;
578:          if (!attr.thOdd)  attr.thOdd = attr.th;
579:          this.attr = attr;
580:  
581:          // write header row? set to true to use "th" tags for first row
582:          this.writeHeader = false;
583:          // write to string buffer rather than response?
584:          this.writeString = false;
585:  
586:          /**
587:           * Write a table cell.
588:           * @param String the table cell content as text
589:           * @param attr an optional attributes holder for the td tag
590:           */
591:          this.write = function(text, attr) {
592:              // set up some variables
593:              var isHeaderRow = (this.writeHeader && this.written < this.ncols);
594:              var isNewRow = (this.written % this.ncols == 0);
595:              var isEvenRow = ((this.written / this.ncols) % 2 == 0);
596:              var isEvenCol = ((this.written % this.ncols) % 2 == 0);
597:              // write out table and table row tags
598:              if (this.written == 0) {
599:                  if (this.writeString)
600:                      res.push();
601:                  self.openTag("table", this.attr.table);
602:                  self.openTag("tr", this.attr.trHead);
603:              } else if (isNewRow) {
604:                  self.closeTag("tr");
605:                  if (isEvenRow)
606:                       self.openTag("tr", this.attr.trEven);
607:                  else
608:                       self.openTag("tr", this.attr.trOdd);
609:              }
610:              // get the attribute object for the table cell
611:              if (!attr) {
612:                  // no explicit attribute given
613:                  if (isEvenCol)
614:                      attr = isHeaderRow ? this.attr.thEven : this.attr.tdEven;
615:                  else
616:                      attr = isHeaderRow ? this.attr.thOdd : this.attr.tdOdd;
617:              }
618:              // write out table cell tag
619:              self.openTag(isHeaderRow ? "th" : "td", attr);
620:              // write out table cell contents
621:              if (text)
622:                  res.write(text);
623:              // close table cell
624:              self.closeTag(isHeaderRow ? "th" : "td");
625:              if (attr && !isNaN(attr.colspan))
626:                  this.written += attr.colspan;
627:              else
628:                  this.written += 1;
629:              return;
630:          };
631:  
632:          /**
633:           * Close all open table tags.
634:           */
635:          this.close = function() {
636:              if (this.written > 0) {
637:                  while (this.written++ % this.ncols != 0)
638:                      res.write("<td></td>");
639:                  res.write("</tr></table>");
640:                  this.written = 0;
641:              }
642:              if (this.writeString)
643:                  return res.pop();
644:              return;
645:          };
646:  
647:          this.dontEnum("ncols", "written", "attr", "writeHeader");
648:          this.dontEnum("writeString", "write", "close");
649:  
650:          return this;
651:      }