views:

593

answers:

1

I would like to know if there is a way to move the node programmatically in dojo Dnd? The reason is I would like to revert the changes to the drag and drop when the web service call triggered a failed save on the database. Here is what I have so far.

In my code, the node Id seems to be unrecognized by the dojo.dnd.Container.DelItem. I cannot just use the selected item on the target because this is a asynchronous webservice function callback. So the user may be selecting another node on the container when this is called.

function OnMoveWSComplete(strResult) {
    var resultObj = eval('(' + strResult + ')');
    var sourceContainer = eval('(' + objResult.sourceContainerId + ')');
    var targetContainer = eval('(' + objResult.targetContainerId + ')');

    var targetNodes = targetContainer.getAllNodes();
    for (var iNode = 0; iNode < targetNodes.length; iNode++) {
        var currId = getLastIdFromStr(targetNodes[iNode].id);

        if (currId == resultObj.Id) {
            var targetN = targetNodes[iNode];

            var Name = targetNodes[iNode].childNodes[0].nodeValue;

            targetContainer.delItem(targetNodes[iNode].id);
            var origData = { Id: resultObj.Id, Name: Name };
            sourceContainer.insertNodes(true, origData);

            break;
        }
    }
}

===================================================================================== Adding Solution code to the the Question (Thanks Eugene Lazutkin) [2009/11/30]:

/**
* Move one node from one container to the other
*/
function moveNode(nodeId, sourceContainer, targetContainer) {
    var node = dojo.byId(nodeId);

    // Save the data
    var saveData = sourceContainer.map[nodeId].data;

    // Do the move
    sourceContainer.parent.removeChild(node);
    targetContainer.parent.appendChild(node);

    // Sync the DOM object → map
    sourceContainer.sync();
    targetContainer.sync();

    // Restore data for recreation of data
    targetContainer.map[nodeId].data = saveData;
}
+2  A: 
Eugene Lazutkin
I actually already did the sync() too but it did not work. By the way, if remove then add is not recommended, could you point me to the move function? I could not find any function in documentation for that.
Nassign
There is no special API to move nodes between sources. Did you try the one I sketched out for you in my answer (the last sentence)? OK, I'll add an example.
Eugene Lazutkin
I just read the source code of sync. it seems the reason why the delItem() and sync() call does not work is because delItem() deletes from the map and sync() copies the actual DOM nodes back to the map structure which actually negates the delItem().
Nassign
Thanks your suggestion worked, I just need to save the map data so I could recreate the node again.
Nassign