Help:Collapsing

From Iwe

(Difference between revisions)
(Created page with '{{helpbox}} A '''collapsible table''' is a special kind of table whose contents can be hidden.<br> The basic collapsible table has at least one element with content, and a header…')
 
Line 1: Line 1:
-
{{helpbox}}
+
{{about|collapsing tables|collapsing other parts|WP:NAVFRAME}}
-
A '''collapsible table''' is a special kind of table whose contents can be hidden.<br>
+
-
The basic collapsible table has at least one element with content, and a header. The header is not hideable, and should describe the contents within the table.
+
-
====Simple example====
+
'''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]].
-
To make a table "collapsible", you merely need to set the table's class as '''<code>collapsible</code>'''. For a [show] or [hide] link to appear, the table's first row must be a header row, i.e. begin (in wikisyntax) with an exclamation mark (no header row means no text gets hidden, either). The use of the class <code>toccolours</code> in the example below is merely for appearance; it is not needed for collapsing to function:
+
 
-
{| width="80%" colspan="2" cellpadding="2" border=0
+
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.
-
|- valign="top"
+
 
-
|
+
==Simple example==
-
<pre>
+
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.
-
{| class="toccolours collapsible" width="250"
+
 
-
!colspan="2"| This header never disappears.
+
{| class=wikitable style="width:80%;" align=center
 +
!Code entered!!Output produced
|-
|-
-
| This column... || ...and this column can be collapsed.
+
|width=50%|&#32;
-
|}
+
<nowiki>{| class="wikitable collapsible"
-
</pre>
+
! Simple collapsible table
-
|
+
-
{| class="toccolours collapsible" width="250"
+
-
!colspan="2"| This header never disappears.
+
|-
|-
-
| This column... || ...and this column can be collapsed.
+
| Lorem ipsum dolor sit amet
-
|}
+
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable collapsible"
 +
! Simple collapsible table
|-
|-
 +
| Lorem ipsum dolor sit amet
|}
|}
-
One-element example, initially expanded:
+
|}
-
{| width="80%" colspan="2" cellpadding="2" border=0
+
 
-
|- valign="top"
+
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.
-
|
+
 
-
<pre>
+
{| class=wikitable style="width:80%;" align=center
-
{| class="collapsible" border=1 width="250"
+
!Code entered!!Output produced
-
!Never disappearing header
+
|-
|-
-
|Text to appear/disappear
+
|style="width:50%;"|&#32;
 +
<nowiki>{| 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
 +
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable collapsible"
 +
|width=33%|A normal cell in the header row
 +
! colspan="2"| Header cell spans two cols
 +
|-
 +
| colspan="2" | Lorem ipsum dolor sit amet
 +
|width=33%| Separate body cell
|}
|}
-
</pre>
+
|}
-
|
+
 
-
{| class="collapsible" border=1 width="250"
+
==Collapsing tables by default==
-
!Never disappearing header
+
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.
 +
 
 +
==="collapsed"===
 +
Adding the <code>collapsed</code> class will cause the table to {{em|always}} be initially collapsed, no matter what happens around it.  It is the simplest method for doing so.  Using the examples above:
 +
 
 +
{| class=wikitable style="width:80%;" align=center
 +
!Code entered!!Output produced
|-
|-
-
|Text to appear/disappear
+
|width=50%|&#32;
 +
<nowiki>{| class="wikitable collapsible collapsed"
 +
! Simple collapsible table
 +
|-
 +
| Lorem ipsum dolor sit amet
 +
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable collapsible collapsed"
 +
! Simple collapsible table
 +
|-
 +
| Lorem ipsum dolor sit amet
|}
|}
|-
|-
 +
|style="width:50%; font-size:90%"|&#32;
 +
<nowiki>{| 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
 +
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable collapsible collapsed"
 +
|width=33%|A normal cell in the header row
 +
! colspan="2"| Header cell spans two cols
 +
|-
 +
| colspan="2" | Lorem ipsum dolor sit amet
 +
|width=33%| Separate body cell
 +
|}
|}
|}
-
====Auto-collapsing tables====
+
==="autocollapse"===
-
You have two choices of auto-collapse. In addition to the <code>collapsible</code> class, you can add either '''<code>collapsed</code>''' or '''<code>autocollapse</code>'''. The first always initially collapses a table. <code>autocollapse</code> only initially collapses a table if there are at least two collapsible tables on a page.
+
Adding the <code>autocollapse</code> 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.
-
{| width="80%" colspan="2" cellpadding="2" border=0
+
 
-
|- valign="top"
+
{| class=wikitable style="width:80%;" align=center
-
|
+
!Code entered!!Output produced
-
<pre>{| class="toccolours collapsible autocollapse" width="250"
+
|-
|-
-
! colspan="2" align="left" | This autocollapse table
+
|width=50%|&#32;
-
will initially collapse because there are at least
+
<nowiki>{| class="wikitable collapsible autocollapse"
-
two collapsible tables on this page.
+
! Simple collapsible table
|-
|-
-
| Column A || Column B
+
| Lorem ipsum dolor sit amet
 +
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable collapsible autocollapse"
 +
! Simple collapsible table
 +
|-
 +
| Lorem ipsum dolor sit amet
|}
|}
-
</pre>
+
|}
-
|
+
 
-
{| class="toccolours collapsible autocollapse" width="250"
+
==="innercollapse" and "outercollapse"===
 +
Using this pair of classes, it is possible to make a table collapsed by default only when it is {{em|contained within}} a particular object, such as another table. This is mainly useful for tables inside templates, which are often nested.
 +
 
 +
{| class=wikitable style="width:80%;" align=center
 +
!Code entered!!Output produced
|-
|-
-
! colspan="2" align="left" | This autocollapse table will initially collapse because there are at least two collapsible tables on this page.
+
|width=50%|&#32;
 +
<nowiki>{| class="wikitable outercollapse"
 +
! This table does not collapse
 +
|-
 +
| But the table inside this cell
 +
 
 +
{| class="wikitable collapsible innercollapse"
 +
! Does collapse
|-
|-
-
| Column A || Column B
+
| Hiding this part
|}
|}
 +
 +
|}</nowiki>
 +
|style="width:50%; text-align:center;"|
 +
{| class="wikitable outercollapse"
 +
! This table does not collapse
|-
|-
 +
| But the table inside this cell
 +
{| class="wikitable collapsible innercollapse"
 +
! Does collapse
 +
|-
 +
| Hiding this part
|}
|}
-
{| width="80%" colspan="2" cellpadding="2" border=0
+
|}
-
|- valign="top"
+
 
-
|
+
|}
-
<pre>{| class="toccolours collapsible collapsed" width="250"
+
 
 +
==Other notes==
 +
 
 +
===Sortable tables===
 +
Collapsible tables can be combined with the [[Help:Sorting|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 cell is not wide enough:
 +
 
 +
{|class="wikitable" style="margin:0.5em auto;width:80%"
|-
|-
-
! colspan="2" | This table will always initially collapse
+
!scope="col" width="60%"|Code entered
 +
!scope="col" width="40%"|Output produced
|-
|-
-
| Column A || Column B
+
|width="60%"|
-
|}</pre>
+
<nowiki>{|class="wikitable collapsible sortable" style="width:5em"
-
|
+
<!--table width too narrow, on purpose here-->
-
{| class="toccolours collapsible collapsed" width="250"
+
|-
|-
-
! colspan="2" | This table will always initially collapse
+
!scope="col"|Name!!scope="col"|Score
|-
|-
-
| Column A || Column B
+
|John||59
-
|}
+
|-
|-
-
|}
+
|Jane||100
-
One-element example, initially collapsed:
+
-
{| width="80%" colspan="2" cellpadding="2" border=0
+
-
|- valign="top"
+
-
|
+
-
<pre>{| class="collapsible collapsed" width="250" border=1
+
-
!Never disappearing header
+
|-
|-
-
|Text to appear/disappear
+
|Bob||72
-
|}</pre>
+
|}</nowiki>
-
|
+
|width="40%"|
-
{| class="collapsible collapsed" width="250" border=1
+
{|class="wikitable sortable collapsible" style="width:5em"
-
!Never disappearing header
+
<!--table width too narrow, on purpose here-->
|-
|-
-
|Text to appear/disappear
+
!scope="col"|Name!!scope="col"|Score
-
|}
+
|-
|-
 +
|John||59
 +
|-
 +
|Jane||100
 +
|-
 +
|Bob||72
|}
|}
-
====Sortable collapsible tables====
+
|}
-
Collapsible tables can also be sorted using the "wikitable sortable" class. When you click an arrow at the top of a column, the contents of the column are shuffled appropriately. See the following example:
+
 
-
{| border=1 width="60%"
+
Possible alternative (that appears quite horrible from utility point of view, though) is sortable table inside one-cell collapsible table whose header will be as large as the full table embedded below. See the following example.
-
!initially expanded!!initially collapsed
+
{|class="wikitable" style="margin:0.5em auto;width:80%"
-
|- width="50% valign="top" align="center"
+
|-
-
|
+
!scope="col" width="60%"|Code entered
-
{|class="collapsible" width="250"
+
!scope="col" width="40%"|Output produced
-
!numbers
+
|-
-
|- width="50%"
+
|width="60%"|
-
|
+
<nowiki>{|class="wikitable collapsible collapsed" style="border:none"
-
{|class="wikitable sortable"
+
|-
-
!name!!number
+
!scope="col"|header
 +
|-
 +
|style="padding:0;border:none"|
 +
{|class="wikitable sortable" style="margin:0;width:100%"
|-
|-
-
|a||123
+
!scope="col"|a!!scope="col"|b
|-
|-
-
|b||6
+
|3||4
|-
|-
-
|c||45
+
|5||6
|}
|}
-
|}
+
|}</nowiki>
-
|
+
|width="40%"|
-
{|class="collapsible collapsed" width="250"
+
{|class="wikitable collapsible collapsed" style="border:none"
-
!numbers
+
|-
 +
!scope="col"|header
|-
|-
-
|
+
|style="padding:0;border:none"|
-
{|class="wikitable sortable"
+
{|class="wikitable sortable" style="margin:0;width:100%"
-
!name!!number
+
|-
|-
-
|a||123
+
!scope="col"|a!!scope="col"|b
|-
|-
-
|b||6
+
|3||4
|-
|-
-
|c||45
+
|5||6
|}
|}
|}
|}
|}
|}
-
For additional help on sorting, see [[Wikipedia:Help:Sorting|Help:Sorting on Wikipedia]].
+
{{-}}
-
== Notes ==
+
===Customization===
-
=== Browser Notes ===
+
Registered users can edit [[Special:MyPage/skin.js|their personal js file]] to change some of the variables used.
-
The collapsible tables code works best with IE or Opera. To ensure that it works correctly with Mozilla Firefox, remember to specify the width of the table. Tables which are initially collapsed in Firefox require the show link to be clicked twice in order to open if the width is not specified. In such cases, it is best to specify the desired width necessary for viewing when expanded.
+
<source lang=javascript>
 +
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')
 +
</source>
-
=== Implementation notes ===
+
===Other HTML elements===
-
Having the collapsing text on the same line or no header at all is not desired, so that is why an unhideable header row is required. Without a header row, there is a large potential for abuse since nothing would be known about the contents of the table.
+
The code that performs the manipulation only checks for table elements, so these classes will not work for other elements, such as <code><nowiki><div></nowiki></code> or <code><nowiki><span></nowiki></code>.  There is a similar feature for use with <code><nowiki><div></nowiki></code> elements, called "[[Wikipedia:NavFrame|NavFrame]]".
-
<br>Also, the show/hide links are hard coded because being able to change the link would essentially let users make the link hidden, which is also undesirable. Having large amounts of unviewable text is a reason some search engines delete sites from their listings.
+
 
 +
===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==
==See also==
-
* [[Help:Editing#Tables]]
+
Documentation:
 +
* {{Section link|Wikipedia:Manual of Style|Scrolling lists and collapsible content}} – style guidelines for collapsing content
 +
* [[Help:Table]]
 +
* [[Help:Table of contents]]
 +
Templates:
 +
* {{Tl|Collapse}}
 +
* {{Tl|Collapse-top}} and {{Tl|Collapse-bottom}} – More feature-rich than {{Tlf|collapse}}
 +
* {{Tl|Hidden}}
 +
* {{Tl|Navbar-collapsible}}
 +
 
 +
== External links ==
 +
* [[meta:Help:Collapsing]]
 +
*[[mw:Manual:Collapsible elements]]
 +
*[[mw:ResourceLoader/Default modules#jquery.makeCollapsible]]
-
[[Category:Help]]
+
{{Wikipedia technical help|collapsed}}
 +
[[Category:Wikipedia how-to]]
 +
[[Category:Wikipedia tables]]

Current revision as of 22:47, 4 April 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 "collapsible table", because it also allows a table with only one element with content, and a header.

Contents

[edit] 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

[edit] 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.

[edit] "collapsed"

Adding the collapsed class will cause the table to Template:Em 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
|}

[edit] "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

[edit] "innercollapse" and "outercollapse"

Using this pair of classes, it is possible to make a table collapsed by default only when it is Template:Em 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

[edit] Other notes

[edit] 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 cell is not wide enough:

Code entered Output produced
{|class="wikitable collapsible sortable" style="width:5em"
<!--table width too narrow, on purpose here-->
|-
!scope="col"|Name!!scope="col"|Score
|-
|John||59
|-
|Jane||100
|-
|Bob||72
|}
NameScore
John59
Jane100
Bob72

Possible alternative (that appears quite horrible from utility point of view, though) is sortable table inside one-cell collapsible table whose header will be as large as the full table embedded below. See the following example.

Code entered Output produced
{|class="wikitable collapsible collapsed" style="border:none"
|-
!scope="col"|header
|-
|style="padding:0;border:none"|
{|class="wikitable sortable" style="margin:0;width:100%"
|-
!scope="col"|a!!scope="col"|b
|-
|3||4
|-
|5||6
|}
|}


[edit] Customization

Registered users can edit their personal js file to change some of the variables used. <source lang=javascript> 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') </source>

[edit] 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".

[edit] 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.

[edit] See also

Documentation:

Templates:

[edit] External links

Template:Wikipedia technical help

Personal tools