SortableList
A SortableJS library adaptation for Blazor Fluent UI. Allows for reordering elements within a list using drag and drop. Inspired by and based on Burke Holland's article and code. Re-used with permission.
The FluentSortableList
is a generic component that takes a list of items and a ItemTemplate
that defines how to render each item in the sortable list.
How to use this in your own project
If you want to use the FluentSortableList
component, you will need to include the script to your index.html
/_Layout.cshtml
/App.razor
file. You can either download from the SortableJS website or use a CDN:
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
We do not include the needed Sortable script in the library.
See the examples below what is needed to use the component in a page.
The component does not actually do any sorting or moving of items. It simply provides the hooks to do so. You will need to handle all events yourself. If you don't handle any events, no sort or move will happen as Blazor needs to make the changes to the underlying data structures so it can re-render the list.
Here is an example of how to reorder your list when the OnUpdate is fired...
private void SortList(FluentSortableListEventArgs args)
{
if (args is null || args.OldIndex == args.NewIndex)
{
return;
}
var oldIndex = args.OldIndex;
var newIndex = args.NewIndex;
var items = this.items;
var itemToMove = items[oldIndex];
items.RemoveAt(oldIndex);
if (newIndex < items.Count)
{
items.Insert(newIndex, itemToMove);
}
else
{
items.Add(itemToMove);
}
}
Examples
Simple sortable list
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Download:
Move items between two lists
Shared lists are lists where items can be dragged from one list to the other and vice-versa. Providing the same "Group" string name for
both lists is what links them together.
Note: When an item is dragged into a different list, it assumes the visual style of that list. This is because Blazor controls the rendering of the list items.
Note: When an item is dragged into a different list, it assumes the visual style of that list. This is because Blazor controls the rendering of the list items.
List 1
List 2
Download:
Clone items
Cloning is enabled by setting the
Clone
parameter to `true`. This allows cloning of an item by dropping it into a shared list.
List 1
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
List 2
Item 11
Item 12
Item 13
Item 14
Item 15
Item 16
Item 17
Item 18
Item 19
Item 20
Download:
Disabling sorting
You can disable sorting with the
Sort
parameter set to `false`. You can also disable dropping items on a list by setting the Drop
parameter to `false`. In the example below, you can drag from list 1 to list 2, but not from list 2 to list 1. You can sort list 2, but not list 1.
List 1
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
List 2
Item 11
Item 12
Item 13
Item 14
Item 15
Item 16
Item 17
Item 18
Item 19
Item 20
Download:
Drag Handles
When setting the
Handle
parameter to true, the items can only be sorted using the drag handle itself. The following CSS classes can be used to split the drag functionality from the content:
- sortable-grab: the grabbable part of the draggable item
- sortable-item-content: the content part of the draggable item
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Download:
Filtering
In the lists below, you cannot drag the item in the accented color. This is because these items are filtered out with an
ItemFilter
parameter (of type Func<TItem, bool>
).
The ItemFilter
parameter is a function that takes an item and returns a boolean value. If the function returns true, the item is excluded from dragging in the list. If the function returns false, the item can be dragged.
In the left list below, the ItemFilter
parameter is set to filter out a random item from the list. In the right list, the ItemFilter
parameter is set to filter out items with an Id larger than 6.
See the Razor tab for how the different functions are being specified.
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Download:
Sortable list using fallback behavior
By setting Fallback parameter to true, the list will not use native HTML5 drag and drop behavior.
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10
Download:
Documentation
FluentSortableListEventArgs Class
Properties
Name | Type | Default | Description |
---|---|---|---|
FromListId | string | Gets the id of the list the item was in before the update. May be null if no Id was set for FluentSortableList | |
NewIndex | int | 0 | Gets the index of the item in the list after the update. |
OldIndex | int | 0 | Gets the index of the item in the list before the update. |
ToListId | string | Gets the id of the list the item is in after the update. May be null if no Id was set for FluentSortableList |
FluentSortableList<TItem> Class
Parameters
Name | Type | Default | Description |
---|---|---|---|
Clone | bool | False | Gets or sets whether elements are cloned instead of moved. Set Pull to 'clone' to enable this. |
Drop | bool | True | Gets or sets wether it is possible to drop items into the current list from another list in the same group. Set to false to disable dropping from another list onto the current list. |
Fallback | bool | False | Gets or sets wether to ignore the HTML5 DnD behaviour and force the fallback to kick in |
Group | string? | Gets or sets the name of the Group used for dragging between lists. Set the group to the same value on both lists to enable. You can only have 1 group with 2 lists. | |
Handle | bool | False | Gets or sets whether the whole item acts as drag handle. Set to true to use an element with classname `.sortable-grab` as the handle. |
ItemFilter | Func<TItem, bool>? | Gets or sets the function to filter out elements that cannot be sorted or moved. | |
Items | IEnumerable<TItem>? | Gets or sets the list of items to be displayed in a sortable list. | |
ItemTemplate | RenderFragment<TItem>? | Gets or sets the template to be used to define each sortable item in the list. Use the @context parameter to access the item and its properties. | |
ListBorderColor | string? | Gets or sets the color of the border on the list. | |
ListBorderWidth | string? | Gets or sets the border width on the list. Must be a valid CSS measurement. | |
ListItemBackgroundColor | string? | Gets or sets the background color of the list items. | |
ListItemBorderColor | string? | Gets or sets the border color of the list items. | |
ListItemBorderWidth | string? | Gets or sets the border width on the list items. Must be a valid CSS measurement. | |
ListItemDropBorderColor | string? | Gets or sets the border color of the list items during repositioning. | |
ListItemDropColor | string? | Gets or sets the background color of the list items during repositioning. | |
ListItemFilteredColor | string? | Gets or sets the color of filtered list items. | |
ListItemHeight | string? | Gets or sets the height of the list items. Must be a valid CSS measurement. | |
ListItemPadding | string? | Gets or sets the padding on the list items. Must be a valid CSS measurement. | |
ListItemSpacing | string? | Gets or sets the spacing between list items. Must be a valid CSS measurement. | |
ListPadding | string? | Gets or sets the padding on the list. Must be a valid CSS measurement. | |
Sort | bool | True | Gets or sets whether the list is sortable. Default is true Disable sorting within a list by setting to false. |
EventCallbacks
Name | Type | Description |
---|---|---|
OnRemove | EventCallback<FluentSortableListEventArgs> | Event callback for when an item is removed from the list. |
OnUpdate | EventCallback<FluentSortableListEventArgs> | Event callback for when the list is updated. |
Methods
Name | Parameters | Type | Description |
---|---|---|---|
OnRemoveJS | int oldIndex int newIndex string fromListId string toListId | void | |
OnUpdateJS | int oldIndex int newIndex string fromListId string toListId | void |