开发者

jQuery shuffle plugin breaks jQuery-UI sortables

I have a problem with a jQuery shuffle plugin I found. It breaks my nested sortable lists. At the bottom I put a link to a demo showing the problem, which is described below.

The html is simple, there is an ordered list containing four list items. Each of these list items contains an unordered list with two list items. And there is a button at the bottom to shuffle the ordered list.

  <ol> 
    <li> 
      <ul> 
        <li>Item A</li> 
        <li>Item B</li> 
      </ul> 
    </li> 
    <li> 
      <ul> 
        <li>Item C</li> 
        <li>Item D</li> 
      </ul> 
    </li> 
    <li> 
      <ul> 
        <li>Item E</li> 
        <li>Item F</li> 
      </ul> 
    </li> 
    <li> 
      <ul> 
        <li>Item G</li> 
        <li>Item H</li> 
      </ul> 
    </li> 
  </ol> 
  <button type="button" class="shuffle">Shuffle</button>

The ordered lists is sortable, and also the unordered lists are sortable and connected (e.g. I can开发者_开发问答 move "Item A" below "Item G"). So far, so good!

$(document).ready(function () {

  $('ol').sortable({
    placeholder: 'dashed'
  });
  $('ul').sortable({
    placeholder: 'dashed',
    connectWith: 'ul'
  });
  $('.shuffle').button().click(function(){
    $('ol').shuffle();
  });

});

The shuffle button (almost) does what I expect it to do, it shuffles the ordered list (not the unordered!). But as it does that, it also breaks the unordered sortable lists. No longer am I able to move the elements around as it worked before I shuffled.

Am I doing something wrong? Is there a bug in the plugin?

Link to the plugin: http://yelotofu.com/labs/jquery/snippets/shuffle/jquery.shuffle.js

Link to a demo of the problem: http://jsbin.com/umeju6


After some testing I found out that doing this works:

$(document).ready(function () {

  $('ol').sortable({
    placeholder: 'dashed'
  });
  $('ul').sortable({
    placeholder: 'dashed',
    connectWith: 'ul'
  });
  $('.shuffle').button().click(function(){
    $('ul').sortable("destroy");
    $('ol').shuffle();
    $('ul').sortable({
      placeholder: 'dashed',
      connectWith: 'ul'
    });
  });

});

Not sure why though. It's not a very elegant solution either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜