利用easyui的拖放能力可以轻松创建购物车,看一个例子:
1、显示产品HTML
<ul class="products">
<li>
<a href="#" class="item">
<img src="shirt1.gif"/>
<div>
<p>Balloon</p>
<p>Price:$25</p>
</div>
</a>
</li>
<li>
<a href="#" class="item">
<img src="shirt2.gif"/>
<div>
<p>Feeling</p>
<p>Price:$25</p>
</div>
</a>
</li>
<!-- other products -->
</ul>
2、购物车HTML:
<div class="cart">
<h1>Shopping Cart</h1>
<table id="cartcontent" 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>
<p class="total">Total: $0</p>
<h2>Drop here to add to cart</h2>
</div>
3、编写拖放代码:
$('.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]));
}
});
4、更新购物篮代码:
var data = {"total":0,"rows":[]};
var totalCost = 0;
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);
}
原文及下载地址:http://jquery-easyui.wikidot.com/tutorial:dnd2
分享到:
相关推荐
jquery-easyui-EDT-1.5.2-build1 jquery-easyui-EDT-1.5.2-build1
jquery-easyui-1.7.0 官方文档以及源码,演示demo,内容详细
jquery-easyui-1.2.2jquery-easyui-1.2.2jquery-easyui-1.2.2
jquery-easyui完整demo演示
jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI...jQuery-Easyui-1.2.6中含有jQuery库、easyui库、demo实例和jquery_easyui_中文帮助文档等。
jquery-easyui的官方主题包,包括metro五套和ui四套
jQuery EasyUI jquery-easyui-1.5.5.6 版 API 中文版 PDF和chm版本,jQuery
该资源为jquery-easyui-1.5.5版本,包含easyui插件所依赖的js和css
Jquery-Easyui-1.2.3以及帮助文档
jquery-easyui-themes,easyui主题包,包含metro-blue metro-gray metro-green metro-orange metro-red ui-cupertino ui-dark-hive ui-pepper-grinder ui-sunny这些主题,具体的php使用方式见:...
jquery-easyui-1.4.4包含css和js 及一些案例,easyui1.4.4
jquery-easyui-1.2 版本 同时包含各种例子:datagrid,tree
jquery-easyui-1.4.5帮助文档
easyui是一种基于jQuery的用户界面插件集合。 easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能。 使用easyui你不需要写很多代码,你只需要通过编写一些简单HTML标记,就可以定义用户界面。 easyui...
教程名称:超实用的jQuery-EasyUI视频教程(30集)课程目录:【】jQuery-EasyUI视频教程(01-02集)【】jQuery-EasyUI视频教程(03-04集)【】jQuery-EasyUI视频教程(05-06集)【】jQuery-EasyUI视频教程(07-08集...
jquery-easyui-1.3.5打包下载
jquery-easyui-1.5版本,自带demo可以随时查看,并且包含easyui主要js
jquery-easyui-1.3.3
jquery-easyui-1.5.3
jquery-easyui-1.3.1,兼容IE7.0