django-mvp-tables 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_mvp_tables-0.1.0/.gitignore +65 -0
- django_mvp_tables-0.1.0/LICENSE +21 -0
- django_mvp_tables-0.1.0/PKG-INFO +549 -0
- django_mvp_tables-0.1.0/README.md +520 -0
- django_mvp_tables-0.1.0/mvp_tables/__init__.py +1 -0
- django_mvp_tables-0.1.0/mvp_tables/apps.py +19 -0
- django_mvp_tables-0.1.0/mvp_tables/static/mvp_tables/tables.css +277 -0
- django_mvp_tables-0.1.0/mvp_tables/templates/cotton/mvp_tables/django_table.html +49 -0
- django_mvp_tables-0.1.0/mvp_tables/templates/cotton/mvp_tables/toolbar.html +67 -0
- django_mvp_tables-0.1.0/mvp_tables/templates/django_tables2/bootstrap5-mvp.html +146 -0
- django_mvp_tables-0.1.0/mvp_tables/templates/table_view.html +157 -0
- django_mvp_tables-0.1.0/mvp_tables/templatetags/__init__.py +1 -0
- django_mvp_tables-0.1.0/mvp_tables/templatetags/mvp_tables.py +154 -0
- django_mvp_tables-0.1.0/mvp_tables/views.py +188 -0
- django_mvp_tables-0.1.0/pyproject.toml +176 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Temporary and binary files
|
|
2
|
+
*~
|
|
3
|
+
*.ruff_cache
|
|
4
|
+
*.mypy_cache
|
|
5
|
+
*.py[cod]
|
|
6
|
+
*.so
|
|
7
|
+
*.cfg
|
|
8
|
+
!.isort.cfg
|
|
9
|
+
!setup.cfg
|
|
10
|
+
*.orig
|
|
11
|
+
*.log
|
|
12
|
+
*.pot
|
|
13
|
+
**/__pycache__
|
|
14
|
+
.cache/*
|
|
15
|
+
.*.swp
|
|
16
|
+
*/.ipynb_checkpoints/*
|
|
17
|
+
.DS_Store
|
|
18
|
+
.vscode/
|
|
19
|
+
.github/instructions/
|
|
20
|
+
docs/api/
|
|
21
|
+
|
|
22
|
+
# Unittest and coverage
|
|
23
|
+
htmlcov
|
|
24
|
+
.coverage
|
|
25
|
+
.coverage.*
|
|
26
|
+
.tox
|
|
27
|
+
junit*.xml
|
|
28
|
+
coverage.xml
|
|
29
|
+
.pytest_cache/
|
|
30
|
+
|
|
31
|
+
# Build and docs folder/files
|
|
32
|
+
build/*
|
|
33
|
+
dist/*
|
|
34
|
+
sdist/*
|
|
35
|
+
docs/_rst/*
|
|
36
|
+
docs/_build/*
|
|
37
|
+
cover/*
|
|
38
|
+
MANIFEST
|
|
39
|
+
|
|
40
|
+
# Per-project virtualenvs
|
|
41
|
+
.venv*/
|
|
42
|
+
.conda*/
|
|
43
|
+
.python-version
|
|
44
|
+
|
|
45
|
+
# Django stuff:
|
|
46
|
+
*.log
|
|
47
|
+
local_settings.py
|
|
48
|
+
db.sqlite3
|
|
49
|
+
db.sqlite3-journal
|
|
50
|
+
media/
|
|
51
|
+
/static/
|
|
52
|
+
/*.sqlite3
|
|
53
|
+
|
|
54
|
+
# */static/vendor/*
|
|
55
|
+
|
|
56
|
+
# Environments
|
|
57
|
+
.env
|
|
58
|
+
.venv
|
|
59
|
+
env/
|
|
60
|
+
venv/
|
|
61
|
+
ENV/
|
|
62
|
+
env.bak/
|
|
63
|
+
venv.bak/
|
|
64
|
+
|
|
65
|
+
dist
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Samuel Jennings
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: django-mvp-tables
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: django-tables2 table pages for django-mvp projects: a list view that renders a table, its template pack and its column styling
|
|
5
|
+
Project-URL: Homepage, https://github.com/django-mvp/django-mvp-tables
|
|
6
|
+
Project-URL: Repository, https://github.com/django-mvp/django-mvp-tables
|
|
7
|
+
Project-URL: Issues, https://github.com/django-mvp/django-mvp-tables/issues
|
|
8
|
+
Author-email: Samuel Jennings <samuel.scott.jennings@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: cotton,django,django-mvp
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Environment :: Web Environment
|
|
14
|
+
Classifier: Framework :: Django
|
|
15
|
+
Classifier: Framework :: Django :: 5.2
|
|
16
|
+
Classifier: Framework :: Django :: 6.0
|
|
17
|
+
Classifier: Framework :: Django :: 6.1
|
|
18
|
+
Classifier: Intended Audience :: Developers
|
|
19
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
20
|
+
Classifier: Natural Language :: English
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
24
|
+
Requires-Python: <4.0,>=3.12
|
|
25
|
+
Requires-Dist: django-mvp<0.43,>=0.42.1
|
|
26
|
+
Requires-Dist: django-tables2<4,>=3.0.0
|
|
27
|
+
Requires-Dist: django>=5.2
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# django-mvp-tables
|
|
31
|
+
|
|
32
|
+
[](https://github.com/django-mvp/django-mvp-tables/actions/workflows/tests.yml)
|
|
33
|
+
[](https://github.com/django-mvp/django-mvp-tables/actions/workflows/build.yml)
|
|
34
|
+
[](https://github.com/django-mvp/django-mvp-tables/actions/workflows/publish.yml)
|
|
35
|
+
[](https://codecov.io/gh/django-mvp/django-mvp-tables)
|
|
36
|
+
[](https://pypi.org/project/django-mvp-tables/)
|
|
37
|
+
[](https://pypi.org/project/django-mvp-tables/)
|
|
38
|
+
[](https://pypi.org/project/django-mvp-tables/)
|
|
39
|
+
[](https://pypi.org/project/django-mvp-tables/)
|
|
40
|
+
[](https://github.com/django-mvp/django-mvp-tables/blob/main/LICENSE)
|
|
41
|
+
[](https://github.com/astral-sh/ruff)
|
|
42
|
+
|
|
43
|
+
Table pages for [django-mvp](https://github.com/django-mvp/django-mvp) projects,
|
|
44
|
+
built on [django-tables2](https://github.com/jieter/django-tables2): a list
|
|
45
|
+
view that renders a table, the template that draws it in django-mvp's theme,
|
|
46
|
+
and the styling of its columns.
|
|
47
|
+
|
|
48
|
+
## Scope & philosophy
|
|
49
|
+
|
|
50
|
+
This package puts a django-tables2 table on a django-mvp page. It supplies the
|
|
51
|
+
table view, the page around the table (toolbar, filter panel, a scrolling
|
|
52
|
+
region with a sticky heading row, and the pager), the template that draws the
|
|
53
|
+
table in django-mvp's markup, and the classes that set how each column wraps
|
|
54
|
+
and aligns.
|
|
55
|
+
|
|
56
|
+
It stays out of everything django-tables2 and django-mvp already do. Columns,
|
|
57
|
+
ordering and row data are the table's, written with django-tables2 as usual.
|
|
58
|
+
Search, filters, the toolbar and the page shell are django-mvp's, and a table
|
|
59
|
+
page uses them unchanged. There are no models, no migrations and no URLs here.
|
|
60
|
+
|
|
61
|
+
When two designs conflict, the one that leaves django-tables2's own API
|
|
62
|
+
working as its documentation describes wins. A host project that knows
|
|
63
|
+
django-tables2 should not have to learn a second way to declare a table.
|
|
64
|
+
|
|
65
|
+
## Installation
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install django-mvp-tables
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
This brings in django-tables2 and django-mvp. Add both apps to
|
|
72
|
+
`INSTALLED_APPS`, with `mvp_tables` above `mvp`:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
INSTALLED_APPS = [
|
|
76
|
+
# ...
|
|
77
|
+
"django_tables2",
|
|
78
|
+
"mvp_tables",
|
|
79
|
+
"mvp",
|
|
80
|
+
]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`mvp_tables` goes above `mvp` because django-mvp 0.42 still ships its own
|
|
84
|
+
copies of these templates under the same names. Django uses the first app that
|
|
85
|
+
has a template, so listing this package first is what puts its templates on
|
|
86
|
+
the page.
|
|
87
|
+
|
|
88
|
+
The package renders inside django-mvp's layout and reads its colours from the
|
|
89
|
+
theme django-mvp supplies, so it does nothing useful on its own. Its
|
|
90
|
+
stylesheet is sent to the page's `<head>` by the table template itself. There
|
|
91
|
+
is nothing to link and nothing to build.
|
|
92
|
+
|
|
93
|
+
## Quickstart
|
|
94
|
+
|
|
95
|
+
Write the table with django-tables2 as usual, then give it a view:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import django_tables2 as tables
|
|
99
|
+
from mvp_tables.views import MVPTableView
|
|
100
|
+
|
|
101
|
+
from .filters import ProductFilter
|
|
102
|
+
from .models import Product
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class ProductTable(tables.Table):
|
|
106
|
+
class Meta:
|
|
107
|
+
model = Product
|
|
108
|
+
fields = ("name", "category", "price", "stock")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class ProductTableView(MVPTableView):
|
|
112
|
+
model = Product
|
|
113
|
+
table_class = ProductTable
|
|
114
|
+
filterset_class = ProductFilter
|
|
115
|
+
paginate_by = 25
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`MVPTableView` is django-mvp's list page with the results drawn as a
|
|
119
|
+
django-tables2 table. It keeps the list page's filterset, pagination and page
|
|
120
|
+
chrome, and renders through the `table_view.html` template.
|
|
121
|
+
`MVPTableViewMixin` holds the same behaviour for composing with a view class
|
|
122
|
+
of your own.
|
|
123
|
+
|
|
124
|
+
## Switching from django-mvp's table page
|
|
125
|
+
|
|
126
|
+
django-mvp up to 0.42 ships this table page as
|
|
127
|
+
`mvp.integrations.django_tables`. Moving to this package takes three edits:
|
|
128
|
+
|
|
129
|
+
1. Install `django-mvp-tables` and add `"django_tables2"` and `"mvp_tables"`
|
|
130
|
+
to `INSTALLED_APPS`, above `"mvp"`.
|
|
131
|
+
2. Change the import:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
# before
|
|
135
|
+
from mvp.integrations.django_tables.views import MVPTableView, MVPTableViewMixin
|
|
136
|
+
|
|
137
|
+
# after
|
|
138
|
+
from mvp_tables.views import MVPTableView, MVPTableViewMixin
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
3. If a template of yours places a component or loads a tag from the table
|
|
142
|
+
page, rename it:
|
|
143
|
+
|
|
144
|
+
| Before | After |
|
|
145
|
+
| --- | --- |
|
|
146
|
+
| `<c-mvp.addons.django-table>` | `<c-mvp-tables.django-table>` |
|
|
147
|
+
| `<c-mvp.page.table.toolbar>` | `<c-mvp-tables.toolbar>` |
|
|
148
|
+
| `{% load mvp %}` for `table_cell_attrs`, `column_alignment_class` or `row_header_columns` | `{% load mvp_tables %}` |
|
|
149
|
+
|
|
150
|
+
Everything else keeps its name: `table_view.html` and its blocks, the
|
|
151
|
+
`django_tables2/bootstrap5-mvp.html` template pack, the `mvp-col-*` classes and
|
|
152
|
+
the `MVP_CONFIG["table"]["wrap"]` setting.
|
|
153
|
+
|
|
154
|
+
## The table page
|
|
155
|
+
|
|
156
|
+
The page fills the screen. Three bands stay put and one region scrolls between
|
|
157
|
+
them:
|
|
158
|
+
|
|
159
|
+
1. **The toolbar.** A bar with the title and a count of the records, and a row
|
|
160
|
+
under it laid out as a list page's toolbar: the filter form with the
|
|
161
|
+
filterset fields the view names in `toolbar_filters`, then, at the end of
|
|
162
|
+
the row, the Filters button that opens the filter panel with the rest and
|
|
163
|
+
the add button. The search and filters that are applied are listed under the
|
|
164
|
+
filter form. The filter panel itself sits beside the table, at the start and
|
|
165
|
+
level with the table, and scrolls on its own. The bar holds the title in the
|
|
166
|
+
compact layout, the table page's default. In any other
|
|
167
|
+
[title layout](https://github.com/django-mvp/django-mvp/blob/main/docs/layout.md#page-title)
|
|
168
|
+
the title is drawn above the bar, and the bar keeps the count and the
|
|
169
|
+
controls.
|
|
170
|
+
2. **The table.** The rows scroll in a region of their own. The heading row
|
|
171
|
+
stays at the top of it and the footer row, where the table declares one, at
|
|
172
|
+
the bottom, so one scrollbar runs beside all three.
|
|
173
|
+
3. **The pager**, only when there is more than one page: the range shown and
|
|
174
|
+
numbered pages on a wide screen, and Previous, a page picker and Next on a
|
|
175
|
+
phone.
|
|
176
|
+
|
|
177
|
+
### The toolbar opens and closes
|
|
178
|
+
|
|
179
|
+
A button at the end of the bar closes the panel, which hands its height to the
|
|
180
|
+
rows, and opens it again. The choice is remembered in the browser, so a reader
|
|
181
|
+
who works with the toolbar closed keeps it closed. While a search or filter is
|
|
182
|
+
applied, the bar says how many, open or closed, so a closed toolbar never hides
|
|
183
|
+
that the table is a subset.
|
|
184
|
+
|
|
185
|
+
The button closes the filter form and the applied filters only. The Filters
|
|
186
|
+
button, the add button and the rest of `page.actions` move up into the bar,
|
|
187
|
+
before the button, so they are there with the panel closed and the button
|
|
188
|
+
stays where it was. A view with no filterset has no panel and no button to
|
|
189
|
+
open one, and its controls sit in the bar.
|
|
190
|
+
|
|
191
|
+
### Surfaces and edges
|
|
192
|
+
|
|
193
|
+
The whole page is one surface. The toolbar, the heading row, the rows, the
|
|
194
|
+
footer row and the pager are all drawn in the page's own background, whatever
|
|
195
|
+
surface the applied theme puts the page on, so the page looks the same in
|
|
196
|
+
every theme. A `<c-mvp-tables.django-table>` you place yourself, inside a card
|
|
197
|
+
for example, takes `base-100` as its ground instead.
|
|
198
|
+
|
|
199
|
+
The parts are told apart by hairlines, in the weight daisyUI gives the line
|
|
200
|
+
between two rows. The heading row also takes a small shadow while rows are
|
|
201
|
+
scrolled under it, as the app header does when it is stuck, and a
|
|
202
|
+
[pinned column](#columns-that-identify-the-row) takes one while the table is
|
|
203
|
+
scrolled sideways. A row under the pointer is tinted with a thin wash of the
|
|
204
|
+
theme's text colour.
|
|
205
|
+
|
|
206
|
+
Four classes are there for a stylesheet of your own to select on:
|
|
207
|
+
|
|
208
|
+
| Class | On | When |
|
|
209
|
+
| --- | --- | --- |
|
|
210
|
+
| `mvp-table-page` | the page | always, on a table page |
|
|
211
|
+
| `mvp-table-pager` | the pager's bar | always |
|
|
212
|
+
| `mvp-table-over-top` | the scroll region | while the rows are scrolled down from the top |
|
|
213
|
+
| `mvp-table-over-side` | the scroll region | while the table is scrolled sideways |
|
|
214
|
+
|
|
215
|
+
### A table that does not fill the page
|
|
216
|
+
|
|
217
|
+
The footer row sits at the bottom of the region however few rows there are.
|
|
218
|
+
With no rows at all, the empty state is drawn between the heading row and the
|
|
219
|
+
footer row: the view's `empty_state_heading` and message for a table with no
|
|
220
|
+
records, or the nothing-matches message with a link that clears the search
|
|
221
|
+
and filters.
|
|
222
|
+
|
|
223
|
+
### Putting your own controls in the toolbar
|
|
224
|
+
|
|
225
|
+
Each part of the toolbar is a block in `table_view.html`:
|
|
226
|
+
|
|
227
|
+
| Block | Holds |
|
|
228
|
+
| --- | --- |
|
|
229
|
+
| `page.header` | Empty. Anything here is drawn above the toolbar |
|
|
230
|
+
| `page.toolbar` | The whole toolbar |
|
|
231
|
+
| `page.title` | The title area: the eyebrow, heading, lead and info icon. In the compact layout, the default, it is the start of the bar. In any other layout it is drawn above the bar |
|
|
232
|
+
| `page.eyebrow`, `page.heading`, `page.lead`, `page.info` | One argument of the title each, inside `page.title` |
|
|
233
|
+
| `page.summary` | The count, in the bar beside the title |
|
|
234
|
+
| `page.controls` | The end of the toolbar row: the Filters button, then `page.actions`. Shown with the panel closed |
|
|
235
|
+
| `page.actions` | The view's `page_actions`, then the add button, inside `page.controls`. Write `{{ block.super }}` to keep both |
|
|
236
|
+
| `page.panel` | The part of the toolbar the button closes: the filter form and the applied search and filters |
|
|
237
|
+
| `page.filters` | The filter form, inside `page.panel` |
|
|
238
|
+
| `page.side` | The filter panel beside the table. Drawn from the view's `panel_fields`, and empty when every field is in the toolbar |
|
|
239
|
+
| `page.refinements` | The applied search and filters, inside `page.panel` |
|
|
240
|
+
| `page.content` | The table |
|
|
241
|
+
| `page.footer` | The pager |
|
|
242
|
+
|
|
243
|
+
```django
|
|
244
|
+
{% extends "table_view.html" %}
|
|
245
|
+
{% block page.actions %}
|
|
246
|
+
<c-button icon="export" text="Export" href="{% url 'product-export' %}" />
|
|
247
|
+
{{ block.super }}
|
|
248
|
+
{% endblock page.actions %}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
`{{ block.super }}` is the view's actions and the add button, so where you
|
|
252
|
+
write it decides where they go: before your own actions, after them, or
|
|
253
|
+
nowhere if you leave it out. `page.filters`, `page.controls` and
|
|
254
|
+
`page.actions` are the same blocks, holding the same controls in the same
|
|
255
|
+
order, as on a list page, so one override serves both.
|
|
256
|
+
|
|
257
|
+
A context key called `actions` is safe on a table page. The toolbar is
|
|
258
|
+
`<c-mvp-tables.toolbar>`, which declares each of its slots, so an unfilled one
|
|
259
|
+
never falls through to a context variable of the same name.
|
|
260
|
+
|
|
261
|
+
### Filters and sorting
|
|
262
|
+
|
|
263
|
+
The toolbar draws the same controls a list page does, and decides on them the
|
|
264
|
+
same way: each one appears when the view configures the thing it drives. Give
|
|
265
|
+
the view a filterset and its search and filters appear. Set
|
|
266
|
+
`show_create_action` and the add button does.
|
|
267
|
+
|
|
268
|
+
The filterset's fields are placed as on a list page. Those named in
|
|
269
|
+
`toolbar_filters` are drawn in the toolbar's panel, in that order, and every
|
|
270
|
+
other field is in the filter panel beside the table. Name them all and there
|
|
271
|
+
is no filter panel and no Filters button:
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
class ProductTableView(MVPTableView):
|
|
275
|
+
model = Product
|
|
276
|
+
table_class = ProductTable
|
|
277
|
+
filterset_class = ProductFilter
|
|
278
|
+
toolbar_filters = ["q", "status"]
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
No sort control appears on a table page. A table view leaves out any
|
|
282
|
+
`SortFilter` its filterset declares: it is not drawn, a value for it in the
|
|
283
|
+
address is ignored, and the rows keep the table's own order. That holds when
|
|
284
|
+
`toolbar_filters` names the sort too, which is skipped without an error. So
|
|
285
|
+
one filterset, sort and all, serves a list page and a table page unchanged.
|
|
286
|
+
|
|
287
|
+
The view raises `ImproperlyConfigured` if you declare `order_by` on it. A
|
|
288
|
+
table already sorts through its own column headings, against the sortable
|
|
289
|
+
columns the table class defines, and an ordering on the view as well would
|
|
290
|
+
give the same table two competing sources for it. Put the ordering on the
|
|
291
|
+
table class, as its own `order_by` or `Meta.order_by`. The refusal happens as
|
|
292
|
+
the class is defined, so a view that declares an ordering fails when Django
|
|
293
|
+
imports the module holding it, naming the class in the message.
|
|
294
|
+
|
|
295
|
+
A search or a filter keeps the order a column heading chose. The filter form
|
|
296
|
+
carries it, under the table's own sort parameter (`sort`, or the table's
|
|
297
|
+
prefix and `sort`), and the view publishes it to the page as `table_sort`,
|
|
298
|
+
`""` when no heading has been chosen. A project that overrides `page.filters`
|
|
299
|
+
with a form of its own keeps the order the same way:
|
|
300
|
+
|
|
301
|
+
```django
|
|
302
|
+
{% if table_sort %}
|
|
303
|
+
<input type="hidden" name="{{ table.prefixed_order_by_field }}" value="{{ table_sort }}">
|
|
304
|
+
{% endif %}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Pagination
|
|
308
|
+
|
|
309
|
+
The table paginates, and the count and links below it describe the table's
|
|
310
|
+
page. `paginate_by` sets the page size as it does on any list view.
|
|
311
|
+
|
|
312
|
+
The view does not paginate a second time. With one page, the row query and any
|
|
313
|
+
`select_related` or `prefetch_related` on it run once per page rather than
|
|
314
|
+
twice, and the count under the table describes the rows above it. That holds
|
|
315
|
+
under a column sort too, which is applied when the table is built.
|
|
316
|
+
|
|
317
|
+
Two things follow for anything that reads the context:
|
|
318
|
+
|
|
319
|
+
- `page_obj` is the table's page, so its `object_list` holds table rows rather
|
|
320
|
+
than model instances. Each row carries its instance as `row.record`.
|
|
321
|
+
- `object_list` and `<model>_list` hold the view's whole queryset rather than a
|
|
322
|
+
page of it. Nothing evaluates it unless a template asks, so it costs
|
|
323
|
+
nothing, but render rows from the table and not from there.
|
|
324
|
+
|
|
325
|
+
A page that shows several paged tables at once gives each pager the table's
|
|
326
|
+
own parameter, so moving through one table leaves the others on their page.
|
|
327
|
+
Build each table with a `prefix`, page it with `RequestConfig`, and pass
|
|
328
|
+
`table.prefixed_page_field` as the pager's `param`:
|
|
329
|
+
|
|
330
|
+
```django
|
|
331
|
+
<c-mvp.page.list.footer :page_obj="table.page" param="{{ table.prefixed_page_field }}" />
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
Leaving `paginate_by` unset means no pagination: the table renders every row,
|
|
335
|
+
and the count and links go with it. `table_pagination = False` says the same
|
|
336
|
+
thing explicitly, and wins where a view sets both.
|
|
337
|
+
|
|
338
|
+
A `?page=` that names no page, whether past the last one or not a number at
|
|
339
|
+
all, is a 404 rather than a quiet fall back to the first or last page. An
|
|
340
|
+
absent or empty one is page one.
|
|
341
|
+
|
|
342
|
+
## Columns
|
|
343
|
+
|
|
344
|
+
### Inferred alignment
|
|
345
|
+
|
|
346
|
+
The template pack aligns a column by the kind of model field behind it, with
|
|
347
|
+
nothing to declare:
|
|
348
|
+
|
|
349
|
+
| Column holds | Alignment |
|
|
350
|
+
| --- | --- |
|
|
351
|
+
| Text (`CharField`, `TextField`, a date, a foreign key and so on) | Leading (`text-start`) |
|
|
352
|
+
| A number (`IntegerField`, `DecimalField`, `FloatField`) | Trailing (`text-end`) |
|
|
353
|
+
| A boolean, or a column with no model field behind it that isn't orderable, such as an action column of buttons or links | Centred (`text-center`) |
|
|
354
|
+
|
|
355
|
+
It declines rather than guesses. A table built over data that isn't a queryset
|
|
356
|
+
has no model to resolve a field from. A column whose accessor resolves to no
|
|
357
|
+
field but is orderable is a plain unresolvable column, not an action column,
|
|
358
|
+
so its kind can't be told either way. Both render with no alignment class.
|
|
359
|
+
|
|
360
|
+
An alignment class in a column's own `attrs` always wins, and every cell of
|
|
361
|
+
the column takes it, heading and footer included:
|
|
362
|
+
|
|
363
|
+
```python
|
|
364
|
+
class ProductTable(tables.Table):
|
|
365
|
+
# Text by default, put at the trailing edge instead.
|
|
366
|
+
sku = tables.Column(attrs={"td": {"class": "text-end"}})
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Width and wrapping
|
|
370
|
+
|
|
371
|
+
A column says how it competes for width and treats its text by naming one or
|
|
372
|
+
more of these classes in its `attrs`:
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
class ProductTable(tables.Table):
|
|
376
|
+
name = tables.Column(attrs={"td": {"class": "mvp-col-grow"}})
|
|
377
|
+
sku = tables.Column(attrs={"td": {"class": "mvp-col-shrink"}})
|
|
378
|
+
description = tables.Column(
|
|
379
|
+
attrs={"td": {"class": "mvp-col-wrap mvp-col-max-md"}}
|
|
380
|
+
)
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
| Class | Effect |
|
|
384
|
+
| --- | --- |
|
|
385
|
+
| `mvp-col-grow` | Claims whatever width is left over once every other column has taken the width it needs. |
|
|
386
|
+
| `mvp-col-shrink` | Takes no more width than the column's own content needs, so a short code or date column never stretches to match a longer neighbour. |
|
|
387
|
+
| `mvp-col-wrap` | Lets a cell's text wrap onto more than one line instead of stretching the column to fit it on one. |
|
|
388
|
+
| `mvp-col-nowrap` | Keeps a cell's text on a single line rather than wrapping the row taller. On paper the cell wraps anyway, so that the column stays on the sheet. |
|
|
389
|
+
| `mvp-col-max-xs` | Stops a column at an `8rem` maximum width once its text is allowed to wrap. |
|
|
390
|
+
| `mvp-col-max-sm` | Stops a column at a `12rem` maximum width once its text is allowed to wrap. |
|
|
391
|
+
| `mvp-col-max-md` | Stops a column at a `16rem` maximum width once its text is allowed to wrap. |
|
|
392
|
+
| `mvp-col-max-lg` | Stops a column at a `24rem` maximum width once its text is allowed to wrap. |
|
|
393
|
+
| `mvp-col-max-xl` | Stops a column at a `32rem` maximum width once its text is allowed to wrap. |
|
|
394
|
+
|
|
395
|
+
Grow and shrink are opposites. Naming both on one column is a contradiction
|
|
396
|
+
the stylesheet does not settle for you, so choose one.
|
|
397
|
+
|
|
398
|
+
A table lays out with `table-layout: auto`, which settles each column's width
|
|
399
|
+
across every cell in it, heading included. A heading longer than anything in
|
|
400
|
+
the body wins, and `mvp-col-shrink` or `mvp-col-max-md` on the `td` alone then
|
|
401
|
+
looks like it did nothing. Where a column's heading is the long part, name the
|
|
402
|
+
class on both:
|
|
403
|
+
|
|
404
|
+
```python
|
|
405
|
+
sku = tables.Column(
|
|
406
|
+
attrs={"td": {"class": "mvp-col-shrink"}, "th": {"class": "mvp-col-shrink"}}
|
|
407
|
+
)
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
The wrap classes are the exception. Headings wrap whatever the project default
|
|
411
|
+
says, so a column is never widened by its own title. Name `mvp-col-nowrap` on
|
|
412
|
+
the `th` yourself if a heading must stay on one line.
|
|
413
|
+
|
|
414
|
+
### The project-wide wrap default
|
|
415
|
+
|
|
416
|
+
Whether a column wraps when it names neither `mvp-col-wrap` nor
|
|
417
|
+
`mvp-col-nowrap` is a project setting, in django-mvp's `MVP_CONFIG`:
|
|
418
|
+
|
|
419
|
+
```python
|
|
420
|
+
MVP_CONFIG = {
|
|
421
|
+
"table": {
|
|
422
|
+
"wrap": True, # every column wraps unless it says mvp-col-nowrap
|
|
423
|
+
},
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
The default is `False`, so a dense table keeps one row per record until you
|
|
428
|
+
say otherwise. The column's own class wins, then this setting, then the
|
|
429
|
+
default.
|
|
430
|
+
|
|
431
|
+
### Columns that identify the row
|
|
432
|
+
|
|
433
|
+
A column carrying the record's icon, its name or its reference does not hold
|
|
434
|
+
one of the row's values. It says which row this is. Name those columns on the
|
|
435
|
+
table's `Meta` and their body cells render as `<th scope="row">` instead of
|
|
436
|
+
`<td>`:
|
|
437
|
+
|
|
438
|
+
```python
|
|
439
|
+
class SampleTable(tables.Table):
|
|
440
|
+
icon = tables.TemplateColumn(template_name="sample/icon.html", verbose_name="")
|
|
441
|
+
name = tables.Column()
|
|
442
|
+
depth = tables.Column()
|
|
443
|
+
|
|
444
|
+
class Meta:
|
|
445
|
+
row_headers = ("icon", "name")
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
A single name may be given on its own: `row_headers = "name"`. The cell keeps
|
|
449
|
+
the column's `td` attributes, so a column loses none of its width behaviour by
|
|
450
|
+
becoming a row header. Naming a column the table does not have raises
|
|
451
|
+
`ImproperlyConfigured` when it renders, rather than being ignored.
|
|
452
|
+
|
|
453
|
+
A screen reader announces the rest of the row against a row header, so
|
|
454
|
+
"1250 metres" is read as the depth of the sample the header names. And
|
|
455
|
+
daisyUI's `table-pin-cols` selects on exactly this markup, so a column
|
|
456
|
+
declared here can be kept in view while a wide table scrolls sideways. Add
|
|
457
|
+
that class to the table area to turn the pinning on:
|
|
458
|
+
|
|
459
|
+
```django
|
|
460
|
+
{% block page.content %}
|
|
461
|
+
<c-mvp-tables.django-table :table="table" class="flex-1 min-h-0 table-pin-cols" />
|
|
462
|
+
{% endblock page.content %}
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
With
|
|
466
|
+
[Cotton's context isolation](https://github.com/django-mvp/django-mvp/blob/main/docs/components.md#switching-on-cottons-context-isolation)
|
|
467
|
+
switched on, pass the empty state's values on the tag as well, the way the
|
|
468
|
+
packaged `table_view.html` does, or a table with no rows draws an empty state
|
|
469
|
+
that knows nothing of the search and filters applied.
|
|
470
|
+
|
|
471
|
+
With a column pinned, the footer row's cell under it is a row header as well,
|
|
472
|
+
so a label such as "Total" stays at the leading edge with the column. On a
|
|
473
|
+
narrow screen a pinned column is held to under half the width, and a longer
|
|
474
|
+
value is cut with an ellipsis.
|
|
475
|
+
|
|
476
|
+
### A column with no heading
|
|
477
|
+
|
|
478
|
+
A column whose heading resolves to nothing renders an empty heading cell. The
|
|
479
|
+
cell stays, so the column keeps its width and its position in the row, and an
|
|
480
|
+
orderable column keeps the control that sorts by it. `verbose_name=""` is the
|
|
481
|
+
usual way to say it.
|
|
482
|
+
|
|
483
|
+
## Components
|
|
484
|
+
|
|
485
|
+
| Component | Takes | Draws |
|
|
486
|
+
| --- | --- | --- |
|
|
487
|
+
| `c-mvp-tables.django-table` | `table`, `class`, `label`, `role` (default `region`) | The scrollable region around a django-tables2 table, with its heading and footer rows pinned. A table with no rows draws django-mvp's empty state in its body, from `empty_state`, `refinements`, `clear_refinements_url`, `directory`, `create_form` and `model_info`, read from the page when not passed |
|
|
488
|
+
| `c-mvp-tables.toolbar` | slots `heading`, `title`, `actions`, `controls`, `note` and the default slot; `heading_above`, `storage_key`, `class` | The band above a full-page table: a bar that always shows and a panel under it that a button opens and closes |
|
|
489
|
+
|
|
490
|
+
Give `c-mvp-tables.django-table` a distinct `label` on any page with more than
|
|
491
|
+
one table.
|
|
492
|
+
|
|
493
|
+
The template pack is `django_tables2/bootstrap5-mvp.html`. `table_view.html`
|
|
494
|
+
uses it already. To draw a table of your own with it, name it on the table or
|
|
495
|
+
in `DJANGO_TABLES2_TEMPLATE`:
|
|
496
|
+
|
|
497
|
+
```python
|
|
498
|
+
class ProductTable(tables.Table):
|
|
499
|
+
class Meta:
|
|
500
|
+
template_name = "django_tables2/bootstrap5-mvp.html"
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
## Printing
|
|
504
|
+
|
|
505
|
+
A table page prints every row of the page being viewed, running across as many
|
|
506
|
+
sheets as it needs, with the heading and footer rows repeated on each.
|
|
507
|
+
|
|
508
|
+
A table fits the width of the paper with all of its columns. Its text and
|
|
509
|
+
padding get smaller, and every cell wraps, inside a word if it has to,
|
|
510
|
+
including a column that names `mvp-col-nowrap`. A table with many columns
|
|
511
|
+
reads better printed in landscape. To leave a column off the printout, name
|
|
512
|
+
`print:hidden` on each of its cells:
|
|
513
|
+
|
|
514
|
+
```python
|
|
515
|
+
class ProductTable(tables.Table):
|
|
516
|
+
barcode = tables.Column(
|
|
517
|
+
attrs={
|
|
518
|
+
"th": {"class": "print:hidden"},
|
|
519
|
+
"td": {"class": "print:hidden"},
|
|
520
|
+
"tf": {"class": "print:hidden"},
|
|
521
|
+
}
|
|
522
|
+
)
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
## Contributing
|
|
526
|
+
|
|
527
|
+
Standards for this repository live in
|
|
528
|
+
[CONSTITUTION.md](https://github.com/django-mvp/django-mvp-tables/blob/main/CONSTITUTION.md),
|
|
529
|
+
and the vocabulary to use in issues and commits lives in
|
|
530
|
+
[CONTEXT.md](https://github.com/django-mvp/django-mvp-tables/blob/main/CONTEXT.md).
|
|
531
|
+
|
|
532
|
+
```bash
|
|
533
|
+
uv sync
|
|
534
|
+
uv run pytest
|
|
535
|
+
uv run pre-commit install
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
`demo/` is a Django project on django-mvp's application shell, for looking at
|
|
539
|
+
this package in a browser while working on it:
|
|
540
|
+
|
|
541
|
+
```bash
|
|
542
|
+
uv run python manage.py migrate
|
|
543
|
+
uv run python manage.py seed_demo
|
|
544
|
+
uv run python manage.py runserver
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
## License
|
|
548
|
+
|
|
549
|
+
MIT. See [LICENSE](https://github.com/django-mvp/django-mvp-tables/blob/main/LICENSE).
|