Marcelo Júnior
811 posts
|
Problema: Eu queria fazer uma lista, que quando eu excluísse uma linha desta lista, não recarregasse a pagina inteira, mas que apenas fosse removida a linha do item excluído.
Para isto, criei um helper, baseado no código do remote_function, onde eu posso executar uma action, sem recarregar uma pagina, e aproveitando o embalo, agreguei algumas funções de RJS.
Basta colar isto no seu application_helper:
def ajax_function(options)
javascript_options = options_for_ajax(options)
update = ‘’
if options[:update] && options[:update].is_a?(Hash)
update = []
update << "success:’#{options[:update][:success]}’" if options[:update][:success]
update << “failure:‘#{options[:update][:failure]}’” if options[:update][:failure]
update = ‘{’ + update.join(‘,’) + ‘}’
elsif options[:update]
update << “‘#{options[:update]}’”
end
function = update.empty? ?
“new Ajax.Request(” :
"new Ajax.Updater(#{update}, "
url_options = options[:url]
url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash)
function << “‘#{url_for(url_options)}’”
function << “, #{javascript_options})”
#Foi aqui que eu alterei…ehehe
function << “;”render(:update){|page| page.visual_effect :puff, options[:remove]} if options[:remove]
function << “;”render(:update){|page| page.insert_html options[:insert_html][:position],options[:insert_html][:id],options[:insert_html][:options]; page.visual_effect :appear, options[:insert_html][:id]} if options[:insert_html]
function << “;”+render(:update){|page| page.replace_html options[:replace_html][:id],options[:replace_html][:options]; page.visual_effect :appear, options[:replace_html][:id]} if options[:replace_html]
function = “#{options[:before]}; #{function}” if options[:before]
function = “#{function}; #{options[:after]}” if options[:after]
function = “if (#{options[:condition]}) { #{function}; }” if options[:condition]
function = “if (confirm(‘#{escape_javascript(options[:confirm])}’)) { #{function}; }” if options[:confirm]
return function
end
Usando:
Vamo usar o exemplo da lista. Eu tenho uma tag table, onde todas as linhas (tr) tem um id:
|