Examples (table) (EN) - bhsd-harry/wikiparser-node GitHub Wiki

Other Languages

Making a table sortable

Expand

You can make a table sortable 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
import type {TableToken} from "wikiparser-node";
const content = `{| class="wikitable"
! Word
|-
| Foo
|-
| Bar
|}`,
	root = Parser.parse(content),
	table = root.querySelector<TableToken>("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
import type {TableToken} from "wikiparser-node";
const content = `{|
! Rank !! Word
|-
| 1 | Foo
|-
| 2 | Bar
|}`,
	root = Parser.parse(content),
	table = root.querySelector<TableToken>("table");
if (table) {
	// Insert an empty row at the end of the table
	const rowCount = table.getRowCount();
	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
|}`,
);
⚠️ **GitHub.com Fallback** ⚠️