auto_complete
Posted by flyerhzm on February 26th, 2008 filed in PluginRails插件形式的Ajax.AutoCompleter实现
安装:
script/plugin install http://svn.rubyonrails.org/rails/plugins/auto_complete/
使用:
1 2 3 4 | # Controller class BlogController < ApplicationController auto_complete_for :tag, :name end |
1 | # View<%= text_field_with_auto_complete :tag, name %> |
参数介绍:
1 2 3 4 | # Controller中可带参数有:conditions, :limit, :order class BlogController < ApplicationController auto_complete_for :tag, :name, :limit => 15, :order => 'created_at DESC' end |
# View中可带参数有两种:一是tag_options,与text_field的options相同;
# 二是completion_options,与prototype库的Ajax.AutoCompleter的options相同
<%= text_field_with_auto_complete :tag, :name, {:size => 10}, {:tokens => ' '} %>
扩展:
auto_complete在输入框为空时,不会做任何事,如果你希望输入框为空时,也会去查询相应的数据完成complete的话,就需要修改一下源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # auto_complete.rbdef auto_complete_for(object, method, options = {})
define_method("auto_complete_for_#{object}_#{method}") do
if params[object].nil?
find_options = {
:order => "#{method} ASC",
:limit => 10 }.merge!(options)
else
find_options = {
:conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method] + '%' ],
:order =>"#{method} ASC",
:limit => 10 }.merge!(options)
end
@items = object.to_s.camelize.constantize.find(:all, find_options)
render :inline => "<%= auto_complete_result @items, '#{method}' %>"
end
end |
# View<%= text_field_with_auto_complete :tag, :name, {:onfocus => "tag_name_auto_completer.activate()"}, { :tokens => ' ' } %>
效果图:




Related posts:
- in_place_editor for rails2.0.2 in_place_editor使用: script/plugin install in_place_editing # Controller class BlogController < ApplicationController...
- in_place_editing Rails 2中in_place_editing被抽出成为一个单独的插件;而prototype库则升级为1.6.0.1。一些由以前版本升级至2.0版本的Rails应用的in place editing功能,若使用了:script => true选项就会出错,表现为编辑完成后,编辑框内文字显示为一段Javascript代码而非输入的内容。如下面的用法: 造成这个错误的原因,是Prototype1.6中去除了evalScripts选项,而这个选项即是由in_place_editor_filed中的:script来指定的。Prototype中加入了一个新的选项htmlResponse,其含义与evalScripts恰好相反。 找到了问题的根源,解决方法便有了,修改vender/plugins/in_place_editing/lib/in_place_macros_helper.rb,将给evalScripts赋值的一行改掉便可: #js_options[’evalScripts’] =...
Related posts brought to you by Yet Another Related Posts Plugin.
September 12th, 2010 at 12:04 AM
Buy:Petcam (Metacam) Oral Suspension.Actos.Nexium.Mega Hoodia.Prednisolone.Zyban.100% Pure Okinawan Coral Calcium.Accutane.Retin-A.Valtrex.Prevacid.Zovirax.Lumigan.Arimidex.Synthroid.Human Growth Hormone….