Skip to content

Commit 3a19fa5

Browse files
predatellpredatell
andauthored
Clear dependent fields on select (applegrew#597)
Co-authored-by: predatell <[email protected]>
1 parent b17b638 commit 3a19fa5

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

django_select2/static/django_select2/django_select2.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
} else {
5858
init($element, settings)
5959
}
60+
$element.on('select2:select', function (e) {
61+
var name = $(e.currentTarget).attr('name')
62+
$('[data-select2-dependent-fields=' + name + ']').each(function () {
63+
$(this).val('').trigger('change')
64+
})
65+
})
6066
})
6167
return this
6268
}

tests/test_forms.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def test_widgets_selected_after_validation_error(self, db, live_server, driver,
479479
WebDriverWait(driver, 60).until(
480480
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.select2-selection--single'))
481481
)
482-
country_container, city_container = driver.find_elements_by_css_selector('.select2-selection--single')
482+
country_container, city_container, city2_container = driver.find_elements_by_css_selector('.select2-selection--single')
483483

484484
# clicking city select2 lists all available cities
485485
city_container.click()
@@ -529,3 +529,44 @@ def test_widgets_selected_after_validation_error(self, db, live_server, driver,
529529
country_names_from_db = {City.objects.get(name=city_name).country.name}
530530
assert len(country_names_from_browser) != Country.objects.count()
531531
assert country_names_from_browser == country_names_from_db
532+
533+
def test_dependent_fields_clear_after_change_parent(self, db, live_server, driver, countries, cities):
534+
driver.get(live_server + self.url)
535+
country_container, city_container, city2_container = driver.find_elements_by_css_selector('.select2-selection--single')
536+
537+
# selecting a country really does it
538+
country_container.click()
539+
WebDriverWait(driver, 60).until(
540+
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.select2-results li:nth-child(2)'))
541+
)
542+
country_option = driver.find_element_by_css_selector('.select2-results li:nth-child(2)')
543+
country_name = country_option.text
544+
country_option.click()
545+
assert country_name == country_container.text
546+
547+
# selecting a city2
548+
city2_container.click()
549+
WebDriverWait(driver, 60).until(
550+
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.select2-results li'))
551+
)
552+
city2_option = driver.find_element_by_css_selector('.select2-results li:nth-child(2)')
553+
city2_name = city2_option.text
554+
city2_option.click()
555+
assert city2_name == city2_container.text
556+
557+
# change a country
558+
country_container.click()
559+
WebDriverWait(driver, 60).until(
560+
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.select2-results li:nth-child(3)'))
561+
)
562+
country_option = driver.find_element_by_css_selector('.select2-results li:nth-child(3)')
563+
country_name = country_option.text
564+
country_option.click()
565+
assert country_name == country_container.text
566+
567+
# check the value in city2
568+
city2_container.click()
569+
WebDriverWait(driver, 60).until(
570+
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, '.select2-results li'))
571+
)
572+
assert city2_container.text == ""

tests/testapp/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ class AddressChainedSelect2WidgetForm(forms.Form):
204204
)
205205
)
206206

207+
city2 = forms.ModelChoiceField(
208+
queryset=City.objects.all(),
209+
label='City not Interdependent',
210+
widget=ModelSelect2Widget(
211+
search_fields=['name__icontains'],
212+
dependent_fields={'country': 'country'},
213+
max_results=500,
214+
attrs={'data-minimum-input-length': 0},
215+
)
216+
)
217+
207218

208219
class GroupieForm(forms.ModelForm):
209220
class Meta:

0 commit comments

Comments
 (0)