StrategyWiki:Guide/Advanced tables

If you want to make more advanced tables, a full understanding of the options in the prettytable template, and a little HTML and CSS knowledge is necessary. Tables should be authored in wiki pages using wikicode formatting to define the table, with styles and other attributes added as necessary.

A good general rule of thumb is to avoid using a table unless you need to. Table markup often complicates page editing.

Wiki table markup summary

 * The above marks must start on a new line except the double || and !! for optionally adding consecutive cells to a line.
 * XHTML attributes. Each mark, except table end, optionally accepts one or more XHTML attributes. Attributes must be on the same line as the mark. Separate attributes from each other with a single space.
 * Cells and caption ( | or ||, ! or !!, and |+ ) hold content. So separate any attributes from content with a single pipe (|). Cell content may follow on same line or on following lines.
 * Table and row marks ( {| and |- ) do not directly hold content. Do not add pipe (|) after their optional attributes. If you erroneously add a pipe after attributes for the table mark or row mark the parser will delete it and your final attribute if it was touching the erroneous pipe!
 * Content may (a) follow its cell mark on the same line after any optional XHTML attributes or (b) on lines below the cell mark. Content that uses wiki markup that itself needs to start on a new line, such as lists or nested tables, must of course be on its own new line.

Wiki markup
The following table uses the standard prettytable template and no captions.

For more table-ish looking wiki markup, cells can be listed on one line separated by double pipes (||) or double exclamation marks (!!). This does not scale well for longer cell content such as paragraphs. It works well for short bits of content however, such as this example table. Extra spaces within cells in the wiki markup can be added, as have been added in the wiki markup below, to make the wiki markup itself look better, but they do not effect the actual table rendering.

HTML attributes can be added to this table following the examples in other tables on this page but have been left out of the following example for simplicity.

Table alignment
Tables default to block elements on the left side of the page, meaning text will stop at a table and continue below it instead of wrapping around. Using the  and   parameters in the prettytable template, you can move a table to the left or right side of the page and allow other contents to flow around it. Floating a table automatically collapses the size, meaning the table will attempt to be as small as it can without breaking its cell contents into multiple lines. If the content is small, as in the above examples, the tables won't be full width across the page.

Along with left and right floats, you can also add CSS to the  parameter to make the table centered on the page. Unless the  parameter is also set, making the table collapse to it's narrowest size, the table will be full width and centering it will have no effect (essentially is is centered, albeit with no margins no either side).

Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left. Table floating left.
 * }

Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right. Table floating right.
 * }

Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered. Table centered.
 * }

Table sorting
Using JavaScript, a sorting icon can be added to a table's header row which will dynamically rewrite the table to move the rows in alphabetic order of the column being sorted on. To make a table sortable, simply add the  parameter to the prettytable template. This adds the "sortable" class to the table, which, along with a header row, is what the JavaScript is looking for.

Sometimes it is necessary to suppress sorting for some columns, where it would either have no effect, or would add no benefit. An example of a column by which you cannot sort is one consisting solely of images. The sorting works mainly on rendered text, so images aren't sorted, even by their alternate text. The only way to get around this, is by putting text somewhere else in the cell, which may or may not be desired. To stop a column from being sorted, the "unsortable" class must be added. Use class="unsortable" to accomplish this.

Below is an example of a sortable table with its middle column set to unsortable. For more information on methods for adding attributes to cells, see the Cell attributes section. Note that if you are using  or   attributes in your table, sorting will most likely not work.

Table attributes
Table attributes affect the whole table, and some of them are in turn picked up by the caption, rows and cells within the table. Setting the "color" style, which changes the font color, will change the color for all text within the cell. Setting the "width", will only affect the table's width, except for the obvious affect that shrinking or enlarging a table would have on the contents within. (Setting the table width to 50% doesn't change cell widths, except that they now take up whatever width they're set at of a smaller table.)

All table attributes can be set through the  parameter of the prettytable template, and should be except in rare circumstances. The following examples show setting a table's width both inside and outside the prettytable template. Note that outside the template, you must surround the value of the attribute with double quotes (") and if you are modifying a style set in the template, you need to put it after the template, as styles are set in order.

Row attributes
You can put attributes on individual rows to affect all cells in the row. In general, the options in prettytable along with proper use of header and normal cells should be enough to distinguish information. However, sometimes it may be necessary to set a row apart from the others in a different way, such as to denote optional content or something of importance. To affect the entire row, you'll want to use the  attribute, but unlike in the prettytable template, you'll need to surround the styles with double quotes ("). The examples below are intended to show the method for modifying a row's styles through use of the   attribute, but knowing which styles to use and how to use them is outside of this page's scope.

Note that multiple styles can be set for a single row, although only one per row is used here. Also, the first "table row" markup (|-) is optional.

Cell attributes
Cell attributes go between the pipe or pipes starting the cell and an ending pipe showing the end of the attribute declarations. You can use the cell attributes to modify the structure inside the table as well as the style of an individual cell. StrategyWiki has certain cell formatting templates for common cell markup. These templates insert the closing pipe after themselves, so any other modifications to cell attributes you are making need to go before them.

The following example shows how to use cell attributes with and without the templates. These are to show the structure for modifying cell styles, and is not meant to be a comprehensive list of every style that can be applied to a cell. Note that attribute values must be surrounded by double quote ("), unlike in the  parameter of the prettytable template.

Header and normal cells
A table without a header row or column is either a list, or confusing to anyone looking at it. In the former case, wiki markup for lists should be used (* or #). The header cell lets readers know what the cells below or to the side of it all have in common. For instance, if there is a table for character stat comparisons, the "Character" header cell in the header row tells someone that all normal cells below it are character names, while the "Strength" header cell let's them know that column is each character's strength. This is a simple and intuitive system that makes tables very useful for displaying a lot of comparable data.

The difference between making a header or normal cell is using either the ! or | markup, respectively. For rows in which all cells are put on the same row, the starting cell is the only one that matters for rendering purposes. That means if you want the first cell in a row to be a header cell, but not the others, you have to put the normal cells on their own line, because using || on the same line has the same effect as using !!. Likewise if you want to start a row with a normal cell and then make the next ones headers (not recommended), trying to use !! will break the table. The prettytable template ensures that header cells get additional styling, which includes centering and bolding the text, as well as making the background color slightly darker. Always use the standard markup for header cells rather than creating your own. Below is an example of a header row, a header column and the unintended consequence of mixing up the markup in a table.

and
These special attributes change the structure of the table. Using these, a single cell can span multiple rows, columns or both. Another way to think of it, is merging cells together, as on a spreadsheet. The purpose of spanning rows or columns varies, but it can be quite useful in certain circumstances. Keep in mind that using these attributes significantly increases the complexity of the table, so other editors, or even yourself later on, may be confused on how to properly edit it. Properly using  and   means carefully analyzing how the table is put together. Also note that spanning any cells will render sortable tables inoperable in most cases.

The  property makes a cell cover more rows than the one it starts on, effectively lengthening it. The  property on the other hand makes a cell cover more columns than the one it starts on, effectively widening it. They both accept as values a number that can be no greater the number of rows or cells in the table, and should be more than 1 (it's already spanning its own row and column). The number includes its own row or column, so to have a cell take up two spaces in a row, you would use. That tells the table to render no cell divider between that cell and the cell to its immediate right, and to spread any content for that cell out across the width of both columns.

Because you are effectively reducing the number of cells in the table when using these attributes, it effects how the rest of the table is laid out. Spanning columns is easier to conceptualize, as you just count the  cell as however many cell it's spanning, then add the rest. The  property may be more confusing, because it affects a different line of wiki markup. In the case of, you'll need to omit a cell from each row below that the cell is spanning. Tables with multiple cells spanning multiple columns and rows can quickly get very complicated, so use these attributes with caution. Below are examples of each property in use to better illustrate how they work.

Caption
A table caption can be added to the top of any table as follows:

Attributes can be added to the caption as follows: