FANDOM


Collapsible tables provide a way to collapse and uncollapse a table when a show/hide button is clicked. The code for this feature is found in the MediaWiki:Common.js.

The initial state of any collapsible content can be controlled using a "collapsible table", because it also allows a table with only one element with content, and a header.

Simple example

Tables are simpler to work with, as you merely need to add the collapsible class to the table itself. For the [hide]/[show] link to appear, the table's first row must be a header row, that is, one of the cells in the first row must be declared with an exclamation mark (in wikisyntax). The use of the class wikitable in the example below is merely for appearance; it is not needed for collapsible to function.

Code enteredOutput produced
{| class="wikitable collapsible"
! Simple collapsible table
|-
| Lorem ipsum dolor sit amet
|}
Simple collapsible table
Lorem ipsum dolor sit amet

Using the syntax above, collapsible tables are often used to always display an introduction to, or summary of, a lengthy discussion or section of content, while hiding the majority of that content from immediate view. The summary message is put in the header cell, and the content is placed into the body cell. The content is then easily accessible by clicking the 'show' button. The collapsible functionality is also available in larger and more complicated tables, as long as there is a header cell in the first row of the table.

Code enteredOutput produced
{| class="wikitable collapsible"
|A normal cell in the header row
! colspan="2"| Header cell spans two cols
|-
| colspan="2" | Lorem ipsum dolor sit amet
| Separate body cell
|}
A normal cell in the header row Header cell spans two cols
Lorem ipsum dolor sit amet Separate body cell

Collapsing tables by default

Just using the collapsible class produces a table which is expanded by default, but can be collapsed by the reader. It is also possible to create tables which are collapsed by default, and can be optionally expanded. There are several methods for doing this, depending on the situations in which you want the table to collapse.

"collapsed"

Adding the collapsed class will cause the table to always be initially collapsed, no matter what happens around it. It is the simplest method for doing so. Using the examples above:

Code enteredOutput produced
{| class="wikitable collapsible collapsed"
! Simple collapsible table
|-
| Lorem ipsum dolor sit amet
|}
{| class="wikitable collapsible collapsed"
|A normal cell in the header row
! colspan="2"| Header cell spans two cols
|-
| colspan="2" | Lorem ipsum dolor sit amet
| Separate body cell
|}

"autocollapse"

Adding the autocollapse class causes the table to collapse when there are 2 or more collapsible tables on the page (this threshold might be different on other projects). The example below, therefore, collapses because there are numerous collapsible tables on the page.

Code enteredOutput produced
{| class="wikitable collapsible autocollapse"
! Simple collapsible table
|-
| Lorem ipsum dolor sit amet
|}
Simple collapsible table
Lorem ipsum dolor sit amet

"innercollapse" and "outercollapse"

Using this pair of classes, it is possible to make a table collapsed by default only when it is contained within a particular object, such as another table. This is mainly useful for tables inside templates, which are often nested.

Code enteredOutput produced
{| class="wikitable outercollapse"
! This table does not collapse
|-
| But the table inside this cell

{| class="wikitable collapsible innercollapse"
! Does collapse
|-
| Hiding this part
|}

|}
This table does not collapse
But the table inside this cell
Does collapse
Hiding this part

Other notes

Sortable tables

Collapsible tables can be combined with the sortable tables functionality without difficulty. However, because the hide/show button is placed in the first header cell located, its positioning can look a bit peculiar if the table is not wide enough:

Code enteredOutput produced
{| class="wikitable collapsible sortable"
!Name!!Score
|-
|John||59
|-
|Jane||100
|-
|Bob||72
|}
NameScore
John59
Jane100
Bob72

Possible alternative is sortable table inside one-cell collapsible table, see example on the right.

Customization

Registered users can edit their personal js file file to change some of the variables used.

autoCollapse = 4 //how many collapsible tables on the page before autocollapse works (default=2)
var collapseCaption = 'collapse' //text of the [hide] link (default='hide')
var expandCaption = 'uncollapse' //text of the [show] link (default='show')

Other HTML elements

The code that performs the manipulation only checks for table elements, so these classes will not work for other elements, such as <div> or <span>. There is a similar feature for use with <div> elements, called "NavFrame".

Limitations

This functionality requires the end-user's browser to have JavaScript enabled. If Javascript is disabled, the default behaviour is to show the content.

See also

This feature doesn't work on mobile devices in the same way that it does on desktop. In general, any auto-collapsed elements are simply un-collapsed on mobile devices, and there is no toggle button at all. When using this feature, be sure to see whether its mobile results meet your needs.

Collapsing is a feature that allows you to collapse (or hide) any HTML element.

There are times when a page can contain many related elements which makes it very long; using a collapsing feature enables editors to shorten the page, and allow readers to expand areas of interest to them. Most frequently this feature is used on long tables of data and large navigation templates.

To make an element collapsible, simply add the class "mw-collapsible" to it.

Examples

Collapsing a table

By adding "mw-collapsible" as the class, a table can be collapsed to save page space.

Number Letter
1 A
2 B
3 C
{| class="article-table mw-collapsible" style="float:right"
! Number !! Letter
|-
| 1 || A
|-
| 2 || B
|-
| 3 || C
|}

Collapsing text

It is possible to collapse text in an article, effectively shortening the page, and allowing the reader to decide if they want to read the rest of the information. For example:

<div class="mw-collapsible">
'''This text is collapsible.'''
</div>

This results in:

This text is collapsible.

Advanced settings

Initial state

To set the initial state as collapsed add "mw-collapsed" along with "mw-collapsible", such as seen in the following table:

{| class="mw-collapsible mw-collapsed article-table"
! The header !! remains visible
|-
| This  content || is hidden
|-
| until 'Expand' || is clicked
|}
The header remains visible
This content is hidden
until 'Expand' is clicked

Selecting collapsible content

You can also specify which part is the collapsible content by using "mw-collapsible-content".

<div class="mw-collapsible mw-collapsed" style="width:100%">
'''This text is not collapsible; but the next is collapsible and hidden by default:''''<div class="mw-collapsible-content">This text should be hidden by default.</div>
'''This text should be visible as well.'''
</div>

This results in:

This text is not collapsible; but the next is collapsible and hidden by default:

This text should be hidden by default.

This text should be visible as well.

Custom wording

Don't like 'Expand'/'Collapse' for the links? No problem! Change them using "data-expandtext" and "data-collapsetext".

{| class="article-table mw-collapsible" data-expandtext="Illuminate" data-collapsetext="Deluminate"
! My || Header
|-
| A || B
|-
| C || D
|}
My Header
A B
C D

Toggle button

In the example table above you may notice that the link in the second column significantly increases the width of the table. We can fix that by moving the toggle to somewhere else on the page.

To do that, we have to assign an ID to the element. This ID must be prefixed with "mw-customcollapsible-" and followed by a unique key phrase for that element. Using our table above, we can add the ID "mw-customcollapsible-myTable".

That takes care of the table itself. Next, we have to add a button/link/text element with the class "mw-customtoggle-myTable" (note this is a class and not an ID like above).

{| class="article-table mw-collapsible" id="mw-customcollapsible-myTable"
! My || Header
|-
| A || B
|-
| C || D
|}

<div class="mw-customtoggle-myTable wikia-menu-button">Show/Hide table</div>
My Header
A B
C D

Show/Hide table

See also

Further help and feedback

Community content is available under CC-BY-SA unless otherwise noted.