Skip to content

Commit 16184c2

Browse files
Merge pull request #3 from erickzanardo/master
Fixing item select after empty result
2 parents 79271af + 2c7d4c4 commit 16184c2

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

lib/ajax-chosen.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ajax-chosen.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ajax-chosen.coffee

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ do ($ = jQuery) ->
100100
# Exit if the data we're given is invalid
101101
return unless data?
102102

103+
# Send the ajax results to the user callback so we can get an object of
104+
# value => text pairs to inject as <option> elements.
105+
items = if @callback_function? then @callback_function(data, @search_field) else data
106+
unless items.length
107+
# If there are no results, display the no_results text
108+
@element.data().chosen.no_results_clear()
109+
@element.data().chosen.no_results @search_field.val()
110+
return
111+
103112
# Go through all of the <option> elements in the <select> and remove
104113
# ones that have not been selected by the user. For those selected
105114
# by the user, add them to a list to filter from the results later.
@@ -112,10 +121,6 @@ do ($ = jQuery) ->
112121
@element.find('optgroup:empty').each ->
113122
$(@).remove()
114123

115-
# Send the ajax results to the user callback so we can get an object of
116-
# value => text pairs to inject as <option> elements.
117-
items = if @callback_function? then @callback_function(data, @search_field) else data
118-
119124
nbItems = 0
120125

121126
_this = @
@@ -156,20 +161,15 @@ do ($ = jQuery) ->
156161
.html(text)
157162
.appendTo(_this.element)
158163

159-
if nbItems
160-
# Tell chosen that the contents of the <select> input have been updated
161-
# This makes chosen update its internal list of the input data.
162-
val_before_trigger = @search_field.val()
163-
@element.trigger("chosen:updated")
164-
# For some reason, the contents of the input field get removed once you
165-
# call trigger above. Often, this can be very annoying (and can make some
166-
# searches impossible), so we add the value the user was typing back into
167-
# the input field.
168-
@search_field.val(val_before_trigger)
169-
else
170-
# If there are no results, display the no_results text
171-
@element.data().chosen.no_results_clear()
172-
@element.data().chosen.no_results @search_field.val()
164+
# Tell chosen that the contents of the <select> input have been updated
165+
# This makes chosen update its internal list of the input data.
166+
val_before_trigger = @search_field.val()
167+
@element.trigger("chosen:updated")
168+
# For some reason, the contents of the input field get removed once you
169+
# call trigger above. Often, this can be very annoying (and can make some
170+
# searches impossible), so we add the value the user was typing back into
171+
# the input field.
172+
@search_field.val(val_before_trigger)
173173

174174
# Finally, call the user supplied callback (if it exists)
175175
@success(data) if @success?

0 commit comments

Comments
 (0)