Conditional toggle button

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)

Use the prototype function Element.toggle for toggeling an element on
and
off.

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

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.

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. 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

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.

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?