Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery EasyUI Drag and Drop - Creating a Drag-and-Drop Shopping Cart

jQuery EasyUI Drag and Drop - Creating a Drag-and-Drop Shopping Cart

If you can easily implement dragging and dropping in your web application, you know something special. With jQuery EasyUI, we can easily implement drag-and-drop functionality in web applications.

In this tutorial, we will show you how to create a shopping cart page that enables users to drag and drop the products they want to buy. The items and prices in the shopping basket will be updated.

Displaying the products on the page



    

As you can see in the code above, we add a container with some

  • elements'
      elements to display the products. All products have name and price attributes, which are contained in

      element.

      Create a shopping cart

      
      
          
              

      Shopping Cart

                                                                                                                                                  
      NameQuantityPrice
              

      Total: $0

              

      Drop here to add to cart

          

      We use a datagrid to display the items in the shopping basket.

      Dragging the cloned product

      
      
          $('.item').draggable({
      
              revert:true,
      
              proxy:'clone',
      
              onStartDrag:function(){
      
                  $(this).draggable('options').cursor = 'not-allowed';
      
                  $(this).draggable('proxy').css('z-index',10);
      
              },
      
              onStopDrag:function(){
      
                  $(this).draggable('options').cursor='move';
      
              }
      
          });
      
      

      Note that we changed the value of the draggable property from 'proxy' to 'clone', so the dragged element will be produced by cloning.

      Dropping the selected product into the shopping cart

      
      
          $('.cart').droppable({
      
              onDragEnter:function(e,source){
      
                  $(source).draggable('options').cursor='auto';
      
              },
      
              onDragLeave:function(e,source){
      
                  $(source).draggable('options').cursor='not-allowed';
      
              },
      
              onDrop:function(e,source){
      
                  var name = $(source).find('p:eq(0)').html();
      
                  var price = $(source).find('p:eq(1)').html();
      
                  addProduct(name, parseFloat(price.split('$')[1]));
      
              }
      
          });
      
          var data = {"total":0,"rows":[]};
      
          var totalCost = 0;
      
          function addProduct(name,price){
      
              function add(){
      
                  for(var i=0; i

      Whenever a product is dropped, we first get its name and price, then call the 'addProduct' function to update the shopping cart.

      Download jQuery EasyUI Examples

      jeasyui-dd-shopping.zip