Skip to content

Examples (table)

Bhsd edited this page Apr 15, 2026 · 5 revisions

Making a table sortable

Expand

You can remove bolding from section headers in the content of a page. This example demonstrates the use of TableToken.

// Making a table sortable (main)
// The content of a page containing a table
var content = `{| class="wikitable"
! Word
|-
| Foo
|-
| Bar
|}`,
	root = Parser.parse(content),
	table = root.querySelector('table');
if (table) {
	table.classList.add('sortable');
}
assert.equal(
	root,
	`{| class="wikitable sortable"
! Word
|-
| Foo
|-
| Bar
|}`,
);

Adding a row to a table

Expand

You can add a row to a table in the content of a page. This example demonstrates the use of TableToken and TdToken.innerText.

// Adding a row to a table (main)
// The content of a page containing a table
var content = `{|
! Rank !! Word
|-
| 1 | Foo
|-
| 2 | Bar
|}`,
	root = Parser.parse(content),
	table = root.querySelector('table'),
	rowCount = table?.getRowCount();
if (table) {
	// Insert an empty row at the end of the table
	table.insertTableRow(rowCount, undefined, '');
	table.getNthCell({row: rowCount, column: 0}).innerText = '3';
	table.getNthCell({row: rowCount, column: 1}).innerText = 'Baz';
}
assert.equal(
	root,
	`{|
! Rank !! Word
|-
| 1 | Foo
|-
| 2 | Bar
|-
|3
|Baz
|}`,
);

Clone this wiki locally