Created
September 15, 2010 23:57
-
-
Save albertoperdomo/581715 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet by @ayosec on IM - Thx ayose! :) | |
//:ruby | |
//= form.input :city_id, :as => :string, :input_html => { :value => resource.city_id } | |
//:javascript | |
jQuery(function() { city_autocompleter("#resource_city_id", "#{escape_javascript resource.localized_city_long_name || params[:city]}") }); | |
function city_autocompleter (city_id, city_label) { | |
city_id = $(city_id); | |
var row = city_id.parent("li"); | |
// Oculta el campo que contiene el ID | |
city_id.hide(); | |
// Crea el span que muestra el nombre de la ciudad | |
var span_label = $("<span>"). | |
text(city_label). | |
click(function () { | |
var input = $("<input>"). | |
autocomplete("...."). | |
select(function(event, item) { | |
// El usuario seleciona un elemento del autocomplete | |
// Se elimina el <input> y se actualiza el span | |
city_id.val(item.id); // actualiza el ID | |
span.text(item.texto).show(); // Actualiza el span con lo que había en la etiqueta del autocomplete y lo muestra | |
input.remove(); // quita el input generado | |
}).appendTo(row); // añade el input al <li> | |
}). | |
appendTo(row). // añade el <span> al <li> | |
focus(). // deja el cursor en el autocomplete | |
blur(function() { | |
// si sale del autocomplete quita el campo | |
span.show(); | |
input.remove(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment