{"id":2657,"date":"2012-01-20T00:40:27","date_gmt":"2012-01-19T19:10:27","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2657"},"modified":"2012-01-20T00:40:27","modified_gmt":"2012-01-19T19:10:27","slug":"create-simple-drag-and-drop-widget-like-igoogle-using-jquery","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/create-simple-drag-and-drop-widget-like-igoogle-using-jquery\/","title":{"rendered":"Create Simple Drag and Drop Widget like iGoogle using JQuery"},"content":{"rendered":"<p>After reading this post you will come to know that how easy its to create the Drag and Drop functionality using JQuery.<\/p>\n<p>To run this example you will need two javascript libraries as follow:<\/p>\n<ol>\n<li><a href=\"http:\/\/jquery.com\/\">JQuery<\/a><\/li>\n<li><a href=\"http:\/\/jqueryui.com\/\">JQueryUI<\/a><\/li>\n<\/ol>\n<figure id=\"attachment_2661\" aria-describedby=\"caption-attachment-2661\" style=\"width: 591px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/jQuery-Drag-and-Drop-Widgets.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-2661  \" title=\"jQuery Drag and Drop Widgets\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/jQuery-Drag-and-Drop-Widgets.png?resize=591%2C234&#038;ssl=1\" alt=\"jQuery Drag and Drop Widgets\" width=\"591\" height=\"234\" \/><\/a><figcaption id=\"caption-attachment-2661\" class=\"wp-caption-text\">jQuery Drag and Drop Widgets<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<h3><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/demos\/DragAndDrop\/index.html\">You can see the complete working demo here<\/a><\/h3>\n<p>Lets say you want to create the widget in two columns, so first create two DIV tags as shown below:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;div id=&quot;column1&quot; class=&quot;column&quot;&gt;\n&lt;!-- Widgets code here --&gt;\n&lt;\/div&gt;\n\n&lt;div id=&quot;column2&quot;  class=&quot;column&quot;&gt;\n&lt;!-- Widgets code here --&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>We are assuming that we can drag the elements from column1 to column2 and vice verse.<\/p>\n<p>Now we will create the actual element which can be drag-gable.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;div id=&quot;column1&quot; class=&quot;column&quot;&gt;\n\t&lt;div class=&quot;dragbox&quot; id=&quot;item1&quot; &gt;\n\t\t&lt;h2&gt;Handle 1\n\t\t  &lt;a href=&quot;#&quot; class=&quot;delete opIcons&quot;&gt; &lt;\/a&gt;\n\t\t  &lt;a href=&quot;#&quot; class=&quot;maxmin opIcons&quot;&gt; &lt;\/a&gt;\n\n\t\t&lt;\/h2&gt;\n\t\t&lt;div class=&quot;dragbox-content&quot; &gt;\n\t\t\t&lt;!-- Panel Content Here --&gt;\n\t\t&lt;\/div&gt;\n\t&lt;\/div&gt;\n&lt;\/div&gt;\n\n&lt;div id=&quot;column2&quot;  class=&quot;column&quot;&gt;\n\t&lt;div class=&quot;dragbox&quot; id=&quot;item2&quot; &gt;\n\t\t&lt;h2&gt;Handle 2\n\t\t  &lt;a href=&quot;#&quot; class=&quot;delete opIcons&quot;&gt; &lt;\/a&gt;\n\t\t  &lt;a href=&quot;#&quot; class=&quot;maxmin opIcons&quot;&gt; &lt;\/a&gt;\n\n\t\t&lt;\/h2&gt;\n\t\t&lt;div class=&quot;dragbox-content&quot; &gt;\n\t\t\t&lt;!-- Panel Content Here --&gt;\n\t\t&lt;\/div&gt;\n\t&lt;\/div&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>As you can see &lt;h2&gt; tag is used to create the heading and there are two &lt;a&gt; (anchor) tags, one for delete and one for toggle.<\/p>\n<p>Now its time to write the JQuery code to drag and drop the &lt;div&gt; tag with <strong>class=&#8221;dragbox&#8221;<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n$(\nfunction(){\n\t$('a.maxmin').click(\n\tfunction(){\n\t\t$(this).parent().siblings('.dragbox-content').toggle();\n\t});\n\n\t$('a.delete').click(\n\tfunction(){\n\t\tvar sel = confirm('do you want to delete the widget?');\n\t\tif(sel)\n\t\t{\n\t\t\t\/\/del code here\n\t\t}\n\t}\n\t);\n\n\t$('.column').sortable({\n\tconnectWith: '.column',\n\thandle: 'h2',\n\tcursor: 'move',\n\tplaceholder: 'placeholder',\n\tforcePlaceholderSize: true,\n\topacity: 0.4,\n\tstop: function(event, ui)\n\t\t{\n\t\t\t$(ui.item).find('h2').click();\n\t\t\tvar sortorder='';\n\n\t\t\t$('.column').each(function(){\n\t\t\t\tvar itemorder=$(this).sortable('toArray');\n\t\t\t\tvar columnId=$(this).attr('id');\n\t\t\t\tsortorder+=columnId+'='+itemorder.toString()+'&amp;';\n\t\t\t});\n\t\t\tsortorder = sortorder.substring(0, sortorder.length - 1)\n\t\t\talert('SortOrder: '+sortorder);\n\n\t\t}\n\t}).disableSelection();\n});\n<\/pre>\n<p style=\"text-align: justify;\">As you can see, on dom ready event using $() &#8211; which is equal to &#8220;$.ready()&#8221;, we have written the code like $(&#8216;.column&#8217;).sortable(&#8230;) which will perform the drag and drop functionality. the &#8220;sortable&#8221; 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.<\/p>\n<p style=\"text-align: justify;\">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.<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\n.column{\n\twidth:49%;\n\tmargin-right:.5%;\n\tmin-height:300px;\n\tbackground:#fff;\n\tfloat:left;\n}\n.column .dragbox{\n\tmargin:5px 2px  20px;\n\tbackground:#fff;\n\tposition:&quot;relative&quot;;\n\tborder:1px solid #946553;\n\t-moz-border-radius:5px;\n\t-webkit-border-radius:5px;\n}\n.column .dragbox h2{\n\tmargin:0;\n\tfont-size:12px;\n\tbackground:#946553;\n\tcolor:#fff;\n\tborder-bottom:1px solid #946553;\n\tfont-family:Verdana;\n\tcursor:move;\n\tpadding:5px;\n}\n\n.dragbox-content{\n\tbackground:#fff;\n\tmin-height:100px; margin:5px;\n\tfont-family:'Lucida Grande', Verdana; font-size:0.8em; line-height:1.5em;\n}\n.column  .placeholder{\n\tbackground: #EED5B7;\n\tborder:1px dashed #946553;\n}\n\n.opIcons\n{\nbackground-image: url('iconSpirite.gif')!important;\nbackground-repeat: no-repeat;\nfloat:right;\nheight:13px;\nwidth:13px;\nmargin:0px 2px;\n}\n\n.maxmin\n{\n\tbackground-position:-26px 1px;\n}\n\n.delete\n{\n\tbackground-position:-269px center;\n\tpadding-top:1px;\n}\n<\/pre>\n<h3><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/demos\/DragAndDrop\/index.html\">You can see the complete working demo here<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Live Demo with source code on creating Simple Drag and Drop Widget using JQuery plugin like iGoogle <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[26],"tags":[134],"class_list":["post-2657","post","type-post","status-publish","format-standard","hentry","category-web","tag-jquery"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":65,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/highlight-current-field-using-jquery\/","url_meta":{"origin":2657,"position":0},"title":"Highlight Current field using JQuery","author":"Jitendra","date":"May 10, 2010","format":false,"excerpt":"Write below HTML code : This will create two text boxes. [sourcecode lang=\"css\"] <form> <div> <label for=\"Name\">Name:<\/label> <input name=\"Name\" type=\"text\"><\/input> <\/div> <div> <label for=\"Email\">Email:<\/label> <input name=\"Email\" type=\"text\"><\/input> <\/div> <\/form> [\/sourcecode] Using jQuery, we can watch for an event where an input form comes into focus: Add link to JQuery file\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Selecting field using jquery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/11-300x50.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2706,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/handling-colon-in-element-id-in-jquery-visualforce-problem\/","url_meta":{"origin":2657,"position":1},"title":"Handling Colon in Element ID in JQuery &#8211; Visualforce problem","author":"Jitendra","date":"February 8, 2012","format":false,"excerpt":"Resolving the JQuery error when the Element ID contains colon with Live Demo","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2843,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/streaming-api-using-jquery-salesforce\/","url_meta":{"origin":2657,"position":2},"title":"Streaming API Using JQuery &#8211; Salesforce","author":"Jitendra","date":"May 6, 2012","format":false,"excerpt":"Tutorial of Streaming API in Salesforce with Source code and Demo Video - Implementation of Bayeux protocol and CometD using JQuery and JSON","rel":"","context":"In &quot;Tech Tips&quot;","block_context":{"text":"Tech Tips","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/tips\/"},"img":{"alt_text":"Salesforce Streaming API - Create Push topic","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Streaming-API-Create-Push-topic-1024x332.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3355,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/jquery-based-raffle-in-salesforce\/","url_meta":{"origin":2657,"position":3},"title":"JQuery Based Raffle in Salesforce","author":"Jitendra","date":"June 29, 2013","format":false,"excerpt":"Recently i run into very interesting scenario where i had to randomly take name of few contacts. There are many ways to do it but I thought why not to implement this functionality in Salesforce with help of some Javascript? So i came up with this - JQuery based Raffle\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"JQuery Based Raffle in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":790,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/row-expand-collapse-using-jquery-and-ajax\/","url_meta":{"origin":2657,"position":4},"title":"Row expand collapse using jquery and Ajax","author":"Jitendra","date":"August 19, 2010","format":false,"excerpt":"Using JQuery and Ajax to expand and collapse the row in ASP.NET","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Ajax JQuery Row Expand Collapse","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/rowexpand-300x203.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3134,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-helptext-like-custom-tooltip-using-jquery\/","url_meta":{"origin":2657,"position":5},"title":"Salesforce Helptext like Custom Tooltip using JQuery","author":"Jitendra","date":"February 4, 2013","format":false,"excerpt":"Welcome back Readers. This is my first blog entry for year 2013, i know its too late. However i can ensure that i have lots of unique post which will come this year. I am starting this year with very light post , mimic the help-text style of Salesforce. Download\u2026","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Salesforce like Helptext - Tooltip using JQuery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/02\/Salesforce-like-Tooltip-using-JQuery.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2657"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2657\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}