Skip to content

Commit

Permalink
Merge pull request #3 from erickzanardo/master
Browse files Browse the repository at this point in the history
Fixing item select after empty result
  • Loading branch information
michaelperrin authored Jun 14, 2016
2 parents 79271af + 2c7d4c4 commit 16184c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
18 changes: 9 additions & 9 deletions lib/ajax-chosen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/ajax-chosen.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions src/ajax-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ do ($ = jQuery) ->
# Exit if the data we're given is invalid
return unless data?

# Send the ajax results to the user callback so we can get an object of
# value => text pairs to inject as <option> elements.
items = if @callback_function? then @callback_function(data, @search_field) else data
unless items.length
# If there are no results, display the no_results text
@element.data().chosen.no_results_clear()
@element.data().chosen.no_results @search_field.val()
return

# Go through all of the <option> elements in the <select> and remove
# ones that have not been selected by the user. For those selected
# by the user, add them to a list to filter from the results later.
Expand All @@ -112,10 +121,6 @@ do ($ = jQuery) ->
@element.find('optgroup:empty').each ->
$(@).remove()

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

nbItems = 0

_this = @
Expand Down Expand Up @@ -156,20 +161,15 @@ do ($ = jQuery) ->
.html(text)
.appendTo(_this.element)

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

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

0 comments on commit 16184c2

Please sign in to comment.