javascript - set order to jquery sortable - Stack Overflow

I use jquery draggble function for unorderd list (ul) and face a problem of getting the order of items

I use jquery draggble function for unorderd list (ul) and face a problem of getting the order of items after it changed and setting the order ofter page is loaded.Suppose we have the following code :

<html>
   <head>
     <script src=".js"></script>
     <script src=".4/jquery.min.js>  </script>
     <script src=".8/jquery-ui.min.js"></script>
   <script>
      $(document).ready(function(){
            $('.secondblock').sortable({axis:'y'});

            $(".block").sortable({
            axis: 'y',
            update: function(event, ui) {// order changed
               var order = jQuery(this).sortable('toArray');
               // set this order to .secondblock

            }
         });
      });
  </script>

  </head>
  <body>
      <ul class="block" type="none">
           <li id = "one">1</li>
           <li id = "two">2</li>
           <li id = "three">3</li>
           <li id = "four">4</li>
           <li id = "five">5</li>
      </ul>
      <ul class="secondblock" type="none">
           <li id = "one">1</li>
           <li id = "two">2</li>
           <li id = "three">3</li>
           <li id = "four">4</li>
           <li id = "five">5</li>
      </ul>
  </body>

Are there any possible solutions?

I use jquery draggble function for unorderd list (ul) and face a problem of getting the order of items after it changed and setting the order ofter page is loaded.Suppose we have the following code :

<html>
   <head>
     <script src="http://code.jquery./jquery-latest.js"></script>
     <script src="http://ajax.googleapis./ajax/libs/jquery/1.4/jquery.min.js>  </script>
     <script src="http://ajax.googleapis./ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
   <script>
      $(document).ready(function(){
            $('.secondblock').sortable({axis:'y'});

            $(".block").sortable({
            axis: 'y',
            update: function(event, ui) {// order changed
               var order = jQuery(this).sortable('toArray');
               // set this order to .secondblock

            }
         });
      });
  </script>

  </head>
  <body>
      <ul class="block" type="none">
           <li id = "one">1</li>
           <li id = "two">2</li>
           <li id = "three">3</li>
           <li id = "four">4</li>
           <li id = "five">5</li>
      </ul>
      <ul class="secondblock" type="none">
           <li id = "one">1</li>
           <li id = "two">2</li>
           <li id = "three">3</li>
           <li id = "four">4</li>
           <li id = "five">5</li>
      </ul>
  </body>

Are there any possible solutions?

Share Improve this question asked Aug 8, 2012 at 17:33 ilya.stmnilya.stmn 1,6245 gold badges23 silver badges41 bronze badges 2
  • So, you want to save the order of the elements, so it's the same when you re-load the page? – gen_Eric Commented Aug 8, 2012 at 17:43
  • yes, i've ommited details of post and getting the strings with orders, just need to understand how can i get and set orders of list items – ilya.stmn Commented Aug 9, 2012 at 7:33
Add a ment  | 

2 Answers 2

Reset to default 5

First, you shouldn't have the same id appear twice in a document. That will cause all kinds of problems.

Instead, set a data-attribute on the items in the second list to reflect corresponding items in the first list:

<ul class="block" type="none">
    <li id = "one">1</li>
    <li id = "two">2</li>
    <li id = "three">3</li>
    <li id = "four">4</li>
    <li id = "five">5</li>
</ul>
<ul class="secondblock" type="none">
    <li data-block-id = "one">1</li>
    <li data-block-id = "two">2</li>
    <li data-block-id = "three">3</li>
    <li data-block-id = "four">4</li>
    <li data-block-id = "five">5</li>
</ul>

Then reflecting the sort of the first list in the second list is simple:

$('.block').sortable({update: sortOtherList});

function sortOtherList(){
    $('.block li').each(function(){ 
        $('.secondblock [data-block-id=' + $(this).attr('id') + ']')
            .remove()
            .appendTo($('.secondblock'));

    });
}

See it working here: http://jsfiddle/6HKZG/2/

Faust's looks better to me. Mark it if it's what you wanted.

Yes. I feel like I need more information, but I had to do almost the exact same thing recently.

First, you want to extend the javascript array object with a sorting algorithm. Here is what I use:

Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

source Move an array element from one array position to another

Then, what I would do is use this along with an OldPosition/NewPosition function to get the index of the element before and after the sort, and use this method to move the object in the array.

I think JQuery sort let's you get information about the array before and after the sort

ex:

$('li').presort(function() {
  window.oldindex = $(this).index();
});
$('li').postsort(function() {
  window.newindex = $(this).index();
  array.move(oldindex,newindex);
});

If you're just looking to make .secondblock match .block after sort, you can do this: source: http://jqueryui./demos/sortable/#events

$( ".selector" ).sortable({
   start: function(event, ui) { //Get the order of the .block list items before dragging }
});
$( ".selector" ).sortable({
   stop: function(event, ui) { //Get the new order of the list items after dragging. Compare the two orders and sort .secondblock to match block }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744740495a4590997.html

相关推荐

  • javascript - set order to jquery sortable - Stack Overflow

    I use jquery draggble function for unorderd list (ul) and face a problem of getting the order of items

    10天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信