Allow passing arbitrary link attributes to TitleColumn

pull/8555/head
Matt Westcott 2022-05-19 15:34:31 +01:00 zatwierdzone przez LB (Ben Johnston)
rodzic a67fd85fcf
commit 5994cc43df
4 zmienionych plików z 19 dodań i 6 usunięć

Wyświetl plik

@ -0,0 +1 @@
{% for name, value in attrs.items %}{% if value is not False %} {{ name }}{% if value is not True %}="{{ value|stringformat:'s' }}"{% endif %}{% endif %}{% endfor %}

Wyświetl plik

@ -1,7 +1,7 @@
<td class="{% if column.classname %}{{ column.classname }} {% endif %}title">
<div class="title-wrapper">
{% if link_url %}
<a href="{{ link_url }}" {% if link_classname %}class="{{ link_classname }}"{% endif %}>{{ value }}</a>
<a {% include "wagtailadmin/tables/attrs.html" with attrs=link_attrs %}>{{ value }}</a>
{% else %}
{{ value }}
{% endif %}

Wyświetl plik

@ -94,6 +94,7 @@ class TestTable(TestCase):
"hostname",
url_name="wagtailsites:edit",
link_classname="choose-site",
link_attrs={"data-chooser": "yes"},
),
Column("site_name", label="Site name"),
],
@ -112,7 +113,7 @@ class TestTable(TestCase):
<tr>
<td class="title">
<div class="title-wrapper">
<a href="/admin/sites/%d/" class="choose-site">blog.example.com</a>
<a href="/admin/sites/%d/" class="choose-site" data-chooser="yes">blog.example.com</a>
</div>
</td>
<td>My blog</td>
@ -120,7 +121,7 @@ class TestTable(TestCase):
<tr>
<td class="title">
<div class="title-wrapper">
<a href="/admin/sites/%d/" class="choose-site">gallery.example.com</a>
<a href="/admin/sites/%d/" class="choose-site" data-chooser="yes">gallery.example.com</a>
</div>
</td>
<td>My gallery</td>

Wyświetl plik

@ -128,17 +128,28 @@ class TitleColumn(Column):
cell_template_name = "wagtailadmin/tables/title_cell.html"
def __init__(
self, name, url_name=None, get_url=None, link_classname=None, **kwargs
self,
name,
url_name=None,
get_url=None,
link_classname=None,
link_attrs=None,
**kwargs,
):
super().__init__(name, **kwargs)
self.url_name = url_name
self._get_url_func = get_url
self.link_attrs = link_attrs or {}
self.link_classname = link_classname
def get_cell_context_data(self, instance, parent_context):
context = super().get_cell_context_data(instance, parent_context)
context["link_url"] = self.get_link_url(instance, parent_context)
context["link_classname"] = self.link_classname
context["link_attrs"] = self.link_attrs.copy()
context["link_attrs"]["href"] = context["link_url"] = self.get_link_url(
instance, parent_context
)
if self.link_classname is not None:
context["link_attrs"]["class"] = self.link_classname
return context
def get_link_url(self, instance, parent_context):