Skip to content
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

Fix #136 -- Add dependent-field support to formsets #231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
11 changes: 9 additions & 2 deletions django_select2/static/django_select2/django_select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@
page: params.page,
field_id: $element.data('field_id')
}

var dependentFields = $element.data('select2-dependent-fields')
if (dependentFields) {
dependentFields = dependentFields.trim().split(/\s+/)
$.each(dependentFields, function (i, dependentField) {
result[dependentField] = $('[name=' + dependentField + ']', $element.closest('form')).val()
var formValue = $('[name=' + dependentField + ']', $element.closest('form')).val();
// This is for inlines, I checked this for a specific case
if (formValue === null || formValue === undefined) {
var newFieldName = $element[0].name.split('-', 2).join('-') + '-' + dependentField;
var formsetValue = $('[name=' + newFieldName + ']', $element.closest('.form-row')).val();
result[dependentField] = formsetValue;
} else {
result[dependentField] = formValue;
}
Comment on lines +30 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can simply this a little and drop the else branch, like so:

Suggested change
var formValue = $('[name=' + dependentField + ']', $element.closest('form')).val();
// This is for inlines, I checked this for a specific case
if (formValue === null || formValue === undefined) {
var newFieldName = $element[0].name.split('-', 2).join('-') + '-' + dependentField;
var formsetValue = $('[name=' + newFieldName + ']', $element.closest('.form-row')).val();
result[dependentField] = formsetValue;
} else {
result[dependentField] = formValue;
}
var formValue = $('[name=' + dependentField + ']', $element.closest('form')).val();
// This is for inlines, I checked this for a specific case
if (formValue === null || formValue === undefined) {
var newFieldName = $element[0].name.split('-', 2).join('-') + '-' + dependentField;
formValue = $('[name=' + newFieldName + ']', $element.closest('.form-row')).val();
}
result[dependentField] = formValue;

})
}

Expand Down