Skip to content

neurodance/Blazor.DragDrop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blazor.DragDrop

NuGet Version

This component provides simple and user-friendly drag and drop functionality for Blazor applications.

Features

  • Drag and drop items within a list, both horizontally and vertically.
  • Drag and drop items between multiple lists. (Coming soon)
  • Choose any HTML element to serve as the parent.
  • Use a drag handle to drag items only by the handle.
  • Fully customizable item template.
  • Animated movement of items.
  • Support for both desktop and mobile devices.
  • And more...

How to set up

  1. Install the RMN.Blazor.DragDrop NuGet package in your project.

  2. Add the component namespace to your _Imports.razor file:

@using RMN.Blazor.DragDrop
  1. Add SortableJS:
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>

API

Parameters

Name Type Default Description
Items List<TItem> [ ] List of items.
OrderPropertyName String "" Item's order property to update after reordering the list.
RootElement String "div" Element that will serve as the parent.
Id String Guid Id for the parent element.
Class String "" CSS classes for the parent element.
Style String "" Inline styles for the parent element.
DragHandleClass String "" CSS class for the drag handle.
UndraggableItemClass String "" CSS class for undraggable items.
AllowDragging Boolean true Enables or disables dragging of all items.
AllowReorder Boolean true Enables or disables reordering the list.
Context String context Parameter name for the list items.

Events

OnUpdate: The method to be called after reordering the list.

Examples

Basic example

<DragDropList Items="Items" Context="item">

    <p>@item.Name</p>

</DragDropList>

Advanced example 1

<DragDropList Items="Items"
              RootElement="ul"
              DragHandleClass="drag-handle"
              Context="item"
              OnUpdate="OnListUpdate">

    <li>
        <i class="fa-solid fa-grip-vertical drag-handle"></i>
        <span>@item.Name</span>
    </li>

</DragDropList>

Advanced example 2

<DragDropList Items="Items.OrderBy(x => x.Order).ToList()"
              OrderPropertyName="Order"
              RootElement="tbody"
              UndraggableItemClass="undraggable-item"
              Context="item"
              OnUpdate="OnListUpdate">

    <tr class="@(item.Disabled ? "undraggable-item" : null)">
        <td>@item.Name</td>
    </tr>

</DragDropList>

For updating the order property of your items after reordering the list, you have 2 options:

  1. Specify the order property name as shown in the example above.

  2. Update manually:

public async Task OnListUpdate() 
{
    Items.ForEach(x => x.Order = Items.IndexOf(x));

    // Saving to database or something else
}

Styling

For styling the item that is being dragged, use the class selector .dragging-item.

License

Project is licensed under the MIT license.

About

Simple drag and drop component for Blazor.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 79.6%
  • JavaScript 16.1%
  • HTML 4.3%