Skip to content

Commit

Permalink
Fix #297 -- Add Django admin' inline formset support (#300)
Browse files Browse the repository at this point in the history
As described in #249 and #297 django-select2 does not currently work
with selects added dynamically in the Django admin.

The solution was implemented in #249 and all credit should go to Jurrian
Tromp. I only added a small correction:

```diff
- jqRow.find('.select2-container').remove()
+ jqRow.find('.django-select2').parent().find('.select2-container').remove()
```

to only remove `.select2-container` when it is a sibling of
`.django-select2`. Otherwise the wrong `.select2-container` might get
deleted.

---------

Co-authored-by: Johannes Maron <[email protected]>
  • Loading branch information
mardukbp and codingjoe authored Dec 10, 2024
1 parent 12e2d60 commit 352811b
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django_select2/static/django_select2/django_select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@
}

$(function () {
$('.django-select2').djangoSelect2()
$('.django-select2').not('[name*=__prefix__]').djangoSelect2()

document.addEventListener('formset:added', (event) => {
$(event.target).find('.django-select2').djangoSelect2()
})
})

return $.fn.djangoSelect2
Expand Down

0 comments on commit 352811b

Please sign in to comment.