0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Use wagtail.admin.ui.tables framework for snippet chooser

This commit is contained in:
Matt Westcott 2021-12-08 21:47:16 +00:00 committed by Matt Westcott
parent 8724dc9b10
commit d7e6382d97
2 changed files with 27 additions and 18 deletions

View File

@ -1,4 +1,4 @@
{% load i18n wagtailadmin_tags wagtailsnippets_admin_tags %}
{% load i18n wagtailadmin_tags %}
{% if items %}
{% if is_searching %}
<h2 role="alert">
@ -10,23 +10,7 @@
</h2>
{% endif %}
<table class="listing">
<col />
<thead>
<tr class="table-headers">
<th>{% trans "Title" %}</th>
</tr>
</thead>
<tbody>
{% for snippet in items %}
<tr>
<td class="title">
<div class="title-wrapper"><a class="snippet-choice" href="{% url 'wagtailsnippets:chosen' model_opts.app_label model_opts.model_name snippet.pk|admin_urlquote %}">{{ snippet }}</a></div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% component table %}
{% url 'wagtailsnippets:choose_results' model_opts.app_label model_opts.model_name as pagination_base_url %}
{% include "wagtailadmin/shared/pagination_nav.html" with items=items linkurl=pagination_base_url %}

View File

@ -8,12 +8,31 @@ from django.views.generic.base import View
from wagtail.admin.forms.search import SearchForm
from wagtail.admin.modal_workflow import render_modal_workflow
from wagtail.admin.ui.tables import Table, TitleColumn
from wagtail.core.models import Locale, TranslatableMixin
from wagtail.search.backends import get_search_backend
from wagtail.search.index import class_is_indexed
from wagtail.snippets.views.snippets import get_snippet_model_from_url_params
class ResultsTable(Table):
header_row_classname = 'table-headers'
class SnippetTitleColumn(TitleColumn):
def __init__(self, name, model, **kwargs):
self.model_opts = model._meta
super().__init__(name, **kwargs)
def get_value(self, instance):
return str(instance)
def get_link_url(self, instance, parent_context):
return reverse(
'wagtailsnippets:chosen', args=[self.model_opts.app_label, self.model_opts.model_name, quote(instance.pk)]
)
class BaseChooseView(View):
def get(self, request, app_label, model_name):
self.model = get_snippet_model_from_url_params(app_label, model_name)
@ -68,6 +87,10 @@ class BaseChooseView(View):
paginator = Paginator(items, per_page=25)
self.paginated_items = paginator.get_page(request.GET.get('p'))
self.table = ResultsTable([
SnippetTitleColumn('title', self.model, label=_('Title'), link_classname='snippet-choice'),
], self.paginated_items)
return self.render_to_response()
def render_to_response(self):
@ -83,6 +106,7 @@ class ChooseView(BaseChooseView):
{
'model_opts': self.model._meta,
'items': self.paginated_items,
'table': self.table,
'is_searchable': self.is_searchable,
'search_form': self.search_form,
'query_string': self.search_query,
@ -101,6 +125,7 @@ class ChooseResultsView(BaseChooseView):
return TemplateResponse(self.request, "wagtailsnippets/chooser/results.html", {
'model_opts': self.model._meta,
'items': self.paginated_items,
'table': self.table,
'query_string': self.search_query,
'is_searching': self.is_searching,
})