icon |
---|
material/table-edit |
Material for MkDocs defines default styles for data tables – an excellent way of rendering tabular data in project documentation. Furthermore, customizations like sortable tables can be achieved with a third-party library and some additional JavaScript.
This configuration enables Markdown table support, which should normally be
enabled by default, but to be sure, add the following lines to mkdocs.yml
:
markdown_extensions:
- tables
See additional configuration options:
Data tables can be used at any position in your project documentation and can contain arbitrary Markdown, including inline code blocks, as well as icons and emojis:
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
Method | Description |
---|---|
GET |
:material-check: Fetch resource |
PUT |
:material-check-all: Update resource |
DELETE |
:material-close: Delete resource |
If you want to align a specific column to the left
, center
or right
, you
can use the regular Markdown syntax placing :
characters at the beginning
and/or end of the divider.
=== "Left"
``` markdown hl_lines="2" title="Data table, columns aligned to left"
| Method | Description |
| :---------- | :----------------------------------- |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
| Method | Description |
| :---------- | :----------------------------------- |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
=== "Center"
``` markdown hl_lines="2" title="Data table, columns centered"
| Method | Description |
| :---------: | :----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
| Method | Description |
| :---------: | :----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
=== "Right"
``` markdown hl_lines="2" title="Data table, columns aligned to right"
| Method | Description |
| ----------: | -----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
| Method | Description |
| ----------: | -----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
If you want to make data tables sortable, you can add tablesort, which is natively integrated with Material for MkDocs and will also work with instant loading via additional JavaScript:
=== ":octicons-file-code-16: docs/javascripts/tablesort.js
"
``` js
document$.subscribe(function() {
var tables = document.querySelectorAll("article table:not([class])")
tables.forEach(function(table) {
new Tablesort(table)
})
})
```
=== ":octicons-file-code-16: mkdocs.yml
"
``` yaml
extra_javascript:
- https://unpkg.com/[email protected]/dist/tablesort.min.js
- javascripts/tablesort.js
```
After applying the customization, data tables can be sorted by clicking on a column:
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
Method | Description |
---|---|
GET |
:material-check: Fetch resource |
PUT |
:material-check-all: Update resource |
DELETE |
:material-close: Delete resource |
Note that tablesort provides alternative comparison implementations like numbers, filesizes, dates and month names. See the [tablesort documentation] tablesort for more information.
<script src="https://unpkg.com/[email protected]/dist/tablesort.min.js"></script> <script> var tables = document.querySelectorAll("article table") new Tablesort(tables.item(tables.length - 1)); </script>The plugin mkdocs-table-reader-plugin allows you to import data from a CSV or Excel file.