Friday, September 20, 2019

jQuery UI Droppable

jQuery UI facilitates you to use droppable() method to make any DOM element droppable at a specified target.

Syntax:
You can use the droppable () method in two forms:

  1. 1.  $(selector, context).droppable (options) Method  
  1. 2.  $(selector, context).droppable ("action", params) Method   

First Method

The droppable (options) method specifies that you can use an HTML element as an element in which you can drop other elements. Here, the option parameter specifies the behavior of the elements involved.
Syntax:
  1. $(selector, context).droppable (options);  
You can use one or more options at a time using JavaScript object. If you are using more than one option then will have to separate them by using a comma. For example:
  1. $(selector, context).droppable({option1: value1, option2: value2..... });  
Following is a list of the different options that can be used with this method:
OptionDescription
acceptThis option is used when you need to control which draggable elements are to be accepted for dropping. by default its value is *.
activeclassThis option is a string representing one or more css classes to be added to the droppable element when an accepted element (one of those indicated in options.accept) is being dragged. by default its value is false.
addclassesThis option when set to false will prevent the ui-droppable class from being added to the droppable elements. by default its value is true.
diasabledThis option when set to true disables the droppable. by default its value is false.
greedyThis option is used when you need to control which draggable elements are to be accepted for dropping on nested droppables. by default its value is false. if this option is set to true, any parent droppables will not receive the element.
hoverclassThis option is string representing one or more css classes to be added to the element of droppable when an accepted element (an element indicated in options.accept) moves into it. by default its value is false.
scopeThis option is used to restrict the droppable action of draggable elements only to items that have the same options.scope (defined in draggable (options)). by default its value is "default".
tolerenceThis option is a string that specifies which mode to use for testing whether a draggable is hovering over a droppable. By default its value is "intersect".

jQuery UI Droppable() method example 1

Let's take an example to demonstrate the jQuery UI Droppable() method with no parameter.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          #draggable-1 {   
  11.            width: 100px; height: 100px; padding: 0.5em; float: left;  
  12.            margin: 22px 5px 10px 0;   
  13.          }  
  14.          #droppable-1 {   
  15.             width: 150px; height: 120px;padding: 0.5em; float: left;   
  16.            margin: 10px;      
  17.          }  
  18.       </style>  
  19.       <script>  
  20.          $(function() {  
  21.             $( "#draggable-1" ).draggable();  
  22.             $( "#droppable-1" ).droppable();  
  23.          });  
  24.       </script>  
  25.    </head>  
  26.    <body>  
  27.       <div id="draggable-1" class="ui-widget-content">  
  28.          <p>I am SSSIT.org<br/> Drag me to JavaTpoint</p>  
  29.       </div>  
  30.       <div id="droppable-1" class="ui-widget-header">  
  31.          <p>Welcome to JavaTpoint</p>  
  32.       </div>  
  33.    </body>  
  34. </html>  

jQuery UI Droppable() method example 2

b) How to use addClass, disabled and tolerance:
Following example specifies how to use these three options addClass, disable and tolerance in the drop function of jQuery UI.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          #draggable-2 {   
  11.            width: 100px; height: 50px; padding: 0.5em;   
  12.            margin: 0px 5px 10px 0;        
  13.          }  
  14.           #droppable-2,#droppable-3, #droppable-4,#droppable-5 {   
  15.            width: 120px; height: 90px;padding: 0.5em; float: left;   
  16.            margin: 10px;   
  17.          }  
  18.            
  19.       </style>  
  20.       <script>  
  21.          $(function() {  
  22.             $( "#draggable-2" ).draggable();  
  23.             $( "#droppable-2" ).droppable({  
  24.             drop: function( event, ui ) {  
  25.                $( this )  
  26.                .addClass( "ui-state-highlight" )  
  27.                .find( "p" )  
  28.                .html( "Dropped!" );  
  29.             }  
  30.          });  
  31.          $( "#droppable-3" ).droppable({  
  32.             disabled : "true",  
  33.             drop: function( event, ui ) {  
  34.                 $( this )  
  35.                .addClass( "ui-state-highlight" )  
  36.                .find( "p" )  
  37.                .html( "Dropped!" );  
  38.             }  
  39.          });  
  40.          $( "#droppable-4" ).droppable({  
  41.             tolerance: 'touch',  
  42.             drop: function( event, ui ) {  
  43.             $( this )  
  44.                .addClass( "ui-state-highlight" )  
  45.                .find( "p" )  
  46.                .html( "Dropped with a touch!" );  
  47.             }  
  48.          });  
  49.          $( "#droppable-5" ).droppable({  
  50.             tolerance: 'fit',  
  51.             drop: function( event, ui ) {  
  52.              $( this )  
  53.             .addClass( "ui-state-highlight" )  
  54.             .find( "p" )  
  55.             .html( "Dropped only when fully fit on the me!" );  
  56.             }  
  57.         });  
  58.       });  
  59.    </script>  
  60. </head>  
  61. <body>  
  62.    <div id="draggable-2" class="ui-widget-content">  
  63.       <p>Drag me to my target</p>  
  64.    </div>  
  65.    <div id="droppable-2" class="ui-widget-header">  
  66.       <p>Drop here</p>  
  67.    </div>  
  68.    <div id="droppable-3" class="ui-widget-header">  
  69.       <p>I'm disabled, you can't drop here!</p>  
  70.    </div>  
  71.    <div id="droppable-4" class="ui-widget-header">  
  72.       <p>Tolerance Touch!<br/> Drop to touch the boundary.</p>  
  73.    </div>  
  74.    <div id="droppable-5" class="ui-widget-header">  
  75.       <p>Tolerance Fit!<br/> Drop within the box.</p>  
  76.    </div>  
  77. </body>  
  78. </html>  

jQuery UI Droppable() method example 3

c) How to select elements to be dropped:
The following example specifies how to use accept and scope options in the drag function of jQuery UI:
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          .wrap{  
  11.             display: table-row-group;  
  12.          }  
  13.          #sqltutorial,#htmltutorial, #javatutorial,#springtutorial {   
  14.             width: 120px; height: 70px; padding: 0.5em; float: left;  
  15.             margin: 0px 5px 10px 0;   
  16.          }  
  17.            
  18.           #sql,#html,#java,#spring {   
  19.             width: 140px; height: 100px;padding: 0.5em; float: left;   
  20.             margin: 10px;    
  21.          }  
  22.            
  23.    </style>  
  24.    <script>  
  25.       $(function() {  
  26.          $( "#sqltutorial" ).draggable();  
  27.          $( "#htmltutorial" ).draggable();  
  28.   
  29.          $( "#sql" ).droppable({  
  30.             accept: "#sqltutorial",  
  31.             drop: function( event, ui ) {  
  32.                $( this )  
  33.                .addClass( "ui-state-highlight" )  
  34.                .find( "p" )  
  35.                .html( "Dropped!" );  
  36.             }  
  37.          });  
  38.          $( "#html" ).droppable({  
  39.             accept: "#htmltutorial",  
  40.             drop: function( event, ui ) {  
  41.             $( this )  
  42.             .addClass( "ui-state-highlight" )  
  43.             .find( "p" )  
  44.             .html( "Dropped!" );  
  45.             }  
  46.          });  
  47.   
  48.          $( "#javatutorial" ).draggable({scope : "java"});  
  49.          $( "#springtutorial" ).draggable({scope : "spring"});  
  50.          $( "#java" ).droppable({  
  51.             scope: "java",  
  52.             drop: function( event, ui ) {  
  53.             $( this )  
  54.             .addClass( "ui-state-highlight" )  
  55.             .find( "p" )  
  56.             .html( "Dropped!" );  
  57.             }  
  58.          });  
  59.          $( "#spring" ).droppable({  
  60.             scope: "spring",  
  61.             drop: function( event, ui ) {  
  62.             $( this )  
  63.             .addClass( "ui-state-highlight" )  
  64.             .find( "p" )  
  65.             .html( "Dropped!" );  
  66.             }  
  67.          });   
  68.       });  
  69.    </script>  
  70. </head>  
  71. <body>  
  72.    <div class="wrap" >  
  73.    <div id="sqltutorial" class="ui-widget-content">  
  74.       <p>Students who want to learn <strong>SQL</strong></p>  
  75.    </div>  
  76. <div id="htmltutorial" class="ui-widget-content">  
  77.       <p>Students who want to learn <strong>HTML</strong></p>  
  78.    </div>  
  79.  <div id="sql" class="ui-widget-header">  
  80.       <p>SQL</p>  
  81.    </div>  
  82. <div id="html" class="ui-widget-header">  
  83.       <p>HTML</p>  
  84.    </div>  
  85.    </div>  
  86.    <hr/>  
  87.    <div class="wrap" >  
  88.    <div id="javatutorial" class="ui-widget-content">  
  89.       <p>Students who want to learn <strong>Java</strong></p>  
  90.    </div>  
  91.    <div id="springtutorial" class="ui-widget-content">  
  92.       <p>Students who want to learn <strong>Spring</strong></p>  
  93.    </div>  
  94.    <div id="java" class="ui-widget-header">  
  95.       <p>Java</p>  
  96.    </div>  
  97.   
  98.    <div id="spring" class="ui-widget-header">  
  99.       <p>Spring</p>  
  100.    </div>  
  101.    </div>  
  102. </body>  
  103. </html>  

jQuery UI Droppable() method example 4

d) How to use activeClass and hoverClass :
activeClass and hoverClass options of jQuery UI are used to manage appearance. Let?s take an example to demonstrate this effect:
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style type="text/css">  
  10.          #draggable-3 {   
  11.            width: 100px; height: 50px; padding: 0.5em; float: left;  
  12.            margin: 21px 5px 10px 0;  
  13.          }  
  14.          #droppable-6 {   
  15.            width: 120px; height: 90px;padding: 0.5em; float: left;   
  16.            margin: 10px;   
  17.          }  
  18.          .active {  
  19.             border-color : red;  
  20.             background : pink;  
  21.             }    
  22.          .hover {  
  23.             border-color : black;  
  24.             background : lightgreen;  
  25.          }  
  26. </style>  
  27.    <script>  
  28.       $(function() {  
  29.          $( "#draggable-3" ).draggable();  
  30.          $( "#droppable-6" ).droppable({  
  31.             activeClass: "active",  
  32.             hoverClass:  "hover",  
  33.             drop: function( event, ui ) {  
  34.                $( this )  
  35.               .addClass( "ui-state-highlight" )  
  36.               .find( "p" )  
  37.               .html( "Dropped!" );  
  38.             }  
  39.          });  
  40.       });  
  41.    </script>  
  42. </head>  
  43. <body>  
  44.  <div id="draggable-3" class="ui-widget-content">  
  45.       <p>Drag me to my target</p>  
  46.    </div>  
  47. <div id="droppable-6" class="ui-widget-header">  
  48.       <p>Hover at me or Drop here</p>  
  49.    </div>  
  50. </body>  
  51. </html>  

Second Method

The droppable ("action", params) method is used to perform an action on droppable elements, like preventing droppable functionality. You have to pass an action specified as a string in the first argument. i.e. "disable" to prevent the drop.
Syntax:
  1. $(selector, context).droppable ("action", params);  
Following is the list of actions that can be used with this method:
ActionDescription
acceptThis option is used when you need to control which draggable elements are to be accepted for dropping. by default its value is *.
activeclassThis option is a string representing one or more css classes to be added to the droppable element when an accepted element (one of those indicated in options.accept) is being dragged. by default its value is false.
addclassesThis option will prevent the ui-droppable class from being added to the droppable elements,when set to false.by default its value is true.
disabledThis option disables the droppable,when set to true. by default its value is false.
greedyThis option is used when you need to control which draggable elements are to be accepted for dropping on nested droppables. by default its value is false. if this option is set to true, any parent droppables will not receive the element.
hoverclassThis option is string representing one or more css classes to be added to the element of droppable when an accepted element (an element indicated in options.accept) moves into it. by default its value is false.
scopeThis option is used to restrict the droppable action of draggable elements only to items that have the same options.scope (defined in draggable (options)). by default its value is "default".
tolerenceThis option is a string that specifies which mode to use for testing whether a draggable is hovering over a droppable. by default its value is "intersect".

jQuery UI Droppable() method example 5

Let's take an example to demonstrate the use of destroy () method:
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          .draggable-4 {   
  11.             width: 90px; height: 50px; padding: 0.5em; float: left;  
  12.             margin: 0px 5px 10px 0;  
  13.             border: 1px solid red;    
  14.             background-color:#B9CD6D;  
  15.          }  
  16.          .droppable-7 {   
  17.             width: 100px; height: 90px;padding: 0.5em; float: left;   
  18.             margin: 10px;   
  19.             border: 1px solid black;   
  20.             background-color:#A39480;  
  21.          }  
  22.          .droppable.active {   
  23.             background-color: red;   
  24.          }  
  25.       </style>  
  26.       <script>  
  27.          $(function() {  
  28.             $('.draggable-4').draggable({ revert: true });  
  29.             $('.droppable-7').droppable({  
  30.                hoverClass: 'active',  
  31.                drop: function(e, ui) {  
  32.                   $(this).html(ui.draggable.remove().html());  
  33.                   $(this).droppable('destroy');  
  34.                   $( this )  
  35.                   .addClass( "ui-state-highlight" )  
  36.                   .find( "p" )  
  37.                   .html( "i'm destroyed!" );  
  38.                }  
  39.             });  
  40.          });  
  41.       </script>  
  42.    </head>  
  43.    <body>  
  44.       <div class="draggable-4"><p>drag 1</p></div>  
  45.       <div class="draggable-4"><p>drag 2</p></div>  
  46.       <div class="draggable-4"><p>drag 3</p></div>  
  47. <div style="clear: both;padding:10px"></div>  
  48. <div class="droppable-7">drop here</div>  
  49.       <div class="droppable-7">drop here</div>  
  50.       <div class="droppable-7">drop here</div>  
  51.    </body>  
  52. </html>  
There are some event methods in jQueryUI which get triggered for a particular event. Following are some event methods.
Event MethodDescription
activate(event, ui)This event is triggered when the accepted draggable element starts dragging. this can be useful if you want to make the droppable "light up" when it can be dropped on.
create(event,ui)This event is triggered when a droppable element is created. ?where? event is of type event, and "ui" is of type object.
deactivate(event,ui)This event is triggered when an accepted draggable stops dragging. "Where" event is of type event, and ui is of type object.
drop(event,ui)This action is triggered when an element is dropped on the droppable. this is based on the tolerance option. "Where" event is of type event, and "ui" is of type object.
out(event,ui)This event is triggered when an accepted draggable element is dragged out of the droppable. this is based on the tolerance option. ?Where? event is of type event, and "ui" is of type object.
over(event,ui)This event is triggered when an accepted draggable element is dragged over the droppable. this is based on the tolerance option. "Where" event is of type event, and "ui" is of type object.

jQuery UI Droppable() method example 6

Let's take an example to demonstrate the "drop", "over" and "out" events during drop functionality.
  1. <!doctype html>  
  2. <html lang="en">  
  3.    <head>  
  4.       <meta charset="utf-8">  
  5.       <title>jQuery UI Droppable - Default functionality</title>  
  6.       <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  7.       <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  8.       <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  9.       <style>  
  10.          #draggable-5 {   
  11.             width: 100px; height: 50px; padding: 0.5em; float: left;  
  12.             margin: 22px 5px 10px 0;   
  13.          }  
  14.          #droppable-8 {      
  15.            width: 120px; height: 90px;padding: 0.5em; float: left;   
  16.            margin: 10px;  
  17.          }  
  18.       </style>  
  19.       <script>  
  20.          $(function() {  
  21.             $( "#draggable-5" ).draggable();  
  22.             $("#droppable-8").droppable({  
  23.                drop: function (event, ui) {  
  24.                $( this )  
  25.                .addClass( "ui-state-highlight" )  
  26.                .find( "p" )  
  27.                .html( "Dropped!" );  
  28.                },     
  29.                over: function (event, ui) {  
  30.                $( this )  
  31.                .addClass( "ui-state-highlight" )  
  32.                .find( "p" )  
  33.                .html( "moving in!" );  
  34.                },  
  35.                out: function (event, ui) {  
  36.                $( this )  
  37.                .addClass( "ui-state-highlight" )  
  38.                .find( "p" )  
  39.                .html( "moving out!" );  
  40.                }        
  41.             });  
  42.          });  
  43.       </script>  
  44.    </head>  
  45.    <body>  
  46. <div id="draggable-5" class="ui-widget-content">  
  47.       <p>Drag me to my target</p>  
  48.    </div>  
  49. <div id="droppable-8" class="ui-widget-header">  
  50.       <p>I am the target. </p>  
  51.    </div>  
  52. </body>  
  53. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...