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:
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
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.
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.
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().
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With