简体   繁体   English

限制jQuery Sortable中的嵌套容器

[英]Restrict nested container in jQuery Sortable

i am working on an UI which will use this jquery sortable script ( http://johnny.github.io/jquery-sortable/ ). 我正在使用该jquery可排序脚本( http://johnny.github.io/jquery-sortable/ )的UI上工作。 I want to have a nested list, where i can only drag specific items into. 我想要一个嵌套列表,在其中只能将特定项目拖入其中。 Here is an example: 这是一个例子:

This is the nested list. 这是嵌套列表。 What i do want do prevent is, that i can Drag 我要阻止的是,我可以拖动

  • Second 第二
  • Third 第三
  • into the nested list. 进入嵌套列表。

    $("ol.example").sortable({
        isValidTarget: function($item, container){
        // Here should be some kind to restrict the nested list
        }
    });
    

    I can initialize the sortable-list and configure possible actions with the isValidTarget: Option 我可以初始化sortable-list并使用isValidTarget:Option配置可能的操作

     $("ol.example").sortable({ isValidTarget: function($item, container){ // Here should be some kind to restrict the nested list } }); 

    I couldn't get it to work, because i don't know how to ask if the target is the nested list. 我无法使其正常工作,因为我不知道如何询问目标是否为嵌套列表。 Does anybody have an idea? 有人有主意吗? Maybe its an easy question, but i'm pretty new to JavaScript. 也许这是一个简单的问题,但是我对JavaScript还是很陌生。

    Best regards 最好的祝福

    The container has the target element as a property ( container.el ) which contains the jQuery element of the currently targeted container. 容器具有target元素作为属性( container.el ),其中包含当前目标容器的jQuery元素。 You can use jQuery methods on that to determine if it's a valid target. 您可以在上面使用jQuery方法来确定它是否是有效的目标。 I think the following is what you're trying to do. 我认为以下是您要尝试执行的操作。

     $("ol.example").sortable({ isValidTarget: function($item, container) { if (container.el.hasClass("nested1")) { return false; } else { return true; } } }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://johnny.github.io/jquery-sortable/js/jquery-sortable-min.js"></script> <ol class="example"> <li>First <ol class="nested1"> <li>N1</li> <li>N2</li> <li>N2</li> </ol> </li> <li>Second</li> <li>Third</li> </ol> 

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM