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 Tigers

Lions
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 %>

<%= category.name %>
<% for subcategory in category.subcategories %>
<%= subcategory.name %> <% 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

Still curious about this…

-Adam

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

<%= category.name %>
<% for subcategory in category.subcategories %>
<%= subcategory.name %>
<% end %> <% end %>

IMHO, ajax would be useful here only if subcategory is big. Otherwise,
this
method work fine.

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

:

<% if @listing.categories.include? category %>

<% end %>

but it didn’t togggle…

Sorry, I’m still a newbie at all of this.

Thanks again,

Adam

Your problem is that you would like a category checked to be shown,
right?
then the code

Should be replaced by
>

Regards

Ops, I meant:

>

IMHO, ajax would be useful here only if subcategory is big. Otherwise,
this
method work fine.

That works great! Thanks a lot,

-Adam

Nicolas B. wrote:

Your problem is that you would like a category checked to be shown,
right?
then the code

Should be replaced by
>

Regards

Doh! It seems so obvious once I see it. :slight_smile:

Thanks a lot,

Adam