Search code examples
jqueryjquery-ui-sortablesapui5

How to handle Drag and drop from ListBox in SAPUI5?


I could find that drag and drop are supported in SAPUI5. But I am not able to implement the same in my app. I tried to bind to the dragstart and dragleave events, they are not working.

I even tried to example provided in the other threads(http://jsbin.com/qova/2/edit?html,output). This example is also not working. I can select the list item, but when I try to drag, the selection just extends and nothing happens.

selection extends

Please let me know if I am doing anything wrong.

Here is the HTML Snapshot

enter image description here

Source code

    <!DOCTYPE html>
<html>
<head>
<meta name="description" content="OpenUI5 Listbox Drop and Drag" />
<meta http-equiv='X-UA-Compatible' content='IE=edge' />  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Drag and Drop List Test</title>
<script id='sap-ui-bootstrap' 
    src='https://openui5.hana.ondemand.com/resources/sap-ui-core.js'
    data-sap-ui-theme="sap_goldreflection"
    data-sap-ui-libs="sap.ui.commons">  
</script>

<script type="text/javascript">
    $.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-draggable');
    $.sap.require('sap.ui.thirdparty.jqueryui.jquery-ui-sortable');

    $(function() {
        $("#lb1-list, #lb2-list").sortable({
            connectWith : ".ui-sortable"
        }).disableSelection();
    });

    var city1 = "Berlin|London|New York|Paris|Amsterdam", 
        city2 = "Sydney|Melbourne|Brisbane|Perth|Wollongong";

    var oListBox1 = new sap.ui.commons.ListBox( "lb1", {
        items : $.map(city1.split("|").sort(), function(v, i) {
            return new sap.ui.core.ListItem({ text : v  });
        }), height : "150px"
    });

    var oListBox2 = new sap.ui.commons.ListBox("lb2", {
        items : $.map(city2.split("|").sort(), function(v, i) {
            return new sap.ui.core.ListItem({text : v });
        }), height : "150px"
    });

    var oLayout = new sap.ui.commons.layout.MatrixLayout({layoutFixed : false})
    oLayout.createRow(oListBox1, oListBox2).placeAt("content");
</script>

</head>
<body id="body" class="sapUiBody">
    <div id="content"></div>
</body>
</html>

Update: The solution works fine if the list is static. But for dynamic lists, where we add rows via code, SAPUI5 re-renders the list and calls remove attributes. The remove attributes call the jQuery-UI remove attributes and removes the CSS Class attributes. Once I made the list items static, the drag drop is working fine.

Is there a solution for drag-drop when the list is dynamic?

Found one solution Please note, this solution is for UI5 applications created with separate views and controllers.

For dynamic lists, the jquery-ui draggable has to called in onAfterRendering. Otherwise, the classes added by jquery-ui will be removed once the list re-renders.

For inline UI5 apps like the one I posted, we can try adding "onAfterRendering" event delegate to list controls.


Solution

  • Found one solution:

    Please note, this solution is for UI5 applications created with separate views and controllers.

    For dynamic lists, the jquery-ui draggable has to called in onAfterRendering of the controller. Otherwise, the classes added by jquery-ui will be removed once the list re-renders.

    For inline UI5 apps like the one in the question I posted, we can try adding "onAfterRendering" event delegate to list controls.