HTML Table Library

The Table Library provides functions that enable you to auto-generate HTML tables from arrays or database result sets.

Initializing the Library

Like most other libraries in revIgniter, the Table library is initialized in your controller using the loaderLoadLibrary handler:

loaderLoadLibrary "Table"

Examples:

Here is an example showing how you can create a table from a multi-dimensional array. Note that the first array index will become the table heading (or you can set your own headings using the setTableHeading handler described in the method reference below).

loaderLoadLibrary "Table"

put "Name" into tData[1][1]
put "Color" into tData[1][2]
put "Size" into tData[1][3]

put "Fred" into tData[2][1]
put "Blue" into tData[2][2]
put "Small" into tData[2][3]

put "Mary" into tData[3][1]
put "Red" into tData[3][2]
put "Large" into tData[3][3]

put "John" into tData[4][1]
put "Green" into tData[4][2]
put "Medium" into tData[4][3]

put generateTable(tData) into gData["testTable"]

Here is an example of a table created from a database query result. The Table library will automatically generate the headings based on the table names (or you can set your own headings using the setTableHeading handler described in the method reference below).

loaderLoadLibrary "Table"

put dbQuery("SELECT * FROM mytable") into tQuery
put tQuery["resultarray"] into tResult

put generateTable(tResult) into gData["testTable"]

Here is an example showing how you might create a table using discrete parameters:

loaderLoadLibrary "Table"

setTableHeading "Name,Color,Size"

addTableRow "Fred,Blue,Small"
addTableRow "Mary,Red,Large"
addTableRow "John,Green,Medium"

put generateTable() into gData["testTable"]

Here is the same example, except instead of individual parameters, arrays are used:

loaderLoadLibrary "Table"

put "Name" into tHeading[1]
put "Color" into tHeading[2]
put "Size" into tHeading[3]
setTableHeading tHeading

put "Fred" into tRow[1]
put "Blue" into tRow[2]
put "Small" into tRow[3]
addTableRow tRow

put "Mary" into tRow[1]
put "Red" into tRow[2]
put "Large" into tRow[3]
addTableRow tRow

put "John" into tRow[1]
put "Green" into tRow[2]
put "Medium" into tRow[3]
addTableRow tRow

put generateTable() into gData["testTable"]

Changing the Look of Your Table

The Table Library permits you to set a table template with which you can specify the design of your layout. Here is the template prototype:

put "<table border=" & quote & "0" & quote && "cellpadding=" & quote & "4" & quote && "cellspacing=" & quote & "0" & quote & ">" into tTmpl["tableOpen"]
put "<tr>" into tTmpl["headingRowStart"]
put "</tr>" into tTmpl["headingRowEnd"]
put "<th>" into tTmpl["headingCellStart"]
put "</th>" into tTmpl["headingCellEnd"]

put "<tr>" into tTmpl["rowStart"]
put "</tr>" into tTmpl["rowEnd"]
put "<td>" into tTmpl["cellStart"]
put "</td>" into tTmpl["cellEnd"]

put "<tr>" into tTmpl["rowAltStart"]
put "</tr>" into tTmpl["rowAltEnd"]
put "<td>" into tTmpl["cellAltStart"]
put "</td>" into tTmpl["cellAltEnd"]

put "</table>" into tTmpl["tableClose"]

setTableTemplate tTmpl

Note:  You'll notice there are two sets of "row" blocks in the template. These permit you to create alternating row colors or design elements that alternate with each iteration of the row data.

You are NOT required to submit a complete template. If you only need to change parts of the layout you can simply submit those elements. In this example, only the table opening tag is being changed:

put "<table border=" & quote & "1" & quote && "cellpadding=" & quote & "2" & quote && "cellspacing=" & quote & "1" & quote && "class=" & quote & "mytable" & quote & ">" into tTmpl["tableOpen"]

setTableTemplate tTmpl

Method Reference

generateTable()

Returns a string containing the generated table. Accepts an optional parameter which can be an array like a database query result array.

setTableCaption

Permits you to add a caption to the table.

setTableCaption "Colors"

setTableHeading

Permits you to set the table heading. You can submit an array or discrete params:

setTableHeading "Name,Color,Size" put "Name" into tHeading[1]
put "Color" into tHeading[2]
put "Size" into tHeading[3]

setTableHeading tHeading

addTableRow

Permits you to add a row to your table. You can submit an array or discrete params:

addTableRow "Blue,Red,Green" put "Blue" into tRow[1]
put "Red" into tRow[2]
put "Green" into tRow[3]

addTableRow tRow

makeTableColumns()

This function takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:

put "one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve" into tList
split tList using comma

put makeTableColumns(tList, 3) into tNewList

put generateTable(tNewList) into gData["testTable"]

// Generates a table with this prototype

<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr><tr>
<td>four</td><td>five</td><td>six</td>
</tr><tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr><tr>
<td>ten</td><td>eleven</td><td>twelve</td></tr>
</table>

setTableTemplate

Permits you to set your template. You can submit a full or partial template.

put "<table border=" & quote & "1" & quote && "cellpadding=" & quote & "2" & quote && "cellspacing=" & quote "1" & quote && "class=" & quote & "mytable" & quote & ">" into tTmpl["tableOpen"]

setTableTemplate tTmpl

setEmptyCellVal

Let's you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:

setEmptyCellVal "&nbsp;"

clearTableVars

Lets you clear the table heading and row data. If you need to show multiple tables with different data you should call this function after each table has been generated to empty the previous table information. Example:

loaderLoadLibrary "Table"

setTableHeading "Name,Color,Size"

addTableRow "Fred,Blue,Small"
addTableRow "Mary,Red,Large"
addTableRow "John,Green,Medium"

put generateTable() into gData["testTable"]
clearTableVars

setTableHeading "Name,Day,Delivery"

addTableRow "Fred,Wednesday,Express"
addTableRow "Mary,Monday,Air"
addTableRow "John,Saturday,Overnight"

put generateTable() into gData["newTestTable"]