Creating Rows
[Previous] [Main] [Next]

 
Adding row objects
Table rows are created using htmlTableRow. You can add as many rows as you like to a table, subject to memory limitations. Even though you can add a practically limitless number of rows, the user's browser may have a limit, plus the user may not be willing to wait for an extremely long document to download. For these reasons, we suggest restraining table size to a few pages at most.  
 
Implicit conversion of rows
Row objects are added to htmlTable objects using the << insertion operator or the Add() method. htmlTable overrides the Add() method (and thus the << operator) so that any object added to a table is implicity converted to an htmlTableRow object if necessary. This feature makes it convenient to simply add items to a table using << without instantiating htmlTableRow directly (see Example 1 below).  
 
htmlTable  table ;  
table << "This is row one"  
      << "This is row two"  
      << "This is row three" ;  
 
 
Although you can add row objects directly (or implicitly) to a table, unless the table contains just one column you'll want to use htmlTableCell to produce columnar tables.  
 
htmlTable     table ;  
htmlTableRow  row1 ;  
htmlTableRow  row2 ;  
 
row1 << htmlTableCell( "row1,col1" )  
     << htmlTableCell( "row1,col2" ) ;  
 
row2 << htmlTableCell( "row2,col1" )  
     << htmlTableCell( "row2,col2" ) ;  
 
table << row1 << row2 ;  
 
Implicit conversion of cells
Similarly to htmlTable, htmlTableRow will implicity convert objects added to it to htmlTableCell objects if necessary. This makes it convenient to add text as cells to rows without explicitly using htmlTableCell.  
 
If the information for the cells is readily available, it's often convenient to use nested parentheses to create an intermediate htmlTableRow object in-line, adding cells to it a single statement (see Example 2 below):  
 
htmlTable  table ;  
table << ( htmlTableRow() << "row1,col1" << "row1,col2" )  
      << ( htmlTableRow() << "row2,col1" << "row2,col2" ) ;  
 
 
Example One
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "html++ example application" ) ;  
 
   htmlTable  table ;  
   table.Border(3).Align(align_center).CellPadding(10) ;  
 
   //  The text objects will be implicitly converted to  
   //  htmlTableRow objects (which will add the text as  
   //  htmlTableCell objects), saving us the trouble and  
   //  making the code more readable. The result will be  
   //  a one-column table.  
   table << "This is row one"  
         << "This is row two"  
         << "This is row three" ;  
 
   page << table ;  
 
   server << page ;  
   return 0 ;  
}  
 
Program One Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
</HEAD>  
<BODY>  
<TABLE BORDER="3" ALIGN="CENTER" CELLPADDING="10">  
<TR><TD>This is row one</TD>  
</TR>  
<TR><TD>This is row two</TD>  
</TR>  
<TR><TD>This is row three</TD>  
</TR>  
</TABLE>  
</BODY>  
</HTML>  
 

ex_tablerow1.gif  

 
 
 
Example Two
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlCgi   server ;  
   htmlPage  page( "html++ example application" ) ;  
 
   htmlTable  table ;  
   table.Border(3).Align(align_center).CellPadding(10) ;  
 
   //  The text objects will be implicitly converted to  
   //  htmlTableCell objects, saving us the trouble and  
   //  making the code more readable. The result will be  
   //  a 2-row X 2-column table.  
table << (htmlTableRow() << "row1,col1" << "row1,col2" )  
      << (htmlTableRow() << "row2,col1" << "row2,col2" );  
 
   page << table ;  
 
   server << page ;  
   return 0 ;  
}  
 
Program Two Output
 
Content-Type: text/html  
<HTML>  
<HEAD>  
<TITLE>html++ example application</TITLE>  
</HEAD>  
<BODY>  
<TABLE BORDER="3" ALIGN="CENTER" CELLPADDING="10">  
<TR><TD>row1,col1</TD>  
<TD>row1,col2</TD>  
</TR>  
<TR><TD>row2,col1</TD>  
<TD>row2,col2</TD>  
</TR>  
</TABLE>  
</BODY>  
</HTML>  
 

ex_tablerow2.gif  

 


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.