Jquery UI嵌套了具有多个UL的Sortable

时间:2012-04-17 14:50:49

标签: javascript jquery jquery-ui

我正在尝试使用多个UL实施jquery UI nested sortables。以下是代码,

   <html>
    <head>
    <script type="text/javascript" src="js/jquery-1.5.2.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){   
    $("#example5 ul").sortable({
                    connectWith: "#example5 ul",
                    placeholder: "ui-state-highlight"
    });

    $("span.item-controls").click(function(){
        //alert($(this).parent().next('div').attr('class'))
        var v = $(this).parent().next('div.panel');
        v.slideToggle("slow");
      });

    });
    </script>
    <style>
    ul { 
        min-height:10px; 
        }
    li {
        margin:5px;
        width:auto;
        border:1px solid #000;
        list-style-type:none;
        cursor:move;
    }

    #example5 {
          display: inline-block;
          float: left;
          position: relative;
          width:900px;
    }
    .fright
    {
          float:right;
    }
    .item-controls {
          cursor: pointer;
          font-size: 12px;
          position: relative;
          right: 20px;
          top: 5px;
          background: url("images/arrows.png") no-repeat scroll transparent;
          xborder: 1px solid #000;    
          background-position:3px 0px;
          width:20px;
          height:15px;
    }.ui-sortable{
    margin:0px;padding:0px;
    }
    .ahead
    {
         margin:0px;
    }
    div.panel
    {
        height:auto;
        display:none;
    }
    div.panel
    {
        margin:0px;
        text-align:center;
        background:#e5eecc;
        border:solid 1px #c3c3c3;
    }
    </style>
    </head>

    <body>
    <div id="example5"><!--UL1-->
        <ul style="width:32%;float:left;" class="navigation">
             <li ><p class="ahead"><span class="heading">Item 1 </span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1</p>
                    </div>
                <ul>
                    <li >
                    <p class="ahead"><span class="heading">Item 1 1</span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1 1</p>              
                    </div>              
                    <ul></ul></li>
                    <li >Item 1 2<ul></ul></li>
                    <li >Item 1 3<ul></ul></li>
                 </ul>

             </li>
             <li >
             <p class="ahead"><span class="heading">Item 2 </span><span class="fright item-controls"></span></p>
                    <div class="panel">
                        <p>This is Item 2</p>       
                    </div>       
             <ul><li >Item 2 1<ul></ul></li></ul>
             </li>
             <li >Item 3<ul></ul></li>
             <li >Item 4<ul></ul></li>
        </ul>
    <!--UL2-->
        <ul style="width:32%;float:left;"  class="navigation2">
             <li ><p class="ahead"><span class="heading">Item 1 </span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1</p>
                    </div>
                <ul>
                    <li >
                    <p class="ahead"><span class="heading">Item 1 1</span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1 1</p>              
                    </div>              
                    <ul></ul></li>
                    <li >Item 1 2<ul></ul></li>
                    <li >Item 1 3<ul></ul></li>
                 </ul>

             </li>
             <li >
             <p class="ahead"><span class="heading">Item 2 </span><span class="fright item-controls"></span></p>
                    <div class="panel">
                        <p>This is Item 2</p>       
                    </div>       
             <ul><li >Item 2 1<ul></ul></li></ul>
             </li>
             <li >Item 3<ul></ul></li>
             <li >Item 4<ul></ul></li>
        </ul>
<!--UL3-->
           <ul style="width:32%;float:left;"  class="navigation3">
             <li ><p class="ahead"><span class="heading">Item 1 </span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1</p>
                    </div>
                <ul>
                    <li >
                    <p class="ahead"><span class="heading">Item 1 1</span><span class="fright item-controls"></span></p>
                     <div class="panel">
                        <p>This is Item 1 1</p>              
                    </div>              
                    <ul></ul></li>
                    <li >Item 1 2<ul></ul></li>
                    <li >Item 1 3<ul></ul></li>
                 </ul>

             </li>
             <li >
             <p class="ahead"><span class="heading">Item 2 </span><span class="fright item-controls"></span></p>
                    <div class="panel">
                        <p>This is Item 2</p>       
                    </div>       
             <ul><li >Item 2 1<ul></ul></li></ul>
             </li>
             <li >Item 3<ul></ul></li>
             <li >Item 4<ul></ul></li>
        </ul>
    </div>
    </body>
    </html> 

到目前为止这么好,得到了预期的结果。现在我的要求是,UL-1不能对UL-2 and UL-3进行排序,UL-2,UL-3也不应该可以排序。希望您了解要求。有可能做到这一点。请提出建议。

2 个答案:

答案 0 :(得分:2)

尝试使用这种方式

$("#example5 ul").sortable({
    connectWith: "#example5 ul.navigation *",
    placeholder: "ui-state-highlight"
});

因此,您的目标将始终是您的主要UL,并且仅用于可排序元素。

注意:

我在你的html部分添加了不同的类名来使用这个解决方案。

答案 1 :(得分:0)

你必须设置“容器”,例如:

$("#example5 ul").sortable({
                connectWith: "#example5 ul",
                placeholder: "ui-state-highlight",
                containment: "#selector"
});

有关详细信息,请check this.