使用sortablejs 对分层数据排序

在项目中,需要对有层级关系的数据进行排序,如图

Item 1 可以在item 2 、item 3 、item 4 等之间进行拖拽,但是item 1.1 item 1.2 等 只能在item 1 里面拖拽。拖拽的范围只能是该item 所在的层级里面拖拽,不能超出,也不能去其他层级。

代码如下:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sortable.js中文网</title>
    <link rel="stylesheet" href="https://sortablejs.com/assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/monokai-sublime.min.css">
    <link rel="stylesheet" href="https://sortablejs.com/assets/css/Features-Clean.css">
</head>

<body>

    <div class="features-clean"></div>

    <div class="container">
      <div class="col">
        <div id="nested" class="row">

          <div id="nestedDemo" class="list-group col nested-sortable">
              <div class="list-group-item nested-1">Item 1
                  <div class="list-group nested-sortable" id="nested-1">
                    <div class="list-group-item nested-2">Item 1.1</div>
                    <div class="list-group-item nested-2">Item 1.2
                      <div class="list-group nested-sortable" id="nested-1-2">
                        <div class="list-group-item nested-3">Item 1.2.1</div>
                        <div class="list-group-item nested-3">Item 1.2.2</div>
                        <div class="list-group-item nested-3">Item 1.2.3</div>
                        <div class="list-group-item nested-3">Item 1.2.4</div>
                      </div>
                    </div>
                    <div class="list-group-item nested-2">Item 1.3</div>
                    <div class="list-group-item nested-2">Item 1.4</div>
                  </div>
              </div>
              <div class="list-group-item nested-1">Item 2</div>
              <div class="list-group-item nested-1">Item 3</div>
              <div class="list-group-item nested-1">Item 4
                <div class="list-group nested-sortable" id="nested-4">
                  <div class="list-group-item nested-2">Item 4.1</div>
                  <div class="list-group-item nested-2">Item 4.2</div>
                  <div class="list-group-item nested-2">Item 4.3</div>
                  <div class="list-group-item nested-2">Item 4.4</div>
                </div>
              </div>
              <div class="list-group-item nested-1">Item 5</div>
          </div>
        </div>
      </div>
    </div>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.0/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/javascript.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.8.3/Sortable.min.js"></script>
    <script src="https://sortablejs.com/assets/js/theme.js"></script>

    <script>

      new Sortable(document.querySelector('#nestedDemo'),{
        handle: '.list-group-item', // handle's class
        animation: 150
      });
      new Sortable(document.querySelector('#nested-1'),{
        handle: '.list-group-item', // handle's class
        animation: 150
      });
      new Sortable(document.querySelector('#nested-1-2'),{
        handle: '.list-group-item', // handle's class
        animation: 150
      });
      new Sortable(document.querySelector('#nested-4'),{
        handle: '.list-group-item', // handle's class
        animation: 150
      });

    </script>
    
</body>

</html>
相关推荐
细心的莽夫1 天前
JavaWeb学习笔记
java·开发语言·笔记·学习·java-ee·web
kali-Myon1 天前
NewStarCTF2024-Week5-Web&Misc-WP
前端·python·学习·mysql·web安全·php·web
觅_1 天前
HTML 鼠标滑动 页面的header背景从透明色变为黑色
前端·html·web
知孤云出岫2 天前
web安全测试渗透案例知识点总结(下)——小白入狱
网络·网络安全·web
Dangks4 天前
[前端] 为网站侧边栏添加搜索引擎模块
前端·javascript·搜索引擎·html·web·seo·博客技巧
扛枪的书生4 天前
Web 靶场笔记-bWAPP
渗透·web·kali
会思想的苇草i5 天前
CSS秘籍-高效样式技巧
前端·css·html·css3·web·flex
满分观测网友z6 天前
ExpandingCard扩展卡片
web·js
行十万里人生6 天前
HTTP 协议
后端·网络协议·http·网络安全·青少年编程·信息与通信·web
墨渊君9256 天前
初窥 HTTP 缓存
前端·网络协议·http·缓存·web