wesnolte / jOrgChart

A jQuery plugin to draw tree-like structures such as OrgCharts.
992 stars 674 forks source link

Drop callback needed #25

Open dougwo opened 12 years ago

dougwo commented 12 years ago

The simplest way to support all the requests to be able to save modified charts, is to provide a callback function as one of the parameters that gets called when the drop is complete. I tried to add it but ran into an issue that is beyond me. I added the following at the end of the // Drop event handler for nodes section:

// handle callback after drop
if (opts.dropHandler) {
    var a = opts.dropHandler;
    window[a](this);
}

where dropHandler is the new options I added. This works to call the function, but it happens too quickly because the new node positions aren't all drawn yet and if I loop through $('.node').each() ... it finds one more than necessary (the original nodes plus the new temporary dragged node). If someone can tell me how to ensure that the drop and subsequent update of the div is complete before calling my function, I'd appreciate it.

deJong-IT commented 11 years ago

I changed the DragAndDrop option to a callback function that gives back the original id's from the source and target li's

At the end of the drop event handler:

        // Check if the dragAndDrop option is a call back
        if (typeof opts.dragAndDrop == 'function') {
            // Need the original li id's
            var source = $(sourceLi).attr('id');
            var target = $(targetLi).attr('id');
            opts.dragAndDrop.call(this,source,target);
        }

When initializing:

jQuery(document).ready(function() {
    $('#org').jOrgChart({
        chartElement : '#orgchart',
        dragAndDrop  : function(source,target) {
            alert(source+' => '+target);
        }
    });
});

After that you can do whatever you want.

wesleypimentel commented 8 years ago

@deJong-IT, this works great! I can't believe that since 2012 your solution was not merged to the original project. I'm using a version a little better "functional" https://github.com/sselvamani22/jOrgChart and even it has no callback calls, this should be essential! Whatever, i'm come from 2016 only to thank you! :) +1