Skip to Content
Author's profile photo Stephanie Lemoine

How to use drag and drop between two SAPUI5 UI elements

In this tutorial I will explain how you can implement drag and drop in SAPUI5. We’ll create a list from which we can drag the list items into a table. The details of the list item will be added as a row in the table. For a demo, look at the following video:

JQuery UI provides easy functionalities to implement drag and drop. To access these functionalities we need to import these libraries to our project. To to so, add the following code to your index page:


<script>
  jQuery(function() {
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-core');
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-widget');
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-mouse');
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-sortable');
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-droppable');
     $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-draggable');
    
     sap.ui.localResources("sapui5draganddrop");
  var app = new sap.m.App();
  var page = sap.ui.view({id:"iddetail1", viewName:"sapui5draganddrop.detail", type:sap.ui.core.mvc.ViewType.XML});
  app.addPage(page);
  app.placeAt("content");
    
  });
  </script>

My code in my view is the following:


<HBox>
  <List id="dragableList" width="200px" class="dragList" items="{materials>/}">
       <items>
            <StandardListItem title="{materials>description}" />
       </items>
  </List>
  <Table id="dropableTable" inset="false" width="500px" class="table">
       <columns>
            <Column hAlign="Center">
                 <Text text="Material" />
       </Column>
       <Column hAlign="Center">
            <Text text="Vendor" />
       </Column>
       </columns>
            <items>
            </items>
    </Table>
  </HBox>

In an HBox I added the list and the table. Make sure you give both the list and the table an ID, we need this in the controller.

In our controller, we will implement our code in the onInit() function. First part is general coding where I define my variables and set my model:


onInit: function() {
  var oSortableList = this.getView().byId("dragableList");
  var listId = oSortableList.getId();
  var listUlId = listId + "-listUl";
  var materialModel = new sap.ui.model.json.JSONModel();
  var oDropableTable = this.getView().byId("dropableTable");
  var tableId = oDropableTable.getId();
  var materials = [
  {id: "0", description: "material 1", vendor: "vendor 1"},
  {id: "1", description: "material 2", vendor: "vendor 1"},
  {id: "2", description: "material 3", vendor: "vendor 2"},
  {id: "3", description: "material 4", vendor: "vendor 2"}
  ];
  materialModel.setData(materials);
  this.getView().setModel(materialModel, "materials");

The first step we need to do is make the list drag-able, you can do this with the following code:


oSortableList.onAfterRendering = function() {
         if (sap.m.List.prototype.onAfterRendering) {
             sap.m.List.prototype.onAfterRendering.apply(this);
         }
  $("#"+listUlId+" li").draggable({
         helper: "clone"
       });
     }

When you implemented this code you’ll see that you can drag the list items, but when you drop them, nothing happens. For that we need to make our table drop-able:


oDropableTable.onAfterRendering = function() {
         if (sap.m.Table.prototype.onAfterRendering) {
             sap.m.Table.prototype.onAfterRendering.apply(this);
         }
        
  $("#"+tableId).droppable({
       drop: function( event, ui ) {
       var listElementId = ui.draggable.context.id;
       var draggedElement = sap.ui.getCore().byId(listElementId);
       var oPath = draggedElement.getBindingContext("materials").getPath();
       var split = oPath.split("/");
   var i = split[1];
  
   var materialData = materialModel.getData();
      
       oDropableTable.addItem(
          new sap.m.ColumnListItem({
          cells: [
                 new sap.m.Text({text: materialData[i].description}),
                 new sap.m.Text({text: materialData[i].vendor})
                 ]
          })
         );
       }
  })
     }

When the drop event is triggered, we can access the ID of our list item which we have dropped on the table. With this ID we can access the list item, retrieve the binding context and the path. And with this path we can read out the correct line from our material data. We then add this data as an item to our table.

And that’s how easy it is to implement drag and drop functionalities in SAPUI5.

Remark: it does seem that the two elements need to be in the same element (in this case the HBox). I tested to see if it is for example possible to drag in a splitapp from the master view to the detail view but this doesn’t seem to be possible. So there are some limitations.

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sai Vellanki
      Sai Vellanki

      Good one Stephanie, Thanks for sharing!

      Regards,

      Sai Vellanki.

      Author's profile photo Neil Gardiner
      Neil Gardiner

      Hi Stephanie,

      Are you able to add code snippets rather than screen shots? This makes it easier for others who wish to C&P the code if they want to try the solution for themselves.

      Cheers,

      Neil

      Author's profile photo Stephanie Lemoine
      Stephanie Lemoine
      Blog Post Author

      Hi Neil,

      I replaced the images with code snippets. Didn't know how to do that when I wrote this blog :-).

      Cheers,

      Stephanie

      Author's profile photo Former Member
      Former Member

      Hello. How to activate drag&drop functionality not in onInit method? It does not work for instance inside of standard method onAfterRendering or onAfterShow

      Author's profile photo Former Member
      Former Member

      I am having trouble recreating this in Web IDE.  Can you show the whole index,htnl file, I think that the $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-core'); line is not happening until after oSortableList.onAfterRendering leading to an error on $("#"+listUlId+" li").draggable({

      Object doesn't support property or method 'draggable'

       

      my index.html

       

      <!DOCTYPE HTML>
      <html>

      <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta charset="UTF-8">

      <title>TestDD</title>

      <script>
      </script>

      <script id="sap-ui-bootstrap"
      src="../../resources/sap-ui-core.js"
      data-sap-ui-libs="sap.m"
      data-sap-ui-theme="sap_belize"
      data-sap-ui-compatVersion="edge"
      data-sap-ui-resourceroots='{"TestDDTestDD": ""}'>
      </script>

      <link rel="stylesheet" type="text/css" href="css/style.css">

      <script>
      debugger;
      jQuery(function() {
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-core');
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-widget');
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-mouse');
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-sortable');
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-droppable');
      $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-draggable');

      });

      sap.ui.getCore().attachInit(function() {
      new sap.m.Shell({
      app: new sap.ui.core.ComponentContainer({
      height : "100%",
      name : "TestDDTestDD"
      })
      }).placeAt("content");
      });
      </script>
      </head>

      <body class="sapUiBody" id="content">
      </body>

      </html>

       

      my controller

       

      sap.ui.define([
      "sap/ui/core/mvc/Controller"
      ], function(Controller) {
      "use strict";

      return Controller.extend("TestDDTestDD.controller.sapui5draganddrop", {

      onInit: function() {
      debugger;
      var oSortableList = this.getView().byId("dragableList");
      var listId = oSortableList.getId();
      var listUlId = listId + "-listUl";
      var materialModel = new sap.ui.model.json.JSONModel();
      var oDropableTable = this.getView().byId("dropableTable");
      var tableId = oDropableTable.getId();
      var materials = [
      {id: "0", description: "material 1", vendor: "vendor 1"},
      {id: "1", description: "material 2", vendor: "vendor 1"},
      {id: "2", description: "material 3", vendor: "vendor 2"},
      {id: "3", description: "material 4", vendor: "vendor 2"}
      ];
      materialModel.setData(materials);
      this.getView().setModel(materialModel, "materials");

      oSortableList.onAfterRendering = function() {
      debugger;
      if (sap.m.List.prototype.onAfterRendering) {
      sap.m.List.prototype.onAfterRendering.apply(this);
      }

      $("#"+listUlId+" li").draggable({
      helper: "clone"
      });

      }

      }
      });
      });

       

       

      Author's profile photo Former Member
      Former Member

      Hi Stephanie,

       

      I am sure it is quite late(2017 as the question was posted in 2014), Is it possible to share the full code for controller.js file.

       

      Regards,

      Jaic

      Author's profile photo Ransome Mathias
      Ransome Mathias

      Stephanie, this is really cool example on how to use Jquery. I loved it. Thank you for sharing.

       

      Author's profile photo Ransome Mathias
      Ransome Mathias

      Hi Stephanie and or Fiori expert (who can take my below question),

      I have one quick question. I wanted to ask how did you manage to figure out that we need to use the below call. In the sdk i dont seem to find the mehtod prototype in the sap.mTable control. I would really appreciate your inputs in how to access these methods or use the sdk documentaion.

      sap.m.Table.prototype.onAfterRendering.apply(this);

      Author's profile photo Jeen Lal
      Jeen Lal

      Hi Stephanie,

      It’s working fine. Here I requires an additional requirement that i need to drag from Table to List (Vice-versa). How can we merge both functionality together.

      Author's profile photo Dumpala Suneetha
      Dumpala Suneetha

      Hi Jeen,

      if you got your requirement/answer, can you please post that here?