How do I generate dynamic items dynamically?

Hi All,

I’m looking for a way to append elements with javascript using
javascript. Basically I have two lists of items. One has a list of
items with an “ADD” button (using “link_to_remote”) and the other has
a list of items with a “REMOVE” button (using “link_to_remote”). When
I click the “ADD” it immediately places the item into the other list.
However, I need to be able to have the newly inserted item perform the
reverse action for “REMOVE”.

I see no way (besides, perhaps creating a partial to render a non-
object) to dynamically generate dynamic items of this nature.

Any ideas?

Best.

Michael Williams wrote:

Hi All,

I’m looking for a way to append elements with javascript using
javascript. Basically I have two lists of items. One has a list of
items with an “ADD” button (using “link_to_remote”) and the other has
a list of items with a “REMOVE” button (using “link_to_remote”).

Do you really need to do it that way? That will call the server every
time a button is clicked. You might want to do the list editing on the
client side, then have a Save button that sends the list to the server.

When
I click the “ADD” it immediately places the item into the other list.
However, I need to be able to have the newly inserted item perform the
reverse action for “REMOVE”.

So the Add button works, but the Remove button does not? You should be
able to do both the same way – after all, the only difference is that
Add moves things from list 1 to list 2, while Remove does likewise from
list 2 to list 1.

I see no way (besides, perhaps creating a partial to render a non-
object) to dynamically generate dynamic items of this nature.

I am not sure what your problem is. Please provide more detail and
code.

Any ideas?

Best.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I am not sure what your problem is. Please provide more detail and
code.

Basically I have two partials that render. One is the “Available
Items” and the other is “Selected Items”. If I select an “Available
Item” then it gets added to “Selected Items”. However, if I remove a
“Selected Item” I need it to go back to “Available Items”. When they
are initially rendered from the VIEW code, both work just fine.
However, I need to dynamically insert javascript for each newly added
unordered list item to either column. Currently I’m actually using a
partial within a partial to manage each new item. It adds a
“link_to_remote” item for each newly added item. My “issue” is that
I can’t figure out how to use the javascript helpers via the
“render :update” method in the controller so I have to call a partial
in order to insert the appropriate code. I could very likely be
asking the wrong question(s)

The reason I need it to update immediately is because it is a multi-
user setup and if one user has selected an item, I need it to
immediately be “taken out” of circulation. In either case, I’d still
need dynamic code to, at the very least, move the items back and
forth.

Michael Williams wrote:

I am not sure what your problem is. �Please provide more detail and
code.

Basically I have two partials that render. One is the “Available
Items” and the other is “Selected Items”. If I select an “Available
Item” then it gets added to “Selected Items”. However, if I remove a
“Selected Item” I need it to go back to “Available Items”.

Yes, I get that. As I said in my first message, if you can do one, you
can do the other the same way – so what’s the problem?

When they
are initially rendered from the VIEW code, both work just fine.
However, I need to dynamically insert javascript for each newly added
unordered list item to either column.

By “dynamically insert javascript”, do you mean that you think you need
to write appropriate JS to the onclick attribute in the HTML tag? If
so, that’s incorrect. You just need to add behavior to the appropriate
DOM element, and that’s very easy to do.

Currently I’m actually using a
partial within a partial to manage each new item. It adds a
“link_to_remote” item for each newly added item.

Why two levels of partials? I don’t see how that’s necessary here.

My “issue” is that
I can’t figure out how to use the javascript helpers via the
“render :update” method in the controller so I have to call a partial
in order to insert the appropriate code.

As I keep saying on this list, learn the framework instead of kludging
your way around it.

I could very likely be
asking the wrong question(s)

It seems to me that you should be asking how to use render :update (or
similar) properly. Or just don’t use RJS and write the JavaScript
directly.

The reason I need it to update immediately is because it is a multi-
user setup and if one user has selected an item, I need it to
immediately be “taken out” of circulation.

I understand. I’ve done things like that as well, though not with
Rails.

In either case, I’d still
need dynamic code to, at the very least, move the items back and
forth.

I do not know if you really know what you mean when you say “dynamic
code”. Please explain how you understand that phrase; I think you may
be using it in an inaccurate sense.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]