Help:Collapsing: Difference between revisions
MatthiasDD (talk | contribs) →Sortable tables: A caption is bettter way |
mw-collapsible as default |
||
Line 1: | Line 1: | ||
{{about|collapsing tables|collapsing other parts|WP:NAVFRAME}} |
{{about|collapsing tables|collapsing other parts|WP:NAVFRAME}} |
||
''' |
'''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. |
The initial state of any collapsible content can be controlled using a "mw-collapsible table", because it also allows a table with only one element with content, and a header. |
||
==Simple example== |
==Simple example== |
||
Tables are simpler to work with, as you merely need to add the <code>collapsible</code> 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 <code>wikitable</code> in the example below is merely for appearance; it is not needed for <code>collapsible</code> to function. |
Tables are simpler to work with, as you merely need to add the <code>mw-collapsible</code> 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 <code>wikitable</code> in the example below is merely for appearance; it is not needed for <code>mw-collapsible</code> to function. |
||
{| class=wikitable style="width:80%;" align=center |
{| class=wikitable style="width:80%;" align=center |
||
Line 12: | Line 12: | ||
|- |
|- |
||
|width=50%|  |
|width=50%|  |
||
<nowiki>{| class="wikitable collapsible" |
<nowiki>{| class="wikitable mw-collapsible" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
|}</nowiki> |
|}</nowiki> |
||
|style="width:50%; text-align:center;"| |
|style="width:50%; text-align:center;"| |
||
{| class="wikitable collapsible" |
{| class="wikitable mw-collapsible" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
Line 31: | Line 31: | ||
|- |
|- |
||
|style="width:50%;"|  |
|style="width:50%;"|  |
||
<nowiki>{| class="wikitable collapsible" |
<nowiki>{| class="wikitable mw-collapsible" |
||
|A normal cell in the header row |
|A normal cell in the header row |
||
! colspan="2"| Header cell spans two cols |
! colspan="2"| Header cell spans two cols |
||
Line 39: | Line 39: | ||
|}</nowiki> |
|}</nowiki> |
||
|style="width:50%; text-align:center;"| |
|style="width:50%; text-align:center;"| |
||
{| class="wikitable collapsible" |
{| class="wikitable mw-collapsible" |
||
|width=33%|A normal cell in the header row |
|width=33%|A normal cell in the header row |
||
! colspan="2"| Header cell spans two cols |
! colspan="2"| Header cell spans two cols |
||
Line 49: | Line 49: | ||
==Collapsing tables by default== |
==Collapsing tables by default== |
||
Just using the <code>collapsible</code> 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. |
Just using the <code>mw-collapsible</code> 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"=== |
==="collapsed"=== |
||
Line 58: | Line 58: | ||
|- |
|- |
||
|width=50%|  |
|width=50%|  |
||
<nowiki>{| class="wikitable collapsible collapsed" |
<nowiki>{| class="wikitable mw-collapsible collapsed" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
|}</nowiki> |
|}</nowiki> |
||
|style="width:50%; text-align:center;"| |
|style="width:50%; text-align:center;"| |
||
{| class="wikitable collapsible collapsed" |
{| class="wikitable mw-collapsible collapsed" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
Line 71: | Line 71: | ||
|- |
|- |
||
|style="width:50%; font-size:90%"|  |
|style="width:50%; font-size:90%"|  |
||
<nowiki>{| class="wikitable collapsible collapsed" |
<nowiki>{| class="wikitable mw-collapsible collapsed" |
||
|A normal cell in the header row |
|A normal cell in the header row |
||
! colspan="2"| Header cell spans two cols |
! colspan="2"| Header cell spans two cols |
||
Line 79: | Line 79: | ||
|}</nowiki> |
|}</nowiki> |
||
|style="width:50%; text-align:center;"| |
|style="width:50%; text-align:center;"| |
||
{| class="wikitable collapsible collapsed" |
{| class="wikitable mw-collapsible collapsed" |
||
|width=33%|A normal cell in the header row |
|width=33%|A normal cell in the header row |
||
! colspan="2"| Header cell spans two cols |
! colspan="2"| Header cell spans two cols |
||
Line 95: | Line 95: | ||
|- |
|- |
||
|width=50%|  |
|width=50%|  |
||
<nowiki>{| class="wikitable collapsible autocollapse" |
<nowiki>{| class="wikitable mw-collapsible autocollapse" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
|}</nowiki> |
|}</nowiki> |
||
|style="width:50%; text-align:center;"| |
|style="width:50%; text-align:center;"| |
||
{| class="wikitable collapsible autocollapse" |
{| class="wikitable mw-collapsible autocollapse" |
||
! Simple collapsible table |
! Simple mw-collapsible table |
||
|- |
|- |
||
| Lorem ipsum dolor sit amet |
| Lorem ipsum dolor sit amet |
||
Line 120: | Line 120: | ||
| But the table inside this cell |
| But the table inside this cell |
||
{| class="wikitable collapsible innercollapse" |
{| class="wikitable mw-collapsible innercollapse" |
||
! Does collapse |
! Does collapse |
||
|- |
|- |
||
Line 132: | Line 132: | ||
|- |
|- |
||
| But the table inside this cell |
| But the table inside this cell |
||
{| class="wikitable collapsible innercollapse" |
{| class="wikitable mw-collapsible innercollapse" |
||
! Does collapse |
! Does collapse |
||
|- |
|- |
||
Line 144: | Line 144: | ||
===Sortable tables=== |
===Sortable tables=== |
||
collapsible tables can be combined with the [[Help:Sorting|sortable tables]] functionality without difficulty. However, because the hide/show button is placed with <code>mw-collapsible</code> in the first header cell located or with <code>mw-collapsible</code> in the right header cell, its positioning can look a bit peculiar if the cell is not wide enough: |
|||
{|class="wikitable" style="margin:0.5em auto;width:80%" |
{|class="wikitable" style="margin:0.5em auto;width:80%" |
||
Line 152: | Line 152: | ||
|- |
|- |
||
|width="60%"| |
|width="60%"| |
||
<nowiki>{|class="wikitable collapsible sortable" style="width:5em" |
<nowiki>{|class="wikitable mw-collapsible sortable" style="width:5em" |
||
<!--table width too narrow, on purpose here--> |
<!--table width too narrow, on purpose here--> |
||
|- |
|- |
||
Line 162: | Line 162: | ||
|}</nowiki> |
|}</nowiki> |
||
|width="40%"| |
|width="40%"| |
||
{|class="wikitable sortable collapsible" style="width:5em" |
{|class="wikitable sortable mw-collapsible" style="width:5em" |
||
<!--table width too narrow, on purpose here--> |
<!--table width too narrow, on purpose here--> |
||
|- |
|- |
||
Line 226: | Line 226: | ||
== External links == |
== External links == |
||
* [[meta:Help:Collapsing]] |
* [[meta:Help:Collapsing]] |
||
*[[mw:Manual: |
*[[mw:Manual:collapsible elements]] |
||
*[[mw:ResourceLoader/Default modules#jquery. |
*[[mw:ResourceLoader/Default modules#jquery.makecollapsible]] |
||
{{Wikipedia technical help|collapsed}} |
{{Wikipedia technical help|collapsed}} |
Revision as of 02:48, 20 September 2016
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 "mw-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 mw-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 mw-collapsible
to function.
Code entered | Output produced | ||
---|---|---|---|
{| class="wikitable mw-collapsible" ! Simple mw-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 entered | Output produced | ||||||
---|---|---|---|---|---|---|---|
{| class="wikitable mw-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 |} |
|
Collapsing tables by default
Just using the mw-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 entered | Output produced | ||||||
---|---|---|---|---|---|---|---|
{| class="wikitable mw-collapsible collapsed" ! Simple mw-collapsible table |- | Lorem ipsum dolor sit amet |} |
| ||||||
{| class="wikitable mw-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 entered | Output produced | ||
---|---|---|---|
{| class="wikitable mw-collapsible autocollapse" ! Simple mw-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 entered | Output produced | ||||
---|---|---|---|---|---|
{| class="wikitable outercollapse" ! This table does not collapse |- | But the table inside this cell {| class="wikitable mw-collapsible innercollapse" ! 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 with mw-collapsible
in the first header cell located or with mw-collapsible
in the right header cell, its positioning can look a bit peculiar if the cell is not wide enough:
Code entered | Output produced | ||||||
---|---|---|---|---|---|---|---|
{|class="wikitable mw-collapsible sortable" style="width:5em" <!--table width too narrow, on purpose here--> |- ! Name !! Score |- | John || 59 |- | Bob || 72 |} |
|
Possible alternative is to add an caption over the table. See the following example.
Code entered | Output produced | ||||||
---|---|---|---|---|---|---|---|
{|class="wikitable sortable mw-collapsible" |+ Caption ! Name !! Score |- | John || 59 |- | Bob || 72 |} |
|
Customization
Registered users can edit their personal js 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
Documentation:
- Wikipedia:Manual of Style § Scrolling lists and collapsible content – style guidelines for collapsing content
- Help:Table
- Help:Table of contents
Templates:
- {{Collapse}}
- {{Collapse-top}} and {{Collapse-bottom}} – More feature-rich than {{collapse}}
- {{Hidden}}
- {{Navbar-collapsible}}