Topic: Sortable_element: How to add item with JS

Hi,

I have a sortable list for some items and I'm adding items to this list
with Ajax calls and page.insert.
Unfortunately those new items are not sortable when added, only after I
refresh the page.

Does anyone know how I can add them 'register' them somehow to that
sortable list via JS or so, when inserting?

Last edited by Christoph (2007-01-27 06:26:56)

Re: Sortable_element: How to add item with JS

After adding the item to the list have you tried calling the sortable_element again to reset the list?

Railscasts - Free Ruby on Rails Screencasts

Re: Sortable_element: How to add item with JS

I'm about to fix it right now, I just added another partial that is called
from edit.rhtml so I can call this partial from create() in a create.rjs to
re-render (replace) the whole sortable_element.

Is that what you mean with call it again?
Or is there something easier to refresh the function?

Christoph

Last edited by Christoph (2007-01-27 12:45:52)

Re: Sortable_element: How to add item with JS

That will probably work too. What I was referring to is just calling the sortable_element function again. If you post the code you are using to add the item to the sortable list I can post the code to show you what I mean. I don't know if it will work though (depends how scriptaculous handles it).

Railscasts - Free Ruby on Rails Screencasts

Re: Sortable_element: How to add item with JS

Ah now I know what you meant.
I found this ticket: http://dev.rubyonrails.org/ticket/3893
which lead me to this function: 'sortable' function in PrototypeHelper/JSGenerator

if I use page.sortable('id_of_the_sortable') in my create.rjs, it refreshes
the list.

Thanks a lot smile

Re: Sortable_element: How to add item with JS

Sweet! I never knew about page.sortable.

Railscasts - Free Ruby on Rails Screencasts

Re: Sortable_element: How to add item with JS

okay one more thing about the usage of this call.

do I need to add both, <%= sortable_element..... %> in the view page.sortable in my
rjs files when updating the page?

or can I issue the page.sortable page call somehow when loading a page like edit.rhtml ?

"Creates a script.aculo.us sortable element. Useful to recreate sortable elements after items get added or deleted."

okay that sounds as if I need both ..

Last edited by Christoph (2007-01-27 14:41:59)

Re: Sortable_element: How to add item with JS

Yep, you need both. The RJS isn't called when loading the page normally (at least I assume it isn't).

Railscasts - Free Ruby on Rails Screencasts

Re: Sortable_element: How to add item with JS

Yeah it seems so,
thanks smile