🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

BlazorSortable

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

BlazorSortable

A Blazor component that wraps the SortableJS library to provide interactive, drag-and-drop list reordering functionality.

2.1.0
NuGet
Version published
Maintainers
1
Created
Source

BlazorSortable

BlazorSortable icon

NuGet Version NuGet Downloads

A Blazor component that wraps the SortableJS library, designed for creating interactive sortable lists with drag-and-drop support. Inspired by BlazorSortable and represents an improved and extended implementation.

Installation

Via dotnet CLI

dotnet add package BlazorSortable

Via PackageReference

Add to your .csproj file:

<ItemGroup>
  <PackageReference Include="BlazorSortable" Version="2.*" />
</ItemGroup>

Setup

  • Add the SortableJS library to:

    • Components/App.razor (for Blazor Web App)
    • Pages/_Host.cshtml (for Blazor Server)
    • wwwroot/index.html (for Blazor WebAssembly)

    Using one of the following methods:

    a) Via CDN:

    <script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
    

    b) Locally:

    <script src="lib/sortable/dist/js/Sortable.min.js"></script>
    

    For local installation:

    • Download the latest version of SortableJS from GitHub
    • Create the folder structure in wwwroot: lib/sortable/dist/js/
    • Place the Sortable.min.js file in the created folder
    • Ensure the path in the script tag matches the file location
  • (Optional) Add base styles to the same file where you added the script:

<link rel="stylesheet" href="_content/BlazorSortable/css/blazor-sortable.css" />
  • Add services in Program.cs:
using BlazorSortable;

// ...

builder.Services.AddSortableServices();
  • Add the using directive in _Imports.razor:
@using BlazorSortable

Usage Examples

SortableList

With a component

<SortableList Items="Persons"
              Class="my-sortable-list"
              Group="group1">
    <PersonComponent Person="context" />
</SortableList>

With inline HTML content

<SortableList Items="Persons"
              Class="my-sortable-list"
              Group="group1"
              Context="person">
    <div class="person-card">
        <h4>@person.Name</h4>
        <p>@person.Email</p>
        <span class="badge">@person.Department</span>
    </div>
</SortableList>

SortableDropZone

<SortableDropZone Class="my-sortable-drop-zone"
                  Group="group1"
                  OnDrop="HandleDrop" />

ConvertersBuilder

For convenient creation of converters dictionary, the ConvertersBuilder class is used. It allows defining transformations for different element types in a method chain:

// Creating a converters dictionary
var converters = new ConvertersBuilder<Employee>()
    .Add<Person>("personList", p => new Employee { Name = p.FullName })
    .Add<Student>("studentList", s => new Employee { Name = s.Name });

// Using in component
<SortableList Items="Employees"
              Converters="converters"
              Group="group1">
    <ItemComponent Item="context" />
</SortableList>

Events

Events use Action<T> instead of EventCallback<T>. Reason: EventCallback.InvokeAsync automatically triggers StateHasChanged() in the parent component. For this component, this causes conflicts between DOM and data model.

Component Parameters

SortableList

ParameterTypeDefaultDescription
TItemThe type of items in the sortable list
ItemsIList<TItem>RequiredList of items to display and sort
ClassstringnullCSS class for the container
StylestringnullInline CSS styles for the container
ChildContentRenderFragment<TItem>nullTemplate for displaying each list item. Can be a component, HTML elements, or any Razor markup
KeySelectorFunc<TItem, object>nullFunction for generating the key used in @key. If not set, the item itself is used
IdstringRandom GUIDUnique identifier of the component. Used internally for coordination between Sortable components and for identifying lists in converters. Must be globally unique
GroupstringRandom GUIDName of the group for interacting with other sortable instances
PullPullModenullMode for pulling items from the list
PullGroupsstring[]nullRequired when Pull="PullMode.Groups". Specifies the groups into which items from this list can be dragged
CloneFactoryFunc<TItem, TItem>nullRequired when Pull="PullMode.Clone". A factory method used to create a clone of the dragged item
OnCloneExceptionAction<Exception>nullRaised when an exception occurs during object cloning
PutPutModenullMode for adding items to the list
PutGroupsstring[]nullRequired when Put="PutMode.Groups". Specifies the groups from which items are allowed to be added
ConvertersDictionary<string, Func<object, TItem>>nullDictionary of converters: the key is the Id of another SortableList, and the value is a function that converts an item from that list to TItem
OnConvertExceptionAction<Exception>nullRaised when an exception occurs during item conversion
SortbooltrueEnables or disables sorting of items within the list
Animationint150Animation duration in milliseconds
HandlestringnullCSS selector for elements that can be dragged (e.g. ".my-handle")
FilterstringnullCSS selector for elements that cannot be dragged (e.g. ".ignore-elements")
GhostClassstring"sortable-ghost"CSS class for the placeholder during drag
ChosenClassstring"sortable-chosen"CSS class for the chosen element
DragClassstring"sortable-drag"CSS class for the dragged element
ForceFallbackbooltrueForces fallback mode for dragging
FallbackClassstring"sortable-fallback"CSS class for the element in fallback mode
OnAddAction<(TItem item, string sourceId, bool isClone, int oldIndex, int newIndex)>nullRaised when an item is added to the list
OnUpdateAction<(TItem item, int oldIndex, int newIndex)>nullRaised when the order of items is changed
OnRemoveAction<(TItem item, int index)>nullRaised when an item is removed from the list

SortableDropZone

ParameterTypeDefaultDescription
ClassstringnullCSS class for the container
StylestringnullInline CSS styles for the container
GroupstringRandom GUIDName of the group for interacting with other sortable instances
PutPutModenullMode for adding items to the zone
PutGroupsstring[]nullRequired when Put="PutMode.Groups". Specifies the groups from which items are allowed to be added
GhostClassstring"sortable-ghost"CSS class for the placeholder during drag
OnDropAction<(object item, string sourceId, bool isClone, int index)>nullRaised when an item is dropped in the zone

PullMode

ValueDescription
TrueAllows pulling items from the list
FalseProhibits pulling items from the list
GroupsAllows pulling items only from specified groups (requires PullGroups parameter)
CloneCreates a clone of the item when dragging (requires CloneFactory parameter)

PutMode

ValueDescription
TrueAllows adding items to the list
FalseProhibits adding items to the list
GroupsAllows adding items only from specified groups (requires PutGroups parameter)

Keywords

blazor

FAQs

Package last updated on 09 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts