Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

dgm/jquery_autocomplete_helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JqueryAutocompleteHelper
==================

This helper uses jquery's autocomplete to look up a value and insert it into a hidden form field.  
Useful for linking a join model to another model when there are too many records for a select field.


Example
=======

This plugin assumes:

The model being searched has a method called display_name:

class Person < ActiveRecord::Base
 def display_name
   first_name + " " + last_name
 end
end

view:

<%= form_for @organization do |f| %>
  <p>
    <%= f.label :person_id %><br />
   <%= f.autocomplete_field(:person_id. :url => people_path) %>
  </p>
....
<%= yield :runview %>


The controller for person needs to search based on params[:q] :

def index
  @people = Person.find(:all, :conditions => ["first_name LIKE ? or last_name LIKE ?", '%'+q + '%', '%'+q + '%' ], :limit => 20)

  respond_to do |format|
    format.html  { ... }
    format.xml  { ... }
    format.js { render :text => @people.map { |c| "#{c.display_name}|#{c.id}\n" } }
  end
end

Formtastic forms are also supported:

<%= semantic_form_for @organization do |f| %>
<% f.inputs do %>
   <%= f.input :person_id, :as => :autocomplete, :url => people_path %>
<% end %>
....
<%= yield :runview %>

Override the display value with :display_value if the relation name doesn't match the id name:
<%= f.input :parent_id, :as => :autocomplete, :url => people_path, :display_value => @person.household_parent.display_name %>

Add a javascript callback to add new models vi :new_callback

<script type="text/javascript">
  function open_person_form() {
  ...   
}
</script>


 <%= f.input :person_id, :as => :autocomplete, :url => people_path, :new_callback => "open_person_form();" %>




Copyright (c) 2009 David Morton <dgm@dgrmm.net>, released under the MIT license

About

A helper class to look up items and update a hidden id number

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages