Restrict dragging to the same level only

Hi There,

Is there a way to restrict dropping of nodes in a tree view to only the same level? 
In the example below, the user can only drag Promotions within the Categories node, but not above it.



5 Replies 1 reply marked as answer

SP Sowmiya Padmanaban Syncfusion Team November 20, 2020 10:09 AM UTC

Hi Joseph Tan,  
 
Greetings from Syncfusion support. 
 
You can achieve your requirement by adding the additional data field in the data source of TreeView component and prevent the dragging using OnNodeDragStop event by setting args.cancel as true. 
 
Please refer the below code snippet for the above suggested solution. 
 
<SfTreeView @ref="tree" TValue="MailItem" AllowDragAndDrop="true"> 
    <TreeViewEvents TValue="MailItem" OnNodeDragStop="dragStop"></TreeViewEvents> 
    <TreeViewFieldsSettings TValue="MailItem" Id="Id" DataSource="@MyFolder" Text="FolderName" ParentID="ParentId" HasChildren="HasSubFolders" Expanded="Expanded"></TreeViewFieldsSettings> 
</SfTreeView> 
public class MailItem 
    { 
        public string Id { get; set; } 
        public string ParentId { get; set; } 
        public string FolderName { get; set; } 
        public bool Expanded { get; set; } 
        public bool HasSubFolders { get; set; } 
        public string level { get; set; } 
    } 
protected override void OnInitialized() 
    { 
        base.OnInitialized(); 
        MyFolder.Add(new MailItem 
        { 
            Id = "1", 
            FolderName = "Inbox", 
            HasSubFolders = true, 
            level ="level1" 
        }); 
}  
 public void dragStop(Syncfusion.Blazor.Navigations.DragAndDropEventArgs args) 
    { 
        // get the dropped node data 
        var dropped_node = this.tree.GetTreeData(args.DroppedNodeData.Id); 
        //get the dragged node data 
        var dragged_node = this.tree.GetTreeData(args.DraggedNodeData.Id); 
        // compare the drag and drop level and prevent the drop of that particular node. 
        if(dragged_node[0].level != dropped_node[0].level || args.DropIndicator != "e-drop-next" ) 
        { 
            args.Cancel = true; 
        } 
    } 
 
Please, refer the below sample link. 
 
 
Please, refer to the below links to know more about the TreeView component. 
 
 
 
 
Please let us know, if you need any further assistance. 
 
Regards,  
Sowmiya.P 



JT Joseph Tan November 20, 2020 09:20 PM UTC

Hi There,

So I tried it and at first it didn't work. But after I upgraded to 18.3.50, it did.

This is bleeding edge stuff ! Thanks!

Cheers
Joseph 


SP Sowmiya Padmanaban Syncfusion Team November 23, 2020 06:23 AM UTC

Hi Joseph Tan,  
 
Sorry for the inconvenience. 
 
We have resolved the drop event issue ( args.cancel is not working in 18.3.48 ) in our latest patch release(18.3.50). So, we suggest you to use the latest (Syncfusion.Blazor)package version (18.3.50) in your application. 
 
Please let us know, if you need any further assistance. 
 
Regards,  
Sowmiya.P 


Marked as answer

JT Joseph Tan November 23, 2020 10:17 PM UTC

Yep - that did it.


SP Sowmiya Padmanaban Syncfusion Team November 24, 2020 04:44 AM UTC

Hi Joseph,  
  
We are happy to hear that your problem has been resolved. Please contact us, if you need any help from us. 
  
Regards,  
Sowmiya.P 


Loader.
Up arrow icon