Forum: Ruby on Rails Conditional toggle button

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex N. (Guest)
on 2006-04-05 04:30
I have a link_to_remote that updates the content of a hidden div and
makes it appear.. But I want it to hide the div and not execute the
link_to_remote function on the second click.  What's the best way to do
this?  I'm thinking I can do it in RJS maybe, but I'm not sure if
there's a nice Rails-y way to tell if a div is hidden or not..

I'd also need to conditionally do the :loading and :success effects
(show/hide spinner)
Daniel -. (Guest)
on 2006-04-05 04:45
(Received via mailing list)
Use the prototype function Element.toggle for toggeling an element on
and
off.
Alex N. (Guest)
on 2006-04-05 04:46
Daniel ----- wrote:
> Use the prototype function Element.toggle for toggeling an element on
> and
> off.

Yes I know about that but you didn't answer the actual question:

I want to have different behavior when it's being shown than when it's
being hidden, which I cannot accomplish by just using Element.toggle
Daniel -. (Guest)
on 2006-04-05 05:24
(Received via mailing list)
I think with a link_to_remote, it will call the action regardless.  I
think
you would need to handle the login in the controller or RJS template.
Alex N. (Guest)
on 2006-04-05 05:31
Daniel ----- wrote:
> I think with a link_to_remote, it will call the action regardless.  I
> think
> you would need to handle the login in the controller or RJS template.

What I'm doing for the time being is in an RJS file:

page << "if(Element.getStyle('show"+params[:id]+"', 'display') ==
'none') {"
page.replace_html 'show'+params[:id], :partial => 'short'
page.visual_effect( :blind_down, 'show'+params[:id] )
page << "} else {"
page.visual_effect( :blind_up, 'show'+params[:id] )
page << "}"

This is a pretty cheap hack as far as I'm concerned and I'd appreciate
if someone could point me towards a cleaner solution.

It also doesn't stop the spinner from firing even on hide, which is
annoying.  I could in theory do the same if(Element.getStyle()) inside
the :loading => '' block.. but.. gross..
Fredrik T. (Guest)
on 2006-08-27 09:02
Alex Nobert wrote:
> I have a link_to_remote that updates the content of a hidden div and
> makes it appear.. But I want it to hide the div and not execute the
> link_to_remote function on the second click.  What's the best way to do
> this?  I'm thinking I can do it in RJS maybe, but I'm not sure if
> there's a nice Rails-y way to tell if a div is hidden or not..
>
> I'd also need to conditionally do the :loading and :success effects
> (show/hide spinner)

Anyone ever come up with a good solution for this. I assume this is very
common for users toggling detail information for list where they want to
load the data on the first click, but just remove it on the second and
so on.

Anyone?
Chris M. (Guest)
on 2006-08-27 13:51
(Received via mailing list)
Fredrik T. wrote:
> Anyone ever come up with a good solution for this. I assume this is very
> common for users toggling detail information for list where they want to
> load the data on the first click, but just remove it on the second and
> so on.

I just use two links that do two different things: the first link loads
and displays the new content, and the second link hides the content.
But you only display one link at any one time, so if they're in the
same place it just looks like one link that's changing from "Show" to
"Hide" or whatever.

So when you click on the "Show" link, as well as loading and displaying
the new content, it hides itself and displays the second link. And when
you click on the "Hide" link that's just been revealed, as well as
hiding the new content, it also hides itself and re-displays the first
link.

The way I look at it is that you've got two fundamentally different
actions going on, so you might as well use two different links.

Chris
Jeremy W. (Guest)
on 2006-08-27 17:42
(Received via mailing list)
I'd do it like so:

<%=link_to_function "Link", "if(Element.visible('divid')) {
            Element.hide('divid')
        } else {
            " + remote_function(:url=>{:action=>"blah"},
:loading=>whatever) + "}" %>

and you could write a helper function that would take the arguments
name, element id and options and output the above too.
This topic is locked and can not be replied to.