April 14, 2013

JQuery makes UI fun

Just recently I have been whipping up a UI with drag and drop. Windows forms and WPF are sort of a pain for a simple drag and drop scenario. Then add in a sortable UI ability and it makes me cry hurt. So, in comes in JQuery UI. This library rocks, and let me show you why!

So, first question: How do I make these things dragable? Here is how:

  $('.dragableItem').draggable(); 
my draggable box

Draggable Demo:

Super easy, just select your element and call draggable. Also, make sure to include the jquery library. Now along with draggable, why not add dropable? Like so:

 $('.droppable').droppable( { activeClass: "ui-state-default", hoverClass: "ui-state-hover", accept: ":not(.ui-sortable-helper)", drop: function (event, ui) { alert('Dropped here.'); } } ); 

Dropable Demo:

my dropable box

Awesome, just got drag and drop in a few pieces of code.

Ok, now lastly, let’s throw together some sortable stuff:

 $('.sortable').sortable({items:'li'}); 
  1. task 1
  2. task 2
  3. task 3
  4. task 4

Sortable Demo:

  1. task 1
  2. task 2
  3. task 3
  4. task 4

Bam, we got some advanced UI features, and done in less than 10 lines of code. Try it out!