SortableJS
SortableJS JavaScript library for reorderable drag-and-drop lists.
SortableJS documentationHow to use
Copy-paste the following <script>
near the end of your pages under JS Implementing Plugins to enable it.
<script src="../node_modules/sortablejs/Sortable.min.js"></script>
Copy-paste the following <script>
near the end of your pages under JS Front to enable it.
<script src="../assets/js/hs.sortable.js"></script>
Copy-paste the init function under JS Plugins Init., before the closing </body>
tag, to enable it.
<script>
(function() {
// INITIALIZATION OF SORTABLE
// =======================================================
HSCore.components.HSSortable.init('.js-sortable')
});
</script>
Simple list example
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup"
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
Shared lists
Use "group": "listGroupName"
<div class="row">
<div class="col-md-6">
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup"
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
</div>
<div class="col-md-6">
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup"
}'>
<div class="list-group-item bg-light">Item 1</div>
<div class="list-group-item bg-light">Item 2</div>
<div class="list-group-item bg-light">Item 3</div>
<div class="list-group-item bg-light">Item 4</div>
<div class="list-group-item bg-light">Item 5</div>
<div class="list-group-item bg-light">Item 6</div>
</div>
<!-- End List Group -->
</div>
</div>
<!-- End Row -->
Disable sorting
Try sorting the list on the left. It is not possible because it has it's sort option set to false
. However, you can still drag from the list on the left to the list on the right.
<div class="row">
<div class="col-md-6">
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"group": {
"name": "listGroup3",
"pull": "clone",
"put": false
}
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
</div>
<div class="col-md-6">
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup3"
}'>
<div class="list-group-item bg-light">Item 1</div>
<div class="list-group-item bg-light">Item 2</div>
<div class="list-group-item bg-light">Item 3</div>
<div class="list-group-item bg-light">Item 4</div>
<div class="list-group-item bg-light">Item 5</div>
<div class="list-group-item bg-light">Item 6</div>
</div>
<!-- End List Group -->
</div>
</div>
<!-- End Row -->
Handle
<!-- List Group -->
<div class="js-sortable sortablejs-custom list-group"
data-hs-sortable-options='{
"animation": 150,
"handle": ".sortablejs-custom-handle"
}'>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 1
</div>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 2
</div>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 3
</div>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 4
</div>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 5
</div>
<div class="list-group-item">
<i class="sortablejs-custom-handle bi-grip-horizontal list-group-icon"></i> Item 6
</div>
</div>
<!-- End List Group -->
Filter
Try dragging the item with a red background. It cannot be done, because that item is filtered out using the filter
option.
<!-- List Group -->
<div class="js-sortable list-group"
data-hs-sortable-options='{
"animation": 150,
"filter": ".sortablejs-custom-disabled"
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item sortablejs-custom-disabled bg-danger text-white">Filtered</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
Grid
<!-- List Group -->
<div class="js-sortable row"
data-hs-sortable-options='{
"animation": 150,
"ghostClass": "sortablejs-custom-chosen-child"
}'>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">1</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">2</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">3</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">4</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">5</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">6</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">7</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">8</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">9</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">10</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">11</div>
</div>
</div>
<div class="col-md-2 mb-3">
<div class="card">
<div class="card-body text-center">12</div>
</div>
</div>
</div>
<!-- End List Group -->
Nested
Item 3
<!-- List Group -->
<div class="js-sortable list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup4",
"fallbackOnBody": true
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">
<h4 class="h5">Item 3</h4>
<div class="js-sortable list-group"
data-hs-sortable-options='{
"animation": 150,
"group": "listGroup5"
}'>
<div class="list-group-item">Item 3.1</div>
<div class="list-group-item">Item 3.2</div>
<div class="list-group-item">Item 3.3</div>
<div class="list-group-item">Item 3.4</div>
</div>
</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
Multiple Drag
The MultiDrag option allows for multiple items to be dragged at a time. You can click to "select" multiple items, and then drag them as one item.
Use "multiDrag": true
<!-- List Group -->
<div class="js-sortable list-group"
data-hs-sortable-options='{
"animation": 150,
"multiDrag": true,
"selectedClass": "active"
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
Swap
The Swap option changes the behaviour of Sortable to allow for items to be swapped with eachother rather than sorted.
Use "swap": true
<!-- List Group -->
<div class="js-sortable list-group"
data-hs-sortable-options='{
"animation": 150,
"swap": true,
"swapClass": "active"
}'>
<div class="list-group-item">Item 1</div>
<div class="list-group-item">Item 2</div>
<div class="list-group-item">Item 3</div>
<div class="list-group-item">Item 4</div>
<div class="list-group-item">Item 5</div>
<div class="list-group-item">Item 6</div>
</div>
<!-- End List Group -->
Links
<!-- List Group -->
<div class="js-sortable list-group">
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 1</a>
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 2</a>
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 3</a>
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 4</a>
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 5</a>
<a class="js-sortable list-group-item sortablejs-custom-handle" href="#">Sortable with link 6</a>
</div>
<!-- End List Group -->
Data-href with modal example
Modal title
<!-- List Group -->
<div class="js-sortable list-group">
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 1</a>
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 2</a>
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 3</a>
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 4</a>
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 5</a>
<a class="js-sortable-modal-link list-group-item sortablejs-custom-handle" data-bs-toggle="modal" data-bs-target="#exampleModal" href="javascript:;">Sortable with modal link 6</a>
</div>
<!-- End List Group -->
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- End Modal -->
Methods
Parameters | Description | Default value |
---|---|---|
|
Class when draging. | sortable-chosen-main |