0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Allow specifying column widths on tables

This commit is contained in:
Matt Westcott 2021-12-08 20:32:51 +00:00 committed by Matt Westcott
parent e3f38f0605
commit 77f8d160a7
3 changed files with 36 additions and 1 deletions

View File

@ -1,6 +1,11 @@
{% load wagtailadmin_tags %}
<table {% if table.classname %}class="{{ table.classname }}"{% endif %}>
{% if table.has_column_widths %}
{% for column in table.columns.values %}
<col {% if column.width %}width="{{ column.width }}"{% endif %} />
{% endfor %}
{% endif %}
<thead>
<tr>
{% for column in table.columns.values %}

View File

@ -40,6 +40,32 @@ class TestTable(TestCase):
</table>
''')
def test_table_render_with_width(self):
data = [
{'first_name': 'Paul', 'last_name': 'Simon'},
{'first_name': 'Art', 'last_name': 'Garfunkel'},
]
table = Table([
Column('first_name'),
Column('last_name', width='75%'),
], data)
html = self.render_component(table)
self.assertHTMLEqual(html, '''
<table class="listing">
<col />
<col width="75%" />
<thead>
<tr><th>First name</th><th>Last name</th></tr>
</thead>
<tbody>
<tr><td>Paul</td><td>Simon</td></tr>
<tr><td>Art</td><td>Garfunkel</td></tr>
</tbody>
</table>
''')
def test_title_column(self):
root_page = Page.objects.filter(depth=2).first()
blog = Site.objects.create(hostname='blog.example.com', site_name='My blog', root_page=root_page)

View File

@ -39,13 +39,14 @@ class Column(metaclass=MediaDefiningClass):
header_template_name = "wagtailadmin/tables/column_header.html"
cell_template_name = "wagtailadmin/tables/cell.html"
def __init__(self, name, label=None, accessor=None, classname=None, sort_key=None):
def __init__(self, name, label=None, accessor=None, classname=None, sort_key=None, width=None):
self.name = name
self.accessor = accessor or name
self.label = label or capfirst(name.replace('_', ' '))
self.classname = classname
self.sort_key = sort_key
self.header = Column.Header(self)
self.width = width
def get_header_context_data(self, parent_context):
"""
@ -208,6 +209,9 @@ class Table(Component):
for instance in self.data:
yield Table.Row(self.columns, instance)
def has_column_widths(self):
return any(column.width for column in self.columns.values())
class Row(Mapping):
# behaves as an OrderedDict whose items are the rendered results of
# the corresponding column's format_cell method applied to the instance