mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Support setting arbitrary attributes on table rows
This commit is contained in:
parent
9a1862adf8
commit
01752bbc90
@ -15,7 +15,7 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in table.rows %}
|
||||
<tr {% if row.classname %}class="{{ row.classname }}"{% endif %}>
|
||||
<tr{% include "wagtailadmin/tables/attrs.html" with attrs=row.attrs %}>
|
||||
{% for cell in row.values %}
|
||||
{% component cell %}
|
||||
{% endfor %}
|
||||
|
@ -198,3 +198,52 @@ class TestTable(TestCase):
|
||||
</table>
|
||||
""",
|
||||
)
|
||||
|
||||
def test_row_attrs(self):
|
||||
class SiteTable(Table):
|
||||
def get_row_attrs(self, instance):
|
||||
attrs = super().get_row_attrs(instance)
|
||||
attrs["data-id"] = instance.pk
|
||||
return attrs
|
||||
|
||||
root_page = Page.objects.filter(depth=2).first()
|
||||
blog = Site.objects.create(
|
||||
hostname="blog.example.com",
|
||||
site_name="My blog",
|
||||
root_page=root_page,
|
||||
is_default_site=True,
|
||||
)
|
||||
gallery = Site.objects.create(
|
||||
hostname="gallery.example.com", site_name="My gallery", root_page=root_page
|
||||
)
|
||||
data = [blog, gallery]
|
||||
|
||||
table = SiteTable(
|
||||
[
|
||||
Column("hostname"),
|
||||
Column("site_name", label="Site name"),
|
||||
],
|
||||
data,
|
||||
)
|
||||
|
||||
html = self.render_component(table)
|
||||
self.assertHTMLEqual(
|
||||
html,
|
||||
f"""
|
||||
<table class="listing">
|
||||
<thead>
|
||||
<tr><th>Hostname</th><th>Site name</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr data-id="{blog.pk}">
|
||||
<td>blog.example.com</td>
|
||||
<td>My blog</td>
|
||||
</tr>
|
||||
<tr data-id="{gallery.pk}">
|
||||
<td>gallery.example.com</td>
|
||||
<td>My gallery</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
""",
|
||||
)
|
||||
|
@ -365,6 +365,13 @@ class Table(Component):
|
||||
def get_row_classname(self, instance):
|
||||
return ""
|
||||
|
||||
def get_row_attrs(self, instance):
|
||||
attrs = {}
|
||||
classname = self.get_row_classname(instance)
|
||||
if classname:
|
||||
attrs["class"] = classname
|
||||
return attrs
|
||||
|
||||
def has_column_widths(self):
|
||||
return any(column.width for column in self.columns.values())
|
||||
|
||||
@ -393,6 +400,10 @@ class Table(Component):
|
||||
def classname(self):
|
||||
return self.table.get_row_classname(self.instance)
|
||||
|
||||
@cached_property
|
||||
def attrs(self):
|
||||
return self.table.get_row_attrs(self.instance)
|
||||
|
||||
|
||||
class InlineActionsTable(Table):
|
||||
classname = "listing listing--inline-actions"
|
||||
|
Loading…
Reference in New Issue
Block a user