最終更新 9 months ago

本範例展示如何使用 SortableJS 實現可拖放的巢狀清單,允許用戶調整層級結構與順序,適用於 層級式資料管理、分類管理、階層式 UI 排序 等應用。

timmy revised this gist 9 months ago. Go to revision

1 file changed, 63 insertions

sortablejs_nested_list.html(file created)

@@ -0,0 +1,63 @@
1 + <!DOCTYPE html>
2 + <html lang="zh-TW">
3 + <head>
4 + <meta charset="UTF-8">
5 + <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 + <title>SortableJS 巢狀清單</title>
7 + <style>
8 + .nested-list {
9 + list-style: none;
10 + padding-left: 20px;
11 + }
12 + .nested-item {
13 + padding: 8px;
14 + margin: 5px;
15 + background: #fff;
16 + border: 1px solid #ddd;
17 + cursor: grab;
18 + }
19 + .nested-item:hover {
20 + background: #f0f0f0;
21 + }
22 + </style>
23 + </head>
24 + <body>
25 +
26 + <h2>可拖曳巢狀清單</h2>
27 +
28 + <ul id="nested-list" class="nested-list">
29 + <li class="nested-item">項目 1
30 + <ul class="nested-list">
31 + <li class="nested-item">子項目 1.1</li>
32 + <li class="nested-item">子項目 1.2</li>
33 + </ul>
34 + </li>
35 + <li class="nested-item">項目 2</li>
36 + <li class="nested-item">項目 3
37 + <ul class="nested-list">
38 + <li class="nested-item">子項目 3.1</li>
39 + </ul>
40 + </li>
41 + </ul>
42 +
43 + <script src="https://cdn.jsdelivr.net/npm/sortablejs@1.15.0/Sortable.min.js"></script>
44 + <script>
45 + new Sortable(document.getElementById('nested-list'), {
46 + animation: 150,
47 + group: 'nested',
48 + fallbackOnBody: true,
49 + swapThreshold: 0.65
50 + });
51 +
52 + document.querySelectorAll('.nested-list').forEach(list => {
53 + new Sortable(list, {
54 + animation: 150,
55 + group: 'nested',
56 + fallbackOnBody: true,
57 + swapThreshold: 0.65
58 + });
59 + });
60 + </script>
61 +
62 + </body>
63 + </html>
Newer Older