Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement drag and drop in Blazor?

Tags:

html

c#

blazor

I know Blazor is a new technology. Its current release states v0.5.1

However I'm currently implementing a PoC for a new web application. We'd like to have the feature drag&drop in the app. I tried to implement it the Blazor way but it does not work.

My droptarget:

<div class="col" ondragstart="@AllowDragOver" ondrop="@Add">

And the draggable item:

<span class="badge badge-warning" draggable="true">îtem 1</span>

The Blazor C# code:

@functions {

void Add()
{
     Items.Add("hello");
}

void AllowDragOver(UIDragEventArgs e)
{
}

}

The problem is that the drop target does not show in the browser as a drop target:

Drag Drop in browser

What I've read so far is that when attaching an event handler to a Blazor C# function (e.g. ondragstart) than the default behavior is the well-known "e.preventDefault()" which should make the drop target droppable.

Does anyone know how to fix this?

Sven

like image 993
Sven Avatar asked Sep 20 '18 12:09

Sven


People also ask

How do you drag and drop in JavaScript?

Introduction to JavaScript Drag and Drop API To drag an image, you simply hold the mouse button down and then move it. To drag the text, you need to highlight some text and drag it in the same way as you would drag an image.

Is Blazor slow?

Blazor projects are slow on the client-side because you have to download the entire dot net runtime along with the necessary DLL libraries on your browser. Additionally, Blazor apps have latency issues.

How do I add a script tag to Blazor?

Inject a script after Blazor starts cshtml (Blazor Server) when the app is initialized: Add autostart="false" to the <script> tag that loads the Blazor script. Inject a script into the <head> element markup that references a custom JS file after starting Blazor by calling Blazor. start().

Which is better Blazor server or Blazor WebAssembly?

The Blazor Server hosting model offers several benefits: Download size is significantly smaller than a Blazor WebAssembly app, and the app loads much faster. -The app takes full advantage of server capabilities, including the use of . NET Core APIs.


1 Answers

I'd like to post my solution.

What I've found out so far is that dataTransfer.setData currently does not work in Blazor 0.5.1. I can workaround this with saving the dragged element in a shared C# class that is injected as a DI service. The container needs to have the call to "e.preventDefault()" to be a valid drop target. Though this is not possible in C# you easily can call this as pure Javascript:

<div class="col-sm-1" dropzone="move" ondragenter="@(e => OnContainerDragEnter(e))" ondragover="event.preventDefault();" ondragleave="@(e => OnContainerDragLeave(e))"
 ondrop="@(e => OnContainerDrop(e, Date))" style="@_highlightColor;@_highlightDropTargetStyle">

Drag and drop works pretty good with C# and very smooth without flickering and interruptions. I will create a simple working example in the next days.

UPDATE:

Sorry, my example is already outdated and currently I haven't enough time to maintain this demo along with its source. So I'd like to add a link to a working demo from Chris Sainty: https://chrissainty.com/investigating-drag-and-drop-with-blazor/

like image 166
Sven Avatar answered Sep 21 '22 21:09

Sven