开发者

Rails + jQuery Sticky Button

开发者 https://www.devze.com 2023-02-18 21:28 出处:网络
I\'m looking for suggestions for how to create a menu button that can be \"sticky\", ie: if the user presses it once it stays pressed for the entire visit to the site, even if they browse through seve

I'm looking for suggestions for how to create a menu button that can be "sticky", ie: if the user presses it once it stays pressed for the entire visit to the site, even if they browse through several different pages and come back.

For instance, let's say I have a search bar like this:

---------------------------------------------------
Keyword Search: _______________  [Advanced Options]
---------------------------------------------------

When the user clicks "Advanced Options" they get more buttons, and the "Advanced Options" button changes to a 'selected' appearance, like thi开发者_开发问答s:

---------------------------------------------------
Keyword Search: _______________  [*Advanced Options*]
Filter by date: ______ to _____
---------------------------------------------------

Now I already have this functionality working fine, it's roughly the following html:

<div class="menu">
  ... menu buttons ...
  <div class="advanced_options">...</div>
</div>

... where my css contains div.advanced_options { display: none; } and I am using jQuery slideToggle() to show and hide the advanced options.

The problem is, if a user clicks a link in the submenu they are, of course, redirected, and the button is stateless so the submenu is now hidden again and the user needs to show it if they want to click something ELSE in the menu.

So, I'm not really sure what would be a good approach to solving this kind of problem. When have you needed to simulate state transferring from one request to another in a rails app like this, and how have you solved it? How would you make this button stay "pressed" from one request to the next?

Thanks!


About 90% of the work I've had to do regarding "state" in a webapp is an in-site search. In this case, I amend all relevant generated URLs to include query parameters associated with the search, something like this:

 # search_parameters is a hash in the form of
 # { :q => 'some search text', :order => 'some search order', 
 #   :criteria => 'some additional criteria' }
 random_resource_link(@random_resource, search_parameters)

In this case, you may be able to get away, for very small states, using the session.

<% if session[:button_enabled] %>
  <!-- HTML to render if the button is enabled -->
<% else %>
  <!-- HTML to render if the button is not enabled -->
<% end %>

Set session[:button_enabled] using an AJAX call.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号