jQuery EasyUI drag - drag and drop to create a shopping cart

If you can through your Web applications simple to implement drag and drop, you know something special. By jQuery EasyUI, we can simply drag and drop functionality in Web applications.

In this tutorial, we'll show you how to create a user drag and drop items to enable users to buy the shopping cart page. Shopping basket of goods and prices will be updated.

Goods displayed on the page

	<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>
		<-! Other products ->
	</ Ul>

As you can see in the above code, we add a containing <ul> element number <li> element to display merchandise. All merchandise has a name and price properties, which is included in the <p> element.

Create a Shopping Cart

	<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>

We use the data grid (datagrid) to display the items in the shopping basket.

Drag the clone of goods

	$ ( '. 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';
		}
	});

Please note that we draggable attribute value from the 'proxy' is set to 'clone', so the drag element produced by cloning.

Place select items to 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 <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);.
	}	

Whenever the placement of goods, we get the first name and commodity prices, and then call 'addProduct' function to update the shopping basket.

Download jQuery EasyUI examples

jeasyui-dd-shopping.zip