Checkboxes expand to show new options when checked?


#1

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


#2

Still curious about this…

-Adam


#3

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.


#4

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


#5

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

Should be replaced by
>

Regards


#6

Ops, I meant:

>

#7

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

That works great! Thanks a lot,

-Adam


#8

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