Create Simple Drag and Drop Widget like iGoogle using JQuery

Author posted by Jitendra on Posted on under category Categories HTML and tagged as Tags with 5 Comments on Create Simple Drag and Drop Widget like iGoogle using JQuery

Live Demo with source code on creating Simple Drag and Drop Widget using JQuery plugin like iGoogle

After reading this post you will come to know that how easy its to create the Drag and Drop functionality using JQuery.

To run this example you will need two javascript libraries as follow:

  1. JQuery
  2. JQueryUI
jQuery Drag and Drop Widgets
jQuery Drag and Drop Widgets

You can see the complete working demo here

Lets say you want to create the widget in two columns, so first create two DIV tags as shown below:

<div id="column1" class="column">
<!-- Widgets code here -->
</div>

<div id="column2"  class="column">
<!-- Widgets code here -->
</div>

We are assuming that we can drag the elements from column1 to column2 and vice verse.

Now we will create the actual element which can be drag-gable.

<div id="column1" class="column">
	<div class="dragbox" id="item1" >
		<h2>Handle 1
		  <a href="#" class="delete opIcons"> </a>
		  <a href="#" class="maxmin opIcons"> </a>

		</h2>
		<div class="dragbox-content" >
			<!-- Panel Content Here -->
		</div>
	</div>
</div>

<div id="column2"  class="column">
	<div class="dragbox" id="item2" >
		<h2>Handle 2
		  <a href="#" class="delete opIcons"> </a>
		  <a href="#" class="maxmin opIcons"> </a>

		</h2>
		<div class="dragbox-content" >
			<!-- Panel Content Here -->
		</div>
	</div>
</div>

As you can see <h2> tag is used to create the heading and there are two <a> (anchor) tags, one for delete and one for toggle.

Now its time to write the JQuery code to drag and drop the <div> tag with class=”dragbox”

$(
function(){
	$('a.maxmin').click(
	function(){
		$(this).parent().siblings('.dragbox-content').toggle();
	});

	$('a.delete').click(
	function(){
		var sel = confirm('do you want to delete the widget?');
		if(sel)
		{
			//del code here
		}
	}
	);

	$('.column').sortable({
	connectWith: '.column',
	handle: 'h2',
	cursor: 'move',
	placeholder: 'placeholder',
	forcePlaceholderSize: true,
	opacity: 0.4,
	stop: function(event, ui)
		{
			$(ui.item).find('h2').click();
			var sortorder='';

			$('.column').each(function(){
				var itemorder=$(this).sortable('toArray');
				var columnId=$(this).attr('id');
				sortorder+=columnId+'='+itemorder.toString()+'&';
			});
			sortorder = sortorder.substring(0, sortorder.length - 1)
			alert('SortOrder: '+sortorder);

		}
	}).disableSelection();
});

As you can see, on dom ready event using $() – which is equal to “$.ready()”, we have written the code like $(‘.column’).sortable(…) which will perform the drag and drop functionality. the “sortable” functionality is used to save the state of the widget in database so that next time you can load the widget in order which they are previously for this you will need to do some extra code here. The sort result is displayed using alert after element drop.

For the delete action, i have written the comment where server request can be called using Ajax to delete the widget. With the help of below CSS this example will be completed with nice look and feel.

.column{
	width:49%;
	margin-right:.5%;
	min-height:300px;
	background:#fff;
	float:left;
}
.column .dragbox{
	margin:5px 2px  20px;
	background:#fff;
	position:"relative";
	border:1px solid #946553;
	-moz-border-radius:5px;
	-webkit-border-radius:5px;
}
.column .dragbox h2{
	margin:0;
	font-size:12px;
	background:#946553;
	color:#fff;
	border-bottom:1px solid #946553;
	font-family:Verdana;
	cursor:move;
	padding:5px;
}

.dragbox-content{
	background:#fff;
	min-height:100px; margin:5px;
	font-family:'Lucida Grande', Verdana; font-size:0.8em; line-height:1.5em;
}
.column  .placeholder{
	background: #EED5B7;
	border:1px dashed #946553;
}

.opIcons
{
background-image: url('iconSpirite.gif')!important;
background-repeat: no-repeat;
float:right;
height:13px;
width:13px;
margin:0px 2px;
}

.maxmin
{
	background-position:-26px 1px;
}

.delete
{
	background-position:-269px center;
	padding-top:1px;
}

You can see the complete working demo here

Related posts

  • Mmoin56

    Thanks for above solution ….

  • Arun

    My mob no: +917204651195

  • Hi it will not working in IE9

  • yash

    awesome. thanks…….

  • pankaj

    i need iconSpirite.gif