Skip to content

Commit 70a86ec

Browse files
authored
feat: ajax paginated inlines (#1440)
1 parent 570cea1 commit 70a86ec

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

src/unfold/static/admin/js/inlines.js

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
};
311311

312312
// Tabular inlines ---------------------------------------------------------
313-
$.fn.tabularFormset = function (selector, options) {
313+
$.fn.tabularFormset = function (selector, options, callback = null) {
314314
const $rows = $(this);
315315

316316
const reinitDateTimeShortCuts = function () {
@@ -370,11 +370,15 @@
370370
addButton: options.addButton,
371371
});
372372

373+
if (typeof callback === "function") {
374+
callback();
375+
}
376+
373377
return $rows;
374378
};
375379

376380
// Stacked inlines ---------------------------------------------------------
377-
$.fn.stackedFormset = function (selector, options) {
381+
$.fn.stackedFormset = function (selector, options, callback = null) {
378382
const $rows = $(this);
379383
const updateInlineLabel = function (row) {
380384
$(selector)
@@ -449,26 +453,47 @@
449453
addButton: options.addButton,
450454
});
451455

456+
if (typeof callback === "function") {
457+
callback();
458+
}
459+
452460
return $rows;
453461
};
454462

463+
$(window).on("htmx:afterSettle", function (event) {
464+
if (event.target.classList.contains("js-inline-admin-formset")) {
465+
initInlines($(event.target), function () {
466+
if (typeof DateTimeShortcuts !== "undefined") {
467+
$(".datetimeshortcuts").remove();
468+
DateTimeShortcuts.init();
469+
}
470+
471+
$(event.target).find(".admin-autocomplete").djangoAdminSelect2();
472+
});
473+
}
474+
});
475+
455476
$(document).ready(function () {
456477
$(".js-inline-admin-formset").each(function () {
457-
const data = $(this).data(),
458-
inlineOptions = data.inlineFormset;
459-
let selector;
460-
switch (data.inlineType) {
461-
case "stacked":
462-
selector = inlineOptions.name + "-group .inline-related";
463-
$(selector).stackedFormset(selector, inlineOptions.options);
464-
break;
465-
case "tabular":
466-
selector =
467-
inlineOptions.name +
468-
"-group .tabular.inline-related tbody:last > tr.form-row";
469-
$(selector).tabularFormset(selector, inlineOptions.options);
470-
break;
471-
}
478+
initInlines(this);
472479
});
473480
});
481+
482+
function initInlines(el, callback = null) {
483+
const data = $(el).data(),
484+
inlineOptions = data.inlineFormset;
485+
let selector;
486+
switch (data.inlineType) {
487+
case "stacked":
488+
selector = inlineOptions.name + "-group .inline-related";
489+
$(selector).stackedFormset(selector, inlineOptions.options, callback);
490+
break;
491+
case "tabular":
492+
selector =
493+
inlineOptions.name +
494+
"-group .tabular.inline-related tbody:last > tr.form-row";
495+
$(selector).tabularFormset(selector, inlineOptions.options, callback);
496+
break;
497+
}
498+
}
474499
}

src/unfold/templates/admin/change_form.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@
104104

105105
{% block inline_field_sets %}
106106
{% for inline_admin_formset in inline_admin_formsets %}
107-
{% include inline_admin_formset.opts.template %}
107+
{% if request.headers.hx_request %}
108+
{% if inline_admin_formset.formset.get_pagination_key in request.GET %}
109+
{% include inline_admin_formset.opts.template %}
110+
{% endif %}
111+
{% else %}
112+
{% include inline_admin_formset.opts.template %}
113+
{% endif %}
108114
{% endfor %}
109115
{% endblock %}
110116

src/unfold/templates/admin/includes/fieldset.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h2 class="border-b border-base-200 border-t border-dashed font-semibold text-fo
99
{{ fieldset.name }}
1010
</h2>
1111
{% else %}
12-
<h2 class="bg-base-100 font-semibold mb-6 px-4 py-3 rounded-default text-font-important-light text-sm 2xl:-mx-4 dark:bg-white/[.02] dark:text-font-important-dark {% if fieldset.is_collapsible %}cursor-pointer{% endif %}">
12+
<h2 class="bg-base-100 font-semibold mb-6 px-4 py-3 rounded-default text-font-important-light text-sm @min-[1570px]:-mx-4 dark:bg-white/[.02] dark:text-font-important-dark {% if fieldset.is_collapsible %}cursor-pointer{% endif %}">
1313
{{ fieldset.name }}
1414
</h2>
1515
{% endif %}

src/unfold/templates/unfold/helpers/pagination_inline.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
<input type="hidden" name="{{ pagination_key }}" value="{{ page_obj.number }}" />
88

99
{% if page_obj.paginator.count and page_obj.paginator.num_pages > 1 %}
10-
<div class="flex items-center gap-2 mt-6 mb-6">
10+
<div class="flex items-center gap-2 mt-6">
1111
{% for i in elided_page_range %}
1212
{% if i == page_obj.paginator.ELLIPSIS %}
1313
<span>{{ page_obj.paginator.ELLIPSIS }}</span>
1414
{% elif i == page_obj.number %}
1515
<span class="font-medium text-primary-600">{{ i }}</span>
1616
{% else %}
17-
<a href="?{% querystring_params pagination_key i %}{% if has_tab %}#{{ inline_admin_formset.formset.prefix|slugify }}{% endif %}">
17+
<a class="cursor-pointer"
18+
hx-get="?{% querystring_params pagination_key i %}{% if has_tab %}#{{ inline_admin_formset.formset.prefix|slugify }}{% endif %}"
19+
hx-push-url="true"
20+
hx-swap="outerHTML"
21+
hx-select="#{{ inline_admin_formset.formset.prefix }}-group"
22+
hx-target="#{{ inline_admin_formset.formset.prefix }}-group">
1823
{{ i }}
1924
</a>
2025
{% endif %}

0 commit comments

Comments
 (0)