Interchange

Tag: Drag and Drop

Drag and Drop custom actions with DragEvent.preventDefault()

by hbomb on Mar.18, 2009, under Flex

Dragging and dropping items within Flex is in general pretty simple, especially for components which already have the drag and drop bits already added to them. However, I was running into some problems with adding my own custom actions to the dragDrop event. My actions would add fine, but Flex has some actions which it will also run automatically… unless you call preventDefault() on the DragEvent. What this allows you to do is to have your own actions run without worrying about whether or not the default actions run, which actions run first, etc.

So if you, for instance, wanted to make sure whatever you were dropping did not already exist, your dragDrop event handler would look something like this:

protected function dragDropEventHandler(event:DragEvent):void{
     event.preventDefault();
     if(!checkExists(event)){
          (event.target.dataProvider as ArrayCollection).addItem(event.draggedItem);
     }
}

And it’s as simple as that. This could also be used for customizing what gets added - perhaps by building an object which contains which object the draggedItem came from, or having custom text entered into a list, or any of a number of things which could work out better than having the default action performed. I hadn’t really seen this little gem talked about much, but I found it very useful and hopefully someone else will, as well.

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...