Saturday, 19 March 2016

Drag and Drop items in Shopping Cart

Hello friends here I am going to tell you how to make drag and drop Shopping cart within few hours.

Step 1. Create a main.html file and paste the below code there

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="keywords" content="jquery,ui,easy,easyui,web,shopping cart">
    <meta name="description" content="TPF Shopping Cart">
    <title>The Perfect Future Shopping Cart</title>
    <link rel="stylesheet" type="text/css" href="easyui.css">
    <link rel="stylesheet" type="text/css" href="icon.css">
    <script type="text/javascript" src="jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="jquery.easyui.min.js"></script>   
    <link rel="stylesheet" href= "main.css">
    <link rel="stylesheet" href="bootstrap.min.css">
    <script>
        var data = {"total":0,"rows":[]};
        var totalCost         = 0;
        var totalItemCount     = 0;
        var rowId = 0;
        $(function(){
            $('#cartcontent').datagrid({
                singleSelect:true
            });
            $('.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';
                }
            });
            $('.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]));
                }
            });
        });

        function enableIncDecBtn(name)
        {
            if(document.getElementById(name+'Plus').disabled == true && document.getElementById(name+'Minus').disabled == true)
            {
                document.getElementById(name+'Plus').disabled     = false;
                document.getElementById(name+'Minus').disabled     = false;
            }
        }
        function disableIncDecBtn(name)
        {
            document.getElementById(name+'Plus').disabled     = true;
            document.getElementById(name+'Minus').disabled     = true;
        }
        function enableIncBtn(name)
        {
            if(document.getElementById(name+'Plus').disabled == true)
            {
                document.getElementById(name+'Plus').disabled     = false;
            }
        }
        function disableIncBtn(name)
        {
            if(document.getElementById(name+'Plus').disabled == false)
            {
                document.getElementById(name+'Plus').disabled     = true;
            }
        }

        function enableDecBtn(name)
        {
            if(document.getElementById(name+'Minus').disabled == true)
            {
                document.getElementById(name+'Minus').disabled     = false;
            }
        }
        function disableDecBtn(name)
        {
            if(document.getElementById(name+'Minus').disabled == false)
            {
                document.getElementById(name+'Minus').disabled     = true;
            }
        }
        function trashButtonEnableDisable()
        {
            if(totalItemCount == 0 && document.getElementById('trashbtn').disabled == false)
            {
                document.getElementById('trashbtn').disabled = true;
            }
            else
            {
                document.getElementById('trashbtn').disabled = false;
            }
        }
        function totalSumHeading()
        {
            $('div.cart .total').html('Total: $'+totalCost);
        }
        function noOfItemHeading()
        {
            $('#itemCount').html(totalItemCount);
        }

        function removeMe()
        {
            var rows     = $('#cartcontent').datagrid('getSelections');  // get all selected rows
            var ids     = [];
            var qty     = 0;
            var price     = 0;
            var name     = '';
            for(var i=0; i<rows.length; i++)
            {
                name     = rows[i].name;
                price     = rows[i].price;
                qty     = rows[i].quantity;
                var index = $('#cartcontent').datagrid('getRowIndex',rows[i]);  // get the row index
                ids.push(index);
            }
            totalItemCount     = totalItemCount - qty;
            totalCost         = totalCost -  (qty * price);
            ids.sort();  // sort index
            ids.reverse();  // sort reverse
            for(var i=0; i<ids.length; i++)
            {
                $('#cartcontent').datagrid('deleteRow',ids[i]);
            }
            noOfItemHeading();   
            totalSumHeading();
            trashButtonEnableDisable();
            disableIncDecBtn(name);
        }
       
        function addProduct(name,price)
        {
            var qty = 0;
            function add()
            {
                for(var i=0; i<data.total; i++)
                {
                    var row = data.rows[i];
                    if (row.name == name){
                        row.quantity += 1;
                        qty     =  row.quantity;
                        return;
                    }
                }
                data.total += 1;
                data.rows.push({
                    name:name,
                    quantity:1,
                    price:price,
                });
            }
            add();
            rowId++;
            totalCost += price;
            totalItemCount += 1;
            $('#cartcontent').datagrid('loadData', data);
            noOfItemHeading();
            totalSumHeading();
            trashButtonEnableDisable();
            if(qty <= 1)
            {
                enableIncBtn(name);
            }
            else if(qty > 1)
            {
                enableDecBtn(name);
            }   
        }

        function removeProduct(name,price)
        {
            var isFound = 0;
            var qty;
            if(data.total == 0)
            {
                return;//no item in the cart
            }
            function remove()
            {
                for(var i=0; i<data.total; i++)
                {
                    var row = data.rows[i];
                    if (row.name == name){
                        row.quantity -= 1;
                        qty = row.quantity;
                        return;
                    }
                }
                data.total -= 1;
                data.rows.pop({
                    name:name,
                    quantity:1,
                    price:price

                });
            }
            remove();
            totalCost         -= price;
            totalItemCount     -= 1;
            $('#cartcontent').datagrid('loadData', data);
            noOfItemHeading();
            totalSumHeading();
            if(qty == 1)
            {
                disableDecBtn(name);
            }
        }
   
    </script>
</head>


<body style="margin:0;padding:0;height:100%;background:#fafafa;">
    <div class="container-fluid" id = "header">   
        <div class="row">
            <div class="col-sm-3">
                <img src="tpf.jpg">
            </div>
            <div class="col-sm-7">
                <center><h3>The Perfect Future Shopping Cart</h3></center>
            </div>
            <div class="col-sm-2">
                <img src="profile.png" height="130px" width="100px">
            </div>
        </div>
    </div>
    <div id = "my-container" class="container-fluid">
         <div class="row">
            <div id = "left-menu" class="col-sm-8"> 
                <ul class="products">
                    <div class="row">
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book5.jpg" class="my-images" />
                                    <div>
                                        <p>Alpine</p>
                                        <p>Price:$25</p>
                                       
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('Alpine',25);" value="ADD">
                                <input type="button" id = "AlpinePlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('Alpine',25);" value="+" disabled>
                                <input type="button" id = "AlpineMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('Alpine',25);" value="-" disabled>
                            </li>
                        </div>
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book2.jpg" class="my-images" />
                                    <div>
                                        <p>Under the Sky</p>
                                        <p>Price:$50</p>
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('Under the Sky',50);" value="ADD">
                                <input type="button" id = "Under the SkyPlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('Under the Sky',50);" value="+" disabled>
                                <input type="button" id = "Under the SkyMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('Under the Sky',50);" value="-" disabled>
                            </li>
                        </div>
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book4.jpg" class="my-images"/>
                                    <div>
                                        <p>In the Water</p>
                                        <p>Price:$100</p>
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('In the Water',100);" value="ADD">
                                <input type="button" id = "In the WaterPlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('In the Water',100);" value="+" disabled>
                                <input type="button" id = "In the WaterMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('In the Water',100);" value="-" disabled>
                            </li>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book9.jpg" class="my-images"/>
                                    <div>
                                        <p>Rock n Roll</p>
                                        <p>Price:$200</p>
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('Rock n Roll',200);" value="ADD">
                                <input type="button" id = "Rock n RollPlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('Rock n Roll',200);" value="+" disabled>
                                <input type="button" id = "Rock n RollMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('Rock n Roll',200);" value="-" disabled>
                            </li>
                        </div>
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book6.jpg" class="my-images"/>
                                    <div>
                                        <p>Master Plan</p>
                                        <p>Price:$300</p>
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('Master Plan',300);" value="ADD">
                                <input type="button" id = "Master PlanPlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('Master Plan',300);" value="+" disabled>
                                <input type="button" id = "Master PlanMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('Master Plan',300);" value="-" disabled>
                            </li>
                        </div>
                        <div class="col-sm-4">
                            <li>
                                <a href="#" class="item" data-toggle="tooltip" data-placement="top" title="Drag the item to the cart">
                                    <img src="book7.jpg" class="my-images"/>
                                    <div>
                                        <p>Remaining Days</p>
                                        <p>Price:$400</p>
                                    </div>
                                </a>
                                <input type="button" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Add new item to the cart"     onclick="addProduct('Remaining Days',400);" value="ADD">
                                <input type="button" id = "Remaining DaysPlus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Increment the Quantity"     onclick="addProduct('Remaining Days',400);" value="+" disabled>
                                <input type="button" id = "Remaining DaysMinus" class = "btn btn-primary" data-toggle="tooltip" data-placement="top" title="Decrement the Quantity"     onclick="removeProduct('Remaining Days',400);" value="-" disabled>
                            </li>
                        </div>
                    </div>   
                </ul>
            </div>
            <div id = "cart" class="col-sm-4 cart">
                <center><h3>My Shopping Cart</h3></center>
                <img src="shoppingcart1.png" style = "height:60px;width:60px;" class=""/>
                <div>
                    <strong><center>You have <span id ="itemCount" >0</span> items in the cart</center></strong>
                </div>
                <hr>
                <div>
                    <table id="cartcontent" fitColumns="true">
                        <thead>
                            <tr>
                                <th field="name" width=130>Name</th>
                                <th field="quantity" width =90 align="right">Quantity</th>
                                <th field="price" width =80 align="right">Price</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <strong>
                    <p class="total">Total: $0</p></strong>
                <hr>
                <button  id = "trashbtn" data-toggle="tooltip" data-placement="top" title="Please select an item row to remove from the cart" type="button" class="btn btn-default" onclick="removeMe();" disabled>
                       <img src = "delete.png" height = "20px" width = "20px"> TRASH
                </button>
                <h3>Drop here to add to cart</h3>
            </div>
        </div>
        <div class="container-fluid" id = "footer">
            <div class = "header-footer">
                <center><strong><h4>The Perfect Future Shopping Cart</h4></strong></center>
            </div>
        </div>
    </div>
</body>
</html>

Step 2: Create a main.css file and paste the below code there


        .products{
            list-style:none;
            margin-right:300px;
            padding:0px;
            height:100%;
        }
        .products li{
            display:inline;
            float:left;
            margin:10px;
        }
        .item{
            display:block;
            text-decoration:none;
        }
        .item img{
            border:1px solid #333;
        }
        .item p{
            margin:0;
            font-weight:bold;
            text-align:center;
            color:#c3c3c3;
        }
        .cart{
            position:absolute;
            right:0;
            top:0px;
            width:350px;
            height:700px;
            background:#F44336;
            padding:0px 10px;
            border:2px solid #3d5c5c;
            border-radius: 20px;
        }
        #my-container
        {
            height:700px;
        }
        h1{
            text-align:center;
            color:#555;
        }
        h2{
            position:absolute;
            font-size:16px;
            left:10px;
            bottom:20px;
            color:#555;
        }
        .total{
            margin:0;
            text-align:right;
            padding-right:20px;
        }
        #header{height: 130px;background-color:white;border-bottom: 2px solid #3d5c5c}
        .my-images{height: 250px;width: 200px;}
        #footer{height: 30px;border-top: 2px solid #3d5c5c;margin-top: 30px;}
        .header-footer{text-align: left}
        #left-menu{}
        .images-margin{}

Step 3: CSS Files 

Link these in main.html or copy the code from these css and store offline.
Store these codes in related files. if you are giving the direct link to the files then please remove the link of css files from main.html.

1.
<link rel="stylesheet"href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
2.
<link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/default/easyui.css">
 3.

<link rel="stylesheet" type="text/css" href="http://www.jeasyui.com/easyui/themes/icon.css">

Step 4: JS Files

 1.
http://code.jquery.com/jquery-1.11.2.min.js

2.
http://www.jeasyui.com/easyui/jquery.easyui.min.js

Step 5: Images

Replace the images name code in the main.html file.

Step 6. Open File main.html in browser
 
 

No comments:

Post a Comment