Dynamically rename div id?

I’ve got a sortable list that you can add new elements to dynamically,
using the prototype helpers (http://api.rubyonrails.org/classes/
ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/
GeneratorMethods.html)

I want to be able to remove a selected item from the list dynamically
– presumably, each item in the list would have a link_to_remote that
would pop a confirmation dialog, then remove the selected row from the
list. When this happens, though, I will need to renumber all the
other div id’s in my list to keep the list in good, numerical order.

Any idea on how to rename a div id in this manner? I don’t want to
replace the div outright, if possible, as that seems really messy.

On Mon, Jan 24, 2011 at 12:45 PM, robo
[email protected]wrote:

Why?

Any idea on how to rename a div id in this manner? I don’t want to
replace the div outright, if possible, as that seems really messy.

Just an idea, but maybe you can leave the div’s as is — when you
remove a
div then it just disappears, and the visible list order is still
preserved.
Then if the user does re-order the list at the ui, then you send ajax on
that event.

On Jan 24, 2011, at 1:45 PM, robo wrote:

I’ve got a sortable list that you can add new elements to dynamically,
using the prototype helpers (http://api.rubyonrails.org/classes/
ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/
GeneratorMethods.html)

I want to be able to remove a selected item from the list dynamically
– presumably, each item in the list would have a link_to_remote that
would pop a confirmation dialog, then remove the selected row from the
list. When this happens, though, I will need to renumber all the
other div id’s in my list to keep the list in good, numerical order.

No, you really really don’t want to do this. Sortables use the actual
ID of the related object to update the position column in the database
for the appropriate records.

If you’re using the sortable list helpers, you must have something
like this in your view:

  • ,,,
  • And when you sort, the callback sends an array of these IDs back to
    the server in their current order.

    You do want to have a separate callback to remove the object from the
    collection you’re sorting, but that’s outside the scope of your
    sortable callback.

    Each time you update the position attribute in the database, you need
    to be doing that against a fresh lookup of your collection in your
    controller (so you won’t try to sort a missing element)[1]. And when
    you do, these IDs that are sent from the page must be the actual IDs
    of the elements, not some arbitrary numerical order values, or else
    you can’t update the correct elements with their new position value.

    Walter

    1. def sort
      @categories = Category.all
      @categories.each do |category|
      category.position = params[‘sort_list’].index(category.id.to_s)
    • 1
      category.save
      end
      render :nothing => true
      end