Help:表格

From mediawiki.org
This page is a translated version of the page Help:Tables and the translation is 91% complete.
Outdated translations are marked like this.
PD 注意:當您編輯本頁面時,即同意以CC0協議授權您的貢獻。您可以在公有領域说明页面找到更多信息。
PD
“Tables”重定向到此处;关于MediaWiki的数据库表结构,请参见Manual:数据库布局

表格可以在維基頁面中創建。 一般来说,除非必要,應盡量避免使用表格。 表格的標示語言通常複雜化了頁面的編輯。[1]

Wiki表格标示摘要

{| 表格起始,必需
|+ 表格 標題選用;每張表格只能出現一次且介於表格起始第一行
|- 表格行第一行選用 -- wiki 引擎會假設是第一行
! 表格標題 儲存格, 選用。 可以使用(!!)在同一行加入接續的表格標題或是單獨使用(!)換新的一行 。
| 表格数据调用,可选。 可以使用(||)接續表格資料儲存格或是單獨使用(|)。
|} 表格末尾必要
  • 上述符號必須出現在新行的開頭除了當在同一行中想要延續儲存格所增用的雙||!!。然而,行开头的空格会被忽略。
  • HTML 屬性。 每一個符號,除了表格的末尾,可選擇性的接受一或多個 HTML 屬性。 屬性必須與符號在同一行。 請使用一個空格隔開每一項屬性。
    • 儲存格與標題(|或是||!或是!!,以及|+)持有表格內容。 所以要使用單線(|)來區隔屬性與內容。 儲存格內容可以都位在同一行或是換到另一行。
    • 表格與行的符號({||-)並不直接持有內容。 在它們的屬性之後請不要加入管線(|)。 如果你在表格與行符號的屬性之後錯誤的加入管線符號,剖析器會將之刪除 以及之後任何觸及該出錯管線的屬性!
  • 內容 (a) 可以任何選用的 HTML 屬性之後跟著同一行它的儲存格標記或是 (b) 儲存格標記的下一行。 使用 wiki 標示語言的內容本身需要自新的一行開始,例如清單,表頭,或是表格內接的表格,必須在它自個兒的新行。
    • 管道符号作为内容。 在表格中插入(|),可以使用<nowiki>|</nowiki>避免成为标示语言的一部分。


基础

使用编辑栏创建表格

在wikitext编辑器中,将光标放在你想插入表格的地方。然后在工具栏中,按“高级”,然后选择 表格按钮。 这会打开一个对话。

在对话框中,你可以选择是否启用表头行、用边框定制表格或使表格有序化。 显示的是一个预览的例子。 你也可以设置你需要的行数和列数。然后,按“插入”按钮。

By default, the following code is generated:

Wikicode:
{| class="wikitable" style="margin:auto"
|+ 标题文本
|-
! 标题文本 !! 标题文本 !! 标题文本
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
Result:
标题文本
标题文本 标题文本 标题文本
示例 示例 示例
示例 示例 示例
示例 示例 示例

Minimal syntax

下面這個表格沒有外框與留白,但是顯示了最簡單的 wiki 表格標示語言的結構。

您输入的 您输出的
{|
|橘子
|苹果
|-
|面包
|饼
|-
|奶油
|冰淇凌 
|}
橘子 苹果
面包
奶油 冰淇凌

想要排列得更像表格,可以使用 wiki 標示語言||將儲存格分開但又並排於同一行。 如果单元格中的文本包含了换行符,可使用‎<br />替代。

您输入的 输出效果
{|
|橘子||苹果||更多
|-
|面包||饼||更多
|-
|奶油||冰<br />淇淋||和<br />更多
|}
橘子 苹果 更多
面包 更多
奶油
淇淋

更多

如下面的Wiki标示语言,单元格包含额外空格,这不影响实际表格的呈现。

您输入的 输出效果
{|
|  橘子 || 苹果 || 更多
|-
|   面包 || 饼 || 更多
|-
|   奶油 || 冰淇凌 || 以及更多
|}
橘子 苹果 更多
面包 更多
奶油 冰淇凌 以及更多

您当然也可以在单元格中输入更长的文本或者更复杂的Wiki语法:

您输入的 输出效果
{|
|Lorem ipsum dolor sit amet, 
consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt
ut labore et dolore magna aliquyam erat, 
sed diam voluptua. 

At vero eos et accusam et justo duo dolores
et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum
dolor sit amet. 
|
* Lorem ipsum dolor sit amet
* consetetur sadipscing elitr
* sed diam nonumy eirmod tempor invidunt
|}
Lorem ipsum dolor sit amet,

consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

  • Lorem ipsum dolor sit amet
  • consetetur sadipscing elitr
  • sed diam nonumy eirmod tempor invidunt

表格头部

表格的头部可以通过使用“!”(感叹号)替代“|”(管道符号)创建。 表头通常显示为粗体,并默认居中。

您输入的 输出效果
{|
! style="text-align:left;"| 项
! 金額數量
! 价钱
|-
|橘子
|10
|7.00
|-
|面包
|4
|3.00
|-
|奶油
|1
|5.00
|-
!总计
|
|15.00
|}
金額數量 價錢
橘子 10 7.00
面包 4 3.00
奶油 1 5.00
总计 15.00
当使用属性作为标头“项目”时,需使用一个竖线条“|”分隔。 而不是感叹号“!”。

标题

表格标题可以添加到任何表的顶部,如下所示。

您输入的 输出效果
{|
|+食品和配餐
|-
|橘子
|苹果
|-
|面包
|饼
|-
|奶油
|冰淇凌 
|}
食品和配餐
橘子 苹果
面包
奶油 冰淇凌

class="wikitable"

通过添加 class="wikitable" ,可以使表格实现:浅灰色背景、可见边框、内边距、左对齐的基本样式。

您输入的 输出效果
{| class="wikitable"
|+食品和配餐
|-
|橘子
|苹果
|-
|面包
|饼
|-
|奶油
|冰淇凌 
|}
食品和配餐
橘子 苹果
面包
奶油 冰淇凌

HTML 跨列和跨行

您可以在单元格可以使用HTML colspanrowspan属性进行高级布局。

您输入的 输出效果
{| class="wikitable"
!colspan="6"|购物清单
|-
|rowspan="2"|麵包和奶油
|派
|小圆面包
|丹麥甜糕餅
|colspan="2"|羊角麵包
|-
|奶酪
|colspan="2"|冰淇凌
|奶油
|酸奶酪
|}
購物清單
麵包和奶油 小圆面包 丹麥甜糕餅 羊角麵包
奶酪 冰淇凌 奶油 酸奶酪

HTML 屬性

你可以添加HTML屬性到表格。 这些属性的规范来源,请参考W3C的HTML表格规格页面

添加属性到表格

将表格属性添加到({|)后将应用属性到整个表格。

您输入的 输出效果
{| class="wikitable" style="text-align: center; color: green;"
|橘子
|苹果
|12,333.00
|-
|面包
|饼
|500.00
|-
|奶油
|冰淇凌
|1.00
|}
橘子 苹果 12,333.00
面包 500.00
奶油 冰淇凌 1.00

單元格属性

你可以將屬性加諸於個別的儲存格。 例如數字靠右對齊會比較好看。

您输入的 您输出的
{| class="wikitable"
| 橘子
| 苹果
| style="text-align:right;" | 12,333.00
|-
| 面包
| 饼
| style="text-align:right;" | 500.00
|-
| 奶油
| 冰淇凌
| style="text-align:right;" | 1.00
|}
橘子 苹果 12,333.00
面包 500.00
奶油 冰淇凌 1.00

当您在单行中列出多个单元格时,也可以使用单元格属性。 注意单元格是被||分隔的,且每个单元格的属性和内容是被|分隔的。

您输入的 输出效果
{| class="wikitable"
| 橘子 || 苹果     || style="text-align:right;" | 12,333.00
|-
| 面包 || 饼       || style="text-align:right;" | 500.00
|-
| 奶油 || 冰淇凌 || style="text-align:right;" | 1.00
|}
橘子 苹果 12,333.00
面包 500.00
奶油 冰淇凌 1.00

行属性

你也可以將屬性加諸於個別的

您输入的 您输出的
{| class="wikitable"
| 橘子
| 苹果
| style="text-align:right;"| 12,333.00
|-
| 面包
| 饼
| style="text-align:right;"| 500.00
|- style="font-style: italic; color: green;"
| 奶油
| 冰淇凌
| style="text-align:right;"| 1.00
|}
橘子 苹果 12,333.00
面包 500.00
奶油 冰淇凌 1.00

标题和标题行的属性

属性可以添加到标题和标题行,如下所示。

您输入的 您输出的
{| class="wikitable"
|+ style="caption-side:bottom; color:#e76700;"|''食品和配餐''
|-
! style="color:green" | 水果
! style="color:red" | 脂肪
|-
|橘子
|奶油
|-
|梨
|饼
|-
|苹果
|冰淇凌 
|}
食品和配餐
水果 脂肪
橘子 奶油
苹果 冰淇凌

边框宽度

如果“border-width:”的值只有一个的话,值将应用于所有边框。

您输入的 您输出的
{|style="border-style: solid; border-width: 20px"
|
您好!
|}

您好!

如果“border-width:”多于一个值时,四个数字分别代表顶部、右侧、底部、左侧的宽度(请记住順時針顺序↑→↓←)

您输入的 您输出的
{|style="border-style: solid; border-width: 10px 20px 100px 0"
|
您好!
|}

您好!

当少于四个值时:
  • 当属性像top, right, bottom这样只有三个值时,left属性的默认值是right(本例中为第二个值),此时左右宽度相等。
  • 当属性像top, right 这样只有两个值时,bottom 属性的默认值是 top(本例中为第二个值),left 属性的默认值是 right (本例中为第二个值),此时上下宽度相等,左右宽度相等。
  • one value i.e. top: then the default value for right is the one of top and it is the same for bottom and left. The fourth width are the same and build a regular border. This is a writing shortcut.

另一种定义单元格四边宽度的方法是使用 "border-left"、"border-right"、"border-top"和"border-bottom"属性:

您输入的 您输出的
{|style="border-left:solid 10px black;border-right:solid 20px black;border-top:solid 30px black;border-bottom:solid 40px black;" align="center" 
|
您好!
|}

您好!

  • HTML属性(例如“width=”、“border=”、“cellspacing=”、“cellpadding=”)不需要任何长度单位(假定为像素单位)。

它们在HTML 5中也是无效的。

"Cellpadding" is for setting the space between the cell wall and the cell content.

[2] CSS样式属性(覆盖HTML属性)需要明确长度单位(如果值不是0的话),例如“px”用于像素。

搭配 HTML 屬性與 CSS 樣式

CSS 樣式屬性可以同時搭配或不搭配其他 HTML 屬性

您输入的 您输出的
{| class="wikitable" style="color:green; background-color:#ffffcc;" cellpadding="10"
|橘子
|苹果
|-
|面包
|饼
|-
|奶油
|冰淇凌 
|}
橘子 苹果
面包
奶油 冰淇凌

Padding

It represents the internal margin between the contents and the border of the cell.

You type You get
{|class=wikitable 
| style="padding: 10px" | style="padding:10px"的示例
|-
| style="padding: 50px" | style="padding:50px"的示例<br/><br/>指定'''每个单元格'''的padding
|-
| style="padding:100px" | style="padding:100px"的示例
|}
style="padding:10px"的示例
style="padding:50px"的示例

指定每个单元格的padding
style="padding:100px"的示例

列宽

列宽可以通过如下方式添加。

您输入的:

{| class="wikitable" style="width: 85%;"
| colspan="2" | 此列列宽为屏幕宽度的85%。
|-
| style="width: 30%"| '''此列列宽为屏幕宽度的85%中的30%'''
| style="width: 70%"| '''此列列宽为屏幕宽度的85%中的70%'''
|}

您输出的:

此列列宽为屏幕宽度的85%
此列列宽为屏幕宽度的85%中的30% 此列列宽为屏幕宽度的85%中的70%

表头单元格的辅助功能

与承载数据的单元格(在表头单元格同一列的下面,或表头单元格同一行的右面)相比,表头单元格并不是十分特殊的单元格。 When the table is rendered in a visual 2D environment, this is usually easy to infer.

However when tables are rendered on non-visual media, you can help the browser to determine which table header cell applies to the description of any selected cell (in order to repeat its content in some accessibility helper) using a scope="row" or scope="col" attribute on table header cells. In most cases with simple tables, you'll use scope="col" on all header cells of the first row, and scope="row" on the first cell of the following rows:

您输入的 您输出的
{| class="wikitable"
|-
! scope="col"| 项目
! scope="col"| 数量
! scope="col"| 价格
|-
! scope="row"| 面包
| 0.3 kg
| $0.65
|-
! scope="row"| 奶油
| 0.125 kg
| $1.25
|-
! scope="row" colspan="2"| 总计
| $1.90
|}
项目 数量 价格
面包 0.3 kg $0.65
奶油 0.125 kg $1.25
总计 $1.90

对齐

表格对齐

表格的对齐通过使用CSS实现。 表格对齐由外边距控制。 A fixed margin on one side will make the table to be aligned to that side, if on the opposite side the margin is defined as auto. 为了使表格居中,你需要把两边的margin(外边距)属性都设为auto

一个右对齐表格的例子:

您输入的 您输出的
{| class="wikitable" style="margin-left: auto; margin-right: 0px;"
| 橘子
| 苹果
|-
| 面包
| 饼
|-
| 奶油
| 冰淇凌 
|}
橘子 苹果
面包
奶油 冰淇凌

一个居中表格的例子:

您输入的 您输出的
{| class="wikitable" style="margin: auto;"
| 橘子
| 苹果
|-
| 面包
| 饼
|-
| 奶油
| 冰淇凌 
|}
橘子 苹果
面包
奶油 冰淇凌

文本环绕表格

如果您将表格与页面的右侧或左侧对齐,表格后面的文本将在表格结尾处开始,并在表格周围留下空白。 You can make the text to be wrapped around the table by making the table to float around the text instead of just aligning it. This can be achieved using the float CSS attribute, which can specify where the table floats to the right side or to the left. When using float, margins doesn't control table alignment and can be used to specify the margin between the table and the surrounding text.

您输入的 您输出的
{| class="wikitable" style="float:right; margin-left: 10px;"
| 橘子
| 苹果
|-
| 面包
| 饼
|-
| 奶油
| 冰淇凌 
|}

Lorem ipsum dolor sit amet, consectetuer adipiscing 
elit, sed diam nonummy nibh euismod tincidunt ut 
laoreet dolore magna aliquam erat volutpat. Ut wisi 
enim ad minim veniam, quis nostrud exerci tation 
ullamcorper suscipit lobortis nisl ut aliquip ex ea 
commodo consequat. Duis autem vel eum iriure dolor 
in hendrerit in vulputate velit esse molestie consequat, 
vel illum dolore eu feugiat nulla facilisis at vero 
eros et accumsan et iusto odio dignissim qui blandit 
praesent luptatum zzril delenit augue duis dolore te 
feugait nulla facilisi.
橘子 苹果
面包
奶油 冰淇凌

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

单元格内容对齐

单元格内容的对齐受两个不同CSS的属性控制text-alignvertical-align控制。 text-align可以在表格、行或单个单元格中指定,而vertical-align只能在单个行或单元格中指定。

您输入的 您输出的
{| class="wikitable"
|- style="vertical-align:top;"
| style="height:100px; width:100px; text-align:left;" | A
| style="height:100px; width:100px; text-align:center;" | B
| style="height:100px; width:100px; text-align:right;" | C
|- style="vertical-align:middle;"
| style="height:100px; width:100px; text-align:left;" | D
| style="height:100px; width:100px; text-align:center;" | E
| style="height:100px; width:100px; text-align:right;" | F
|- style="vertical-align:bottom;"
| style="height:100px; width:100px; text-align:left;" | G
| style="height:100px; width:100px; text-align:center;" | H
| style="height:100px; width:100px; text-align:right;" | I
|}
A B C
D E F
G H I

注意事项

负数

如果你要在一行的第一个单元格中显示负数(例如 |-6 ),其中的负号可能会导致显示不正常。因为MediaWiki软件会认为你的标记并不是新单元格,而是新行 (|-)。 避免的方法是在负号前加空格(| -6)或者使用单行单元格标记(|| -6)。

CSS对战属性

Table borders specified through CSS rather than the border attribute will render incorrectly in a small subset of text browsers.

常用列属性、列组和行组

The MediaWiki syntax for tables currently offers no support for specifying common attributes for columns (with the HTML element ‎<col />), column groups (HTML element ‎<colgroup>‎</colgroup>) and row groups (HTML elements ‎<thead>‎</thead>, ‎<tbody>‎</tbody> and ‎<tfoot>‎</tfoot>). Those standard HTML elements are not accepted even in their HTML or XHTML syntax.

All the rows and cells (header or data) of the table are rendered within a single implicit row group (HTML element ‎<tbody>‎</tbody>) without any attributes or styles.

Tables and the Visual Editor (VE)

See also: 帮助:可视化编辑器/用户指南

参见Phab: T108245: "Fully support basic table editing in the visual editor"。 See the list of tasks. Finished tasks are struck. It can be difficult to figure out from the technical language there what exactly has been improved, or what features have been added. Please add explanatory info below.

Can now move or delete columns and rows

Click on a column or row header. Then click on the arrow. From the popup menu click on "Move" or "Delete".

Insert blank row or column

From the same popup menu click on "Insert".

Copy table from web page to Visual Editor

It is possible to copy and paste a table from a web page directly into the Visual Editor (VE). To do so safely, use a sandbox and check the table for proper coding in wikitext source mode and proper display in the Visual Editor and in preview mode.

Helping tools

  • Excel2Wiki allows you to copy a spreadsheet from Excel, Apache OpenOffice, LibreOffice, or Gnumeric to convert it into wikicode table.

參見

备注

  1. 表格可以直接借助HTML的表格元件來製作,或是透過 wiki 格式語言在 wiki 網頁予以定義。 HTML表格元件與相關的使用方式在各式的網頁都有詳細的說明於此不再贅述。 使用 wiki 格式語言,相較於HTML表格元件的好處是當表格以字元符號架構出來的,使得你更容易的以文章編輯的角度看出表格的舖排樣子。
  2. HTML table cellpadding Attribute