Skip to content

Fix dependent fields in a formset #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
- Find the closest common ancestor of two chained selects instead of …
…assuming it is a form.

- Try to find the dependent field by its name attribute using a two-fold search strategy: exact match otherwise ends with
  • Loading branch information
mardukbp committed Feb 19, 2025
commit 3ca4238d367a5a7075a5fb2fbbba20ab4e375308
9 changes: 8 additions & 1 deletion django_select2/static/django_select2/django_select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@

let dependentFields = $element.data('select2-dependent-fields')
if (dependentFields) {
const find_element = function(selector) {
const result = $(selector, $element.closest(':has(' + selector + ')'))
if (result.length > 0) return result
else return null
}
dependentFields = dependentFields.trim().split(/\s+/)
$.each(dependentFields, function (i, dependentField) {
result[dependentField] = $('[name=' + dependentField + ']', $element.closest('form')).val()
const name_is = '[name=' + dependentField + ']'
const name_ends_with = '[name$=' + dependentField + ']'
result[dependentField] = (find_element(name_is) || find_element(name_ends_with)).val()
})
}

Expand Down
Loading