easyUI拖動購物車案例
阿新 • • 發佈:2018-06-25
fin $0 fff otto height cti borde rds 屬性
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="jquery,ui,easy,easyui,web">
<title>拖動購物車案例</title>
<link rel="stylesheet" type="text/css" href="js/jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="js/jquery-easyui/themes/icon.css">
<script src="js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-easyui/jquery.easyui.min.js"></script>
<style type="text/css">
.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:fixed;
right:0;
top:0;
width:300px;
height:100%;
background:#ccc;
padding:0px 10px;
}
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;
}
</style>
<script>
var data = {"total":0,"rows":[]};
var totalCost = 0;
$(function(){
$(‘#cartcontent‘).datagrid({
singleSelect:true
});
//拖動克隆的商品: draggable 屬性的值從 ‘proxy‘ 設置為 ‘clone‘,所以拖動元素將由克隆產生。
$(‘.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‘;
}
});
//放置選擇商品到購物車中(購物車采用的是datagrid來存儲)
$(‘.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]));
}
});
});
//每當放置商品的時候,需要首先得到商品名稱和價格,然後調用 ‘addProduct‘ 函數來更新購物車。
function addProduct(name,price){
function add(){
for(var i=0; i<data.total; i++){
var row = data.rows[i];
if (row.name == name){
row.quantity += 1;
return;
}
}
data.total += 1;
data.rows.push({
name:name,
quantity:1,
price:price
});
}
add();
totalCost += price;
$(‘#cartcontent‘).datagrid(‘loadData‘, data);
$(‘div.cart .total‘).html(‘Total: $‘+totalCost);
}
</script>
</head>
<body style="margin:0;padding:0;height:100%;background:#fafafa;">
<!--創建商品-->
<ul class="products">
<li>
<a href="#" class="item">
<img src="images/shirt1.gif"/>
<div>
<p>Balloon</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt2.gif"/>
<div>
<p>Feeling</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt3.gif"/>
<div>
<p>Elephant</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt4.gif"/>
<div>
<p>Stamps</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt5.gif"/>
<div>
<p>Monogram</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="images/shirt6.gif"/>
<div>
<p>Rolling</p>
<p>Price:$25</p>
</div>
</a>
</li>
</ul>
<!--創建購物車-->
<div class="cart">
<h1>Shopping Cart</h1>
<div style="background:#fff">
<table id="cartcontent" fitColumns="true" style="width:300px;height:auto;">
<thead>
<tr>
<th field="name" width=140>Name</th>
<th field="quantity" width=60 align="right">Quantity</th>
<th field="price" width=60 align="right">Price</th>
</tr>
</thead>
</table>
</div>
<p class="total">Total: $0</p>
<h2>Drop here to add to cart</h2>
</div>
</body>
</html>
easyUI拖動購物車案例