Topic: how to update ranking in sortable elements?

hey all,
I have a sortable list like that:

# view
<ul id="list">
<% 6.times do |i| -%>
<span class="item_position" id="item_position_<%= i+1 %>">
           <%= i+1 %></span>  <li id="item_<%= i+1 %>">I'm number <%=
i+1 %></li>
<% end -%>
</ul>

<p id="list-info"></p>

<%= sortable_element 'list',
     :update => 'list-info',
     :complete => visual_effect(:highlight, 'list'),
     :url => { :action => "order" } %>

# controller
def order
   @order = params[:list]
   render :partial => 'list'
end


I would like to update the position number in the span item_position
as I drag and drop elements. As an exemple, if I drag element number 5
to the second position I would like to update its span to 2.

Any idea how to do that?

thanx in advance

Pat

Last edited by patcito (2006-12-29 09:58:40)

Re: how to update ranking in sortable elements?

funny, i just did this.

Here is the generic code:

here is the view:

<div id="admin_index" class="rowcontainer">
  <div class="threequartercolumn">
    <h1>Clients in category <%= @category.title %></h1>

    <div id="client_list">
     <%= render :partial => "client_list", :locals => {:clients => @clients, :category => @category} %>
    </div>
  </div>
   
</div>


partial:
  <ul id="clients">
    <li id="title" class="clearfix"><span class="client_id">ID</span><span class="client_title">Title</span><span class="client_position">Position</span></li>
    <% for client in clients %>
     <li id="client_<%= client.id %>">
       <span class="client_id"><%= link_to client.id, :action => "edit_client", :id => client.id %></span>
       <span class="client_title"><%= client.nice_title %></span>
       <span class="client_position"><%= client.position(category) %></span>
     </li>
    <% end %>
  </ul>

<%= sortable_element 'clients', :url => { :action => "sort", :id => @category.id } %>


controller code:
def sort
    @category = Category.find(params[:id])
    @clients = @category.clients.find_all_authorized
    @clients.each do |client|
      cat_client = CategoryClient.find_by_category_id_and_client_id(@category, client)
      cat_client.position = params['clients'].index(client.id.to_s) + 1
      cat_client.save
    end
    @clients = @category.clients(true).find_all_authorized
    render :update do |page|
      page.replace_html "client_list", :partial => "client_list", :locals => {:clients => @clients, :category => @category}
      page.visual_effect :highlight, "clients", :duration => 0.5, :endcolor => "\'#262626\'"
    end
  end

hope that helps

--jake

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: how to update ranking in sortable elements?

Very cool, I was wondering if RJS would work with sortable_element. Glad to see it does.

Railscasts - Free Ruby on Rails Screencasts

Re: how to update ranking in sortable elements?

thanks a lot, that's really cool indeed.

Re: how to update ranking in sortable elements?

ryanb wrote:

Very cool, I was wondering if RJS would work with sortable_element. Glad to see it does.

oh yea, RJS is badass.

I find myself coding less and less in JS and more RJS.  Even for simple stuff, like clicking on a link and having it fill in 100 text boxes with default info, I'll use link_to_remote and create a RJS method to handle it. 

I dont know if it is faster, but it is WAAAYYY easier smile




--jake

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.