1

I have a list of "tasks" and I'm using "sortable" so that a user can prioritize them, putting the most important ones on top. But of course, every time the page is refreshed they go back to their original position. I'm wondering if it's possible to save the position of the tasks?

Here's my code (it's a rails project):

<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

  <script>
    $(document).ready(function() {
      $('.move').sortable();
      });
  </script>


<%= render "form" %>



<div class="move">
<% if [email protected]? %>
  <% for item in @project.tasks %>
  <div class="container">
  <div class="panel panel-default">
  <div class="panel-heading">
    <div class='move'><%= item.title %></div>
  </div>
  </div>
  <div class="panel-body">
  <p>
    <div class='move'><%= item.description %></div>
  </p>
  <%= link_to "Edit", edit_project_task_path(@project, item) %> 
  </div>
  </div>
  <% end %>
<% end %>
</div>
2

2 Answers 2

1

You can use jQuery cookie and get the li position using jQuery UI sortable toArray:

Serializes the sortable's item id's into an array of string.

then in the load change the li position by swithin their position using insertAfter and eq selector.

Code:

var savedOrd = $.cookie('sortOrder');
if (savedOrd) {
    $.each(savedOrd.split(','), function (i, e) {
        $('#' + e).insertAfter('#sortable>li:eq(' + i + ')');
    });
}

$('#save').click(function () {
    var saveOrd = $('#sortable').sortable('toArray');
    $.cookie('sortOrder', saveOrd);
});

Demo: http://jsfiddle.net/IrvinDominin/amq6gt8w/

Alternatively instead of using cookie you can call two server methods to set and get the element positions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this was helpful. I'm still trying to integrate it with my own project though, I can't seem to get it to work. I don't really know jQuery, I'm just trying to implement this one jQuery feature on my Rails project.
0

You can find the index of the task and save it and sort based on index while displaying.

To find the index of the task on UI refer this : Fiddle

$("div").click(function(){
    var index = $("div").index($(this));
    var position =  index+1;
    $('span').html(position);
})

2 Comments

Sorry I posted the wrong code. The answer should still be the same though I image, but I updated the original post. I'm a little confused by what you mean though (I just started coding a few weeks ago). You're saying I should replace my jQuery with what you posted? I tried that and I can no longer move the tasks.
@roguerat of course they will be removed when page is refreshed. Either you have to save the order in Database or session so when page is refreshed you can align them in proper order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.