Adam Bloom
2006-Mar-31 02:44 UTC
[Rails] Checkboxes expand to show new options when checked?
I''m writing a simple CMS where every item ("listing") HABTM categories and subcategories (which themselves belong_to a category). What I''m trying to do now is create a UI effect for listing creation: when you go to create a listing, only the possible categories will be shown, but when you select a category a set of new categories will be displayed like so: Lions Tigers Bears> click on TigersLions Tigers Feeding Breeding Storing Bears> de-select Tigers and it returns to normal.What I have right now is a display where every category and subcategory is displayed at once. The code looks something like this: <% for category in @categories %> <div id="categories"> <input type="checkbox" /NAME, ID, VALUE ARE HERE> <%= category.name %> </div> <% for subcategory in category.subcategories %> <div id="subcategories"> <input type="checkbox" /DITTO> <%= subcategory.name %> </div <% end %> <% end %> Is there some kind of conditional I can put before the second "for" statement? An is_checked? sort of thing? Or maybe AJAX using Element.toggle? I don''t know where to start - and a web search didn''t do much good. Thanks, Adam -- Posted via http://www.ruby-forum.com/.
Adam Bloom
2006-Mar-31 18:26 UTC
[Rails] Re: Checkboxes expand to show new options when checked?
Still curious about this... -Adam -- Posted via http://www.ruby-forum.com/.
Nicolas Buet
2006-Mar-31 19:06 UTC
[Rails] Re: Checkboxes expand to show new options when checked?
you have the javascript option, or the ajax option. In javascript, it is quite straightforward: a click on something toggle the visibility of a div. It works like that: <% for category in @categories %> <div id="categories"> <a href="#" onclick="Element.toggle(''subcategories_<%category.name%>'');"><%category.name %></a> </div> <% for subcategory in category.subcategories %> <div id="subcategories_<%= category.name %>" style="display:none"> <input type="checkbox" /DITTO> <%= subcategory.name %> </div> <% end %> <% end %> IMHO, ajax would be useful here only if subcategory is big. Otherwise, this method work fine. On 3/31/06, Adam Bloom <admanb@gmail.com> wrote:> > Still curious about this... > > -Adam > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060331/82756fa7/attachment.html
Adam Bloom
2006-Mar-31 21:57 UTC
[Rails] Re: Re: Checkboxes expand to show new options when checked?
> IMHO, ajax would be useful here only if subcategory is big. Otherwise, > this > method work fine.That works great! Thanks a lot, -Adam -- Posted via http://www.ruby-forum.com/.
Adam Bloom
2006-Mar-31 22:27 UTC
[Rails] Re: Re: Checkboxes expand to show new options when checked?
I''m having one problem. In my code, inside the <input ... > tag I have: <% if @listing.categories.include? category %>checked="checked"<% end %> because the same form that''s used to create is used to edit. Obviously this results in the categories being checked, but the subcategories aren''t toggled on. I tried this inside the <div id="categories">: <% if @listing.categories.include? category %> <script language="javascript" type="text/javascript">Element.toggle(''subcategories_<%=category.name%>'');</script> <% end %> but it didn''t togggle... Sorry, I''m still a newbie at all of this. Thanks again, Adam -- Posted via http://www.ruby-forum.com/.
Nicolas Buet
2006-Apr-01 08:48 UTC
[Rails] Re: Re: Checkboxes expand to show new options when checked?
Your problem is that you would like a category checked to be shown, right? then the code <div id="subcategories_<%= category.name %>" style="display:none"> Should be replaced by <div id="subcategories_<%= category.name %>" <%= ''style="display:none"'' if @listing.categories.include? category %> > Regards On 4/1/06, Adam Bloom <admanb@gmail.com> wrote:> > I''m having one problem. In my code, inside the <input ... > tag I have: > > <% if @listing.categories.include? category %>checked="checked"<% end %> > > because the same form that''s used to create is used to edit. Obviously > this results in the categories being checked, but the subcategories > aren''t toggled on. I tried this inside the <div id="categories">: > > <% if @listing.categories.include? category %> > <script language="javascript" > type="text/javascript">Element.toggle(''subcategories_<%=category.name% > >'');</script> > <% end %> > > but it didn''t togggle... > > Sorry, I''m still a newbie at all of this. > > Thanks again, > > Adam > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/f772af6a/attachment.html
Nicolas Buet
2006-Apr-01 08:50 UTC
[Rails] Re: Re: Checkboxes expand to show new options when checked?
Ops, I meant: <div id="subcategories_<%= category.name %>" <%= ''style="display:none"'' if not @ listing.categories.include? category %> > On 4/1/06, Nicolas Buet <nicolas.buet@gmail.com> wrote:> > Your problem is that you would like a category checked to be shown, > right? then the code > <div id="subcategories_<%= category.name %>" style="display:none"> > Should be replaced by > <div id="subcategories_<%= category.name %>" <%= ''style="display:none"'' > if @ listing.categories.include? category %> > > > Regards > > > > On 4/1/06, Adam Bloom <admanb@gmail.com> wrote: > > > > I''m having one problem. In my code, inside the <input ... > tag I have: > > > > <% if @listing.categories.include ? category %>checked="checked"<% end > > %> > > > > because the same form that''s used to create is used to edit. Obviously > > this results in the categories being checked, but the subcategories > > aren''t toggled on. I tried this inside the <div id="categories">: > > > > <% if @listing.categories.include? category %> > > <script language="javascript" > > type="text/javascript">Element.toggle(''subcategories_<%=category.name% > > >'');</script> > > <% end %> > > > > but it didn''t togggle... > > > > Sorry, I''m still a newbie at all of this. > > > > Thanks again, > > > > Adam > > > > -- > > Posted via http://www.ruby-forum.com/ . > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/317c844a/attachment.html
Adam Bloom
2006-Apr-01 19:48 UTC
[Rails] Re: Re: Re: Checkboxes expand to show new options when check
Nicolas Buet wrote:> Your problem is that you would like a category checked to be shown, > right? > then the code > <div id="subcategories_<%= category.name %>" style="display:none"> > Should be replaced by > <div id="subcategories_<%= category.name %>" <%= ''style="display:none"'' > if > @listing.categories.include? category %> > > > RegardsDoh! It seems so obvious once I see it. :) Thanks a lot, Adam -- Posted via http://www.ruby-forum.com/.