views:

116

answers:

2

I have a set of items that can be dragged / dropped using Dojo DND, that part has already been implemented and all is working well. The specific question I have is that I'd like to display very different data both in terms of content and visually while the item is being dragged than the initial view of the item, which is what is presently being displayed when the item is being dragged, how do i do this?

A: 

Seems to be easier than I thought.

In your initial draggable item create a sub item that has the content you want to display and give it the css value of display: none, then when you're dragging, give the "other" items the value of display: none and to the new item the value of display: block.

Michael
Using this technique it can be done just with CSS rules --- classes used by the avatar are documented, so you can set display:none in CSS without using any JavaScript code.But the proper way to use different representations for different roles is to define a creator function for your sources. Read all about it and about CSS classes in the official documentation: http://docs.dojocampus.org/dojo/dnd
Eugene Lazutkin
+1  A: 

It seems that you may have already found the solution to your problem, but I thought I would just throw in my $.02.

What I did was to use a custom "drag avatar" for each drag handle (in the dojo vernacular, the "avatar" is what is displayed while the user is dragging). Here's a direct snippet from the code:

for ( var i=0; i<this.cellsTotal; ++i ) {
    // Create the DND source
    var source = new dojo.dnd.Source(
        "drag-handle-container-" + i,
        {copyOnly: true, creator: createAvatar}
    );
    // Add stuff to it
    source.insertNodes(null, [{data: i, type: [this.dnd.classes.handle]}]);
}

createAvatar is a function callback previously defined something like this:

var createAvatar = function(item, mode) {
    var node       = dojo.doc.createElement("span");
    node.id        = dojo.dnd.getUniqueId();
    node.itemIndex = parseInt(item.data);
    dojo.addClass(node, "dojoDndItem");

    // Creating the drag avatar or the source node?
    if ( mode == "avatar" ) {
        // Create the DOM for the avatar (what is shown while dragging)
    } else {
        // Create the DOM for the source node (the handle for drag initiation)
    }

    return {node: node, data: item, type: [context.dnd.classes.avatar]};
};

Your method works obviously, but this method frees you from having to worry about toggling styles as Dojo will take care of all of the for you.

p.s.: I hate Dojo DND and its documentation. This took me waaay to long to figure out when I had to do it originally.

Justin Johnson
This is the official documentation: http://docs.dojocampus.org/dojo/dnd --- the creator function is described practically at the beginning. How do you suggest to improve the doc so we have less hate and more love in this world?
Eugene Lazutkin
Hi Euguene, and thanks for your message. dojocampus.com has improved the documentation greatly in comparison to api.dojotoolkit.org. Sadly, when I was using Dojo for the project that my example code came from, dojocampus seemed incomplete. I ended up using this tutorial <http://www.sitepen.com/blog/2008/06/10/dojo-drag-and-drop-1/> to figure out the above problem.
Justin Johnson
I should also add that, while not a fan of Dijit, I am a proponent for Dojo core, specifically the i18n, and package management with `require`, `provide`, `declare`, and `setObject`.
Justin Johnson
I think the best way to improve the documentation is to have good examples in the api.dojotoolkit.org side of things. One is often digging through there to figure out whats available and way too many times you need to revert to reading code in order to understand what in the world is actually going on, which, sort of makes the documentation pointless...
Michael