mkdocs-material/docs/reference/data-tables.md

192 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2020-08-04 23:22:47 +03:00
---
2021-12-16 19:08:57 +03:00
icon: material/table-edit
2020-08-04 23:22:47 +03:00
---
# Data tables
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].
2020-08-04 23:22:47 +03:00
[sortable tables]: #sortable-tables
2023-09-14 20:09:18 +03:00
[additional JavaScript]: ../customization.md#additional-javascript
2020-08-04 23:22:47 +03:00
## Configuration
This configuration enables Markdown table support, which should normally be
enabled by default, but to be sure, add the following lines to `mkdocs.yml`:
``` yaml
markdown_extensions:
- tables
```
See additional configuration options:
- [Tables]
[Tables]: ../setup/extensions/python-markdown.md#tables
2020-08-04 23:22:47 +03:00
## Usage
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]:
2020-08-04 23:22:47 +03:00
``` markdown title="Data table"
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
[icons and emojis]: icons-emojis.md
2020-08-04 23:22:47 +03:00
### Column alignment
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
2020-08-04 23:22:47 +03:00
and/or end of the divider.
=== "Left"
2022-01-11 12:19:33 +03:00
``` markdown hl_lines="2" title="Data table, columns aligned to left"
2020-08-04 23:22:47 +03:00
| Method | Description |
| :---------- | :----------------------------------- |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
2020-08-04 23:22:47 +03:00
| Method | Description |
| :---------- | :----------------------------------- |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
2020-08-04 23:22:47 +03:00
=== "Center"
2020-08-04 23:22:47 +03:00
2022-01-11 12:19:33 +03:00
``` markdown hl_lines="2" title="Data table, columns centered"
2020-08-04 23:22:47 +03:00
| Method | Description |
| :---------: | :----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
2020-08-04 23:22:47 +03:00
| Method | Description |
| :---------: | :----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
2020-08-04 23:22:47 +03:00
=== "Right"
2020-08-04 23:22:47 +03:00
2022-01-11 12:19:33 +03:00
``` markdown hl_lines="2" title="Data table, columns aligned to right"
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------: | -----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------: | -----------------------------------: |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
[regular Markdown syntax]: https://www.markdownguide.org/extended-syntax/#tables
2020-08-04 23:22:47 +03:00
## Customization
### Sortable tables
If you want to make data tables sortable, you can add [tablesort], which is
2020-08-06 09:59:27 +03:00
natively integrated with Material for MkDocs and will also work with [instant
loading] via [additional JavaScript]:
2020-08-04 23:22:47 +03:00
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `docs/javascripts/tablesort.js`"
2020-08-04 23:22:47 +03:00
``` js
2021-02-23 00:27:30 +03:00
document$.subscribe(function() {
2021-12-07 01:51:34 +03:00
var tables = document.querySelectorAll("article table:not([class])")
2020-08-04 23:22:47 +03:00
tables.forEach(function(table) {
new Tablesort(table)
})
})
```
2022-09-11 20:25:40 +03:00
=== ":octicons-file-code-16: `mkdocs.yml`"
2020-08-04 23:22:47 +03:00
``` yaml
extra_javascript:
- https://unpkg.com/tablesort@5.3.0/dist/tablesort.min.js
2021-10-10 13:19:14 +03:00
- javascripts/tablesort.js
2020-08-04 23:22:47 +03:00
```
After applying the customization, data tables can be sorted by clicking on a
column:
2020-08-04 23:22:47 +03:00
2022-01-11 12:19:33 +03:00
``` markdown title="Data table, columns sortable"
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
```
<div class="result" markdown>
2020-08-04 23:22:47 +03:00
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
</div>
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/tablesort@5.3.0/dist/tablesort.min.js"></script>
2020-08-04 23:22:47 +03:00
<script>
var tables = document.querySelectorAll("article table")
new Tablesort(tables.item(tables.length - 1));
</script>
[tablesort]: http://tristen.ca/tablesort/demo/
[instant loading]: ../setup/setting-up-navigation.md#instant-loading
### Import table from file
2023-09-14 20:09:18 +03:00
The plugin [mkdocs-table-reader-plugin][table-reader-docs] allows you to
import data from a CSV or Excel file.
2023-09-14 20:09:18 +03:00
[table-reader-docs]: https://timvink.github.io/mkdocs-table-reader-plugin/