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

autocompletion plugin should change the input field value on navigation through the result list via key down/up. #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 0 additions & 4 deletions integration/public/javascripts/autocomplete-rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
Expand Down
21 changes: 15 additions & 6 deletions lib/rails3-jquery-autocomplete/autocomplete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def autocomplete(object, method, options = {})
items = {}
end

render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data])
render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data], options[:distinct])
end
end
end
Expand All @@ -73,24 +73,33 @@ def get_autocomplete_limit(options)
# # returns a Actor constant supposing it is already defined
#
def get_object(model_sym)
object = model_sym.to_s.camelize.constantize
model_sym.to_s.camelize.constantize
end

#
# Returns a hash with three keys actually used by the Autocomplete jQuery-ui
# Can be overriden to show whatever you like
# Hash also includes a key/value pair for each method in extra_data
#
def json_for_autocomplete(items, method, extra_data=[])
items.collect do |item|
hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
def json_for_autocomplete(items, method, extra_data=[], distinct = false)
arr = []
items.each do |item|
hash = {}
hash['id'] = item.id.to_s unless distinct
hash['value'] = item.send(method)
hash['label'] = hash['value']

extra_data.each do |datum|
hash[datum] = item.send(datum)
end if extra_data
# TODO: Come back to remove this if clause when test suite is better
hash

next if distinct and arr.include?(hash)
arr << hash
end
arr
end

end
end